Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は、時刻 0 にパソコンの電源をつけ、それからマウスを N 回クリックしました。i(1 \le i \le N) 回目のクリックは時刻 T_i に行われました。
高橋君が時刻 x_1 と時刻 x_2 (ただし x_1 < x_2)にマウスを連続してクリックしたとき、x_2 - x_1 \le D であれば時刻 x_2 にダブルクリックが成立したと言います。
高橋君が最初にダブルクリックを成立させた時刻を求めてください。ただし、高橋君が 1 回もダブルクリックを成立させていないならば -1 を出力してください。
制約
- 1 \le N \le 100
- 1 \le D \le 10^9
- 1 \le T_i \le 10^9(1 \le i \le N)
- T_i < T_{i+1}(1 \le i \le N-1)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D T_1 T_2 \dots T_N
出力
高橋君が 1 回でもダブルクリックを成立させたならば最初にダブルクリックが成立した時刻を、そうでないならば -1 を出力せよ。
入力例 1
4 500 300 900 1300 1700
出力例 1
1300
高橋君は時刻 900,1300 にマウスをクリックしていて、1300 - 900 \le 500 であるため時刻 1300 にダブルクリックが成立しています。
時刻 1300 より前にダブルクリックは成立していないため、1300 を出力してください。
入力例 2
5 99 100 200 300 400 500
出力例 2
-1
高橋君は 1 回もダブルクリックを成立させていません。よって、-1 を出力してください。
入力例 3
4 500 100 600 1100 1600
出力例 3
600
高橋君が複数回ダブルクリックを成立させていても、そのうち最初の時刻のみを出力することに注意してください。
Score : 100 points
Problem Statement
Takahashi turned on a computer at time 0 and clicked the mouse N times. The i-th (1 \le i \le N) click was at time T_i.
If he consecutively clicked the mouse at time x_1 and time x_2 (where x_1 < x_2), a double click is said to be fired at time x_2 if and only if x_2 - x_1 \le D.
What time was a double click fired for the first time? If no double click was fired, print -1 instead.
Constraints
- 1 \le N \le 100
- 1 \le D \le 10^9
- 1 \le T_i \le 10^9(1 \le i \le N)
- T_i < T_{i+1}(1 \le i \le N-1)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N D T_1 T_2 \dots T_N
Output
If at least one double click was fired, print the time of the first such event; otherwise, print -1.
Sample Input 1
4 500 300 900 1300 1700
Sample Output 1
1300
Takahashi clicked the mouse at time 900 and 1300. Since 1300 - 900 \le 500, a double click was fired at time 1300.
A double click had not been fired before time 1300, so 1300 should be printed.
Sample Input 2
5 99 100 200 300 400 500
Sample Output 2
-1
No double click was fired, so print -1.
Sample Input 3
4 500 100 600 1100 1600
Sample Output 3
600
If multiple double clicks were fired, be sure to print only the first such event.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
6 桁の正整数 N が与えられます。
この整数が以下の条件を全て満たすか判定してください。
- N の各桁のうち、 1 は丁度 1 つである。
- N の各桁のうち、 2 は丁度 2 つである。
- N の各桁のうち、 3 は丁度 3 つである。
制約
- N は 100000 \le N \le 999999 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が問題文中の条件を全て満たすなら Yes 、そうでないなら No と 1 行に出力せよ。
入力例 1
123233
出力例 1
Yes
123233 は問題文中の条件を満たすので、 Yes と出力します。
入力例 2
123234
出力例 2
No
123234 は問題文中の条件を満たさないので、 No と出力します。
入力例 3
323132
出力例 3
Yes
入力例 4
500000
出力例 4
No
Score : 100 points
Problem Statement
You are given a 6-digit positive integer N.
Determine whether N satisfies all of the following conditions.
- Among the digits of N, the digit 1 appears exactly once.
- Among the digits of N, the digit 2 appears exactly twice.
- Among the digits of N, the digit 3 appears exactly three times.
Constraints
- N is an integer satisfying 100000 \le N \le 999999.
Input
The input is given from Standard Input in the following format:
N
Output
Print Yes if N satisfies all the conditions described in the problem statement, and No otherwise, in one line.
Sample Input 1
123233
Sample Output 1
Yes
123233 satisfies the conditions in the problem statement, so print Yes.
Sample Input 2
123234
Sample Output 2
No
123234 does not satisfy the conditions in the problem statement, so print No.
Sample Input 3
323132
Sample Output 3
Yes
Sample Input 4
500000
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
. および # からなる文字列 S が与えられます。
以下の条件を全て満たす文字列 T のうち、 o の文字数が最大となるものを一つ求めてください。
- T の長さは S の長さと等しい。
- T は
.、#、oからなる。 - S_i=
#であるとき、またそのときに限り T_i=#である。 - T_i=T_j=
o(i < j) ならば、 T_{i+1},\ldots,T_{j-1} の中に#が 1 つ以上存在する。
制約
- S は
.および#からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
条件を全て満たす文字列 T のうち、 o の文字数が最大となるものを一つ出力せよ。
そのような文字列が複数ある場合、どれを出力しても正答となる。
入力例 1
#..#.
出力例 1
#o.#o
T= #o.#o とすると全ての条件を満たすことが確認できます。
全ての条件を満たす T であって、 o の文字数が 2 より多い文字列は存在しないので #o.#o を出力すると正答となります。
この他にも #.o#o を出力しても正答となります。
入力例 2
#
出力例 2
#
入力例 3
.....
出力例 3
..o..
この他にも o....、.o...、...o.、....o を出力しても正答となります。
入力例 4
...#..#.##.#.
出力例 4
o..#.o#o##o#o
Score : 250 points
Problem Statement
You are given a string S consisting of . and #.
Among all strings T that satisfy all of the following conditions, find one with the maximum number of os.
- The length of T is equal to the length of S.
- T consists of
.,#, oro. - T_i=
#if and only if S_i=#. - If T_i=T_j=
o(i < j), then there exists at least one#among T_{i+1},\ldots,T_{j-1}.
Constraints
- S is a string consisting of
.and#with length between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Output one string with the maximum number of os among all strings T that satisfy all conditions.
If there are multiple such strings, printing any of them will be considered correct.
Sample Input 1
#..#.
Sample Output 1
#o.#o
Setting T= #o.#o satisfies all conditions.
There is no string T that satisfies all conditions and has more than two os, so outputting #o.#o is correct.
Outputting #.o#o is also correct.
Sample Input 2
#
Sample Output 2
#
Sample Input 3
.....
Sample Output 3
..o..
Outputting o...., .o..., ...o., or ....o is also correct.
Sample Input 4
...#..#.##.#.
Sample Output 4
o..#.o#o##o#o
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は漢文の勉強をしていて、漢字を読む順番が分からず困っています。高橋君を助けましょう!
1 から N までの N 個の整数が小さい方から順に 1 列に並んでいます。
整数の間に M 個の「レ」が挟まっています。i 個目の「レ」は、整数 a_i と整数 a_i + 1 の間にあります。
あなたは次の手順に従って、N 個の整数を 1 回ずつ全て読みます。
- まず、頂点に 1 から N までの番号がついた N 頂点 M 辺の無向グラフ G を考える。i 本目の辺は頂点 a_i と頂点 a_i + 1 を結んでいる。
- そして、読まれていない整数が無くなるまで次の操作を繰り返す。
- 読まれていない整数のうち最小のものを x とする。頂点 x が含まれる連結成分 C を選び、C に含まれる頂点の番号を大きい方から順に全て読む。
例えば、整数と「レ」が

という順番で並んでいる場合を考えます。(この場合 N = 5, M = 3, a = (1, 3, 4) です。)
このとき、整数が読まれる順番は以下の手順によって 2, 1, 5, 4, 3 に決定します。
- 最初、読まれていない整数のうち最小のものは 1 であり、グラフ G の頂点 1 を含む連結成分に含まれる頂点は \lbrace 1, 2 \rbrace である。よって 2, 1 がこの順で読まれる。
- 次に、読まれていない整数のうち最小のものは 3 であり、グラフ G の頂点 3 を含む連結成分に含まれる頂点は \lbrace 3, 4, 5 \rbrace である。よって 5, 4, 3 がこの順で読まれる。
- すべての整数が読まれたので手順を終了する。
N, M, (a_1, a_2, \dots, a_M) が入力として与えられるので、 N 個の整数を読む順番を出力してください。
連結成分とは
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。
制約
- 1 \leq N \leq 100
- 0 \leq M \leq N - 1
- 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M a_1 a_2 \dots a_M
出力
答えを以下の形式で出力せよ。ここで p_i は、i 番目に読まれる整数を意味する。
p_1 p_2 \dots p_N
入力例 1
5 3 1 3 4
出力例 1
2 1 5 4 3
問題文の例にある通り、整数と「レ」が

という順で並んでいる場合は 2, 1, 5, 4, 3 の順で読みます。
入力例 2
5 0
出力例 2
1 2 3 4 5
「レ」が存在しない場合もあります。
入力例 3
10 6 1 2 3 7 8 9
出力例 3
4 3 2 1 5 6 10 9 8 7
Score : 200 points
Problem Statement
Studying Kanbun, Takahashi is having trouble figuring out the order to read words. Help him out!
There are N integers from 1 through N arranged in a line in ascending order.
Between them are M "レ" marks. The i-th "レ" mark is between the integer a_i and integer (a_i + 1).
You read each of the N integers once by the following procedure.
- Consider an undirected graph G with N vertices numbered 1 through N and M edges. The i-th edge connects vertex a_i and vertex (a_i+1).
- Repeat the following operation until there is no unread integer:
- let x be the minimum unread integer. Choose the connected component C containing vertex x, and read all the numbers of the vertices contained in C in descending order.
For example, suppose that integers and "レ" marks are arranged in the following order:

(In this case, N = 5, M = 3, and a = (1, 3, 4).)
Then, the order to read the integers is determined to be 2, 1, 5, 4, and 3, as follows:
- At first, the minimum unread integer is 1, and the connected component of G containing vertex 1 has vertices \lbrace 1, 2 \rbrace, so 2 and 1 are read in this order.
- Then, the minimum unread integer is 3, and the connected component of G containing vertex 3 has vertices \lbrace 3, 4, 5 \rbrace, so 5, 4, and 3 are read in this order.
- Now that all integers are read, terminate the procedure.
Given N, M, and (a_1, a_2, \dots, a_M), print the order to read the N integers.
What is a connected component?
A subgraph of a graph is a graph obtained by choosing some vertices and edges from the original graph.A graph is said to be connected if and only if one can travel between any two vertices in the graph via edges.
A connected component is a connected subgraph that is not contained in any larger connected subgraph.
Constraints
- 1 \leq N \leq 100
- 0 \leq M \leq N - 1
- 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M a_1 a_2 \dots a_M
Output
Print the answer in the following format, where p_i is the i-th integers to read.
p_1 p_2 \dots p_N
Sample Input 1
5 3 1 3 4
Sample Output 1
2 1 5 4 3
As described in the Problem Statement, if integers and "レ" marks are arranged in the following order:

then the integers are read in the following order: 2, 1, 5, 4, and 3.
Sample Input 2
5 0
Sample Output 2
1 2 3 4 5
There may be no "レ" mark.
Sample Input 3
10 6 1 2 3 7 8 9
Sample Output 3
4 3 2 1 5 6 10 9 8 7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder 社の入口には特殊なパスコード入力装置があり、 この装置は文字列を 1 つ表示する画面と 2 つのボタンからなります。
画面に表示される文字列を t とします。 t ははじめ空文字列であり、ボタンを押すことで t に以下の変化が起きます。
- ボタン A を押すと、t の末尾に
0が追加される。 - ボタン B を押すと、t に含まれるすべての数字が、それぞれ次の数字に置き換わる。ここで、
0から8までの数字については次の数字は 1 大きな数を表す数字とし、9の次の数字は0とする。
たとえば、t が 1984 のときにボタン A を押すと t は 19840 に変化し、さらにボタン B を押すと t は 20951 に変化します。
文字列 S が与えられます。t が空文字列である状態からボタンを 0 回以上押して t を S に一致させるとき、ボタンを最少で何回押す必要があるかを求めてください。
制約
- S は
0,1,2,3,4,5,6,7,8,9からなる文字列 - 1 \leq |S| \leq 5 \times 10^5 (|S| は S の長さをあらわす)
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
21
出力例 1
4
以下の順にボタンを押せば、t が 21 になります。
- ボタン A を押す。t は
0になる。 - ボタン B を押す。t は
1になる。 - ボタン A を押す。t は
10になる。 - ボタン B を押す。t は
21になる。
4 回より少ない回数ボタンを押して t を 21 にすることはできないので、4 を出力します。
入力例 2
407
出力例 2
17
入力例 3
2025524202552420255242025524
出力例 3
150
Score : 300 points
Problem Statement
At the entrance of AtCoder Inc., there is a special pass-code input device. The device consists of a screen displaying one string, and two buttons.
Let t be the string shown on the screen. Initially, t is the empty string. Pressing a button changes t as follows:
- Pressing button A appends
0to the end of t. - Pressing button B replaces every digit in t with the next digit: for digits
0through8the next digit is the one whose value is larger by 1; the next digit after9is0.
For example, if t is 1984 and button A is pressed, t becomes 19840; if button B is then pressed, t becomes 20951.
You are given a string S. Starting from the empty string, press the buttons zero or more times until t coincides with S. Find the minimum number of button presses required.
Constraints
- S is a string consisting of
0,1,2,3,4,5,6,7,8, and9. - 1 \le |S| \le 5 \times 10^{5}, where |S| is the length of S.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
21
Sample Output 1
4
The following sequence of presses makes t equal to 21.
- Press button A. t becomes
0. - Press button B. t becomes
1. - Press button A. t becomes
10. - Press button B. t becomes
21.
It is impossible to obtain 21 with fewer than four presses, so output 4.
Sample Input 2
407
Sample Output 2
17
Sample Input 3
2025524202552420255242025524
Sample Output 3
150
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder 社はロゴ入りの T シャツを販売しています。
高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、
- S の i 文字目が
0のとき、i 日目に何の予定も入っていません。 - S の i 文字目が
1のとき、i 日目に高橋君は食事に行く予定があります。 - S の i 文字目が
2のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。
高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。
- 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
- 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
- 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
- 一度着用した T シャツは次に洗濯するまで着用できない。
N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。
新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。
制約
- 1\leq M\leq N\leq 1000
- S は
0,1,2のみからなる長さ N の文字列 - N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S
出力
問題文の条件をみたすように行動するために
高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。
入力例 1
6 1 112022
出力例 1
2
高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。
- 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
- 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
- 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
- 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
- 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
- 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、
どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。
入力例 2
3 1 222
出力例 2
3
入力例 3
2 1 01
出力例 3
0
高橋君は新しく T シャツを購入する必要はありません。
Score : 300 points
Problem Statement
AtCoder Inc. sells T-shirts with its logo.
You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,
- if the i-th character of S is
0, he has no plan scheduled for the i-th day; - if the i-th character of S is
1, he plans to go out for a meal on the i-th day; - if the i-th character of S is
2, he plans to attend a competitive programming event on the i-th day.
Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.
- On days he goes out for a meal, he will wear a plain or logo T-shirt.
- On days he attends a competitive programming event, he will wear a logo T-shirt.
- On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
- Once he wears a T-shirt, he cannot wear it again until he washes it.
Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.
Constraints
- 1\leq M\leq N\leq 1000
- S is a string of length N consisting of
0,1, and2. - N and M are integers.
Input
The input is given from Standard Input in the following format:
N M S
Output
Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.
Sample Input 1
6 1 112022
Sample Output 1
2
If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:
- On the first day, he wears a logo T-shirt to go out for a meal.
- On the second day, he wears a plain T-shirt to go out for a meal.
- On the third day, he wears a logo T-shirt to attend a competitive programming event.
- On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
- On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
- On the sixth day, he wears a logo T-shirt to attend a competitive programming event.
If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.
Sample Input 2
3 1 222
Sample Output 2
3
Sample Input 3
2 1 01
Sample Output 3
0
He does not need to buy new T-shirts.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
同時に最大 K 人の客を入れられるレストラン店があります。この店の前には脇道があり、ここで 1 つの待ち行列を管理しています。
時刻 0 の時点で店内に客はおらず、待ち行列も空です。
今日は N 個の団体客が来る予定であり、来訪が早い順に 1 から N までの番号が付けられています。団体客 i の人数は C_i 人で、時刻 A_i に待ち行列の末尾に加わります。また、この団体客は入店してから B_i 単位時間後に退店します。
それぞれの団体客は、以下の 2 条件が同時に満たされた最も早い時刻に、待ち行列を離れて入店します。
- その団体客は、待ち行列の先頭にいる。つまりその団体客は、その時点で待ち行列にいる団体客のうち、最も早く待ち行列に加わっている。
- その団体客と、店内にいるすべての団体客 (ちょうどその時刻に入店したぶんを含み、退店したぶんを除く) の人数を合算すると、K 人以下になる。
それぞれの団体客が入店する時刻を求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 B_1 C_1 \vdots A_N B_N C_N
出力
N 行出力せよ。i 行目 (1 \leq i \leq N) には、団体客 i が入店する時刻を整数で出力せよ。
入力例 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
出力例 1
30 60 105 120
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 3 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わった直後にそのまま入店し、店内の客は 7 名になる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 105 に、団体客 2 が退店し、店内の客は 3 名になる。その直後、団体客 3 が入店し、店内の客は 8 名になる。
- 時刻 120 に、団体客 4 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 150 に、団体客 3 が退店し、店内の客は 5 名になる。
- 時刻 165 に、団体客 4 が退店し、店内の客は 3 名になる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。
入力例 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
出力例 2
30 330 330 330
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 120 に、団体客 4 が待ち行列に加わる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。その直後、団体客 2,3,4 が入店し、店内の客は 9 名になる。
- 時刻 375 に、団体客 2,3,4 が退店し、店内の客は 0 名になる。
入力例 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
出力例 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
Score : 400 points
Problem Statement
There is a restaurant that can accommodate at most K customers simultaneously. In front of this restaurant, there is a side street where one queue is managed.
At time 0, there are no customers in the restaurant, and the queue is also empty.
Today, N groups of customers are scheduled to come, and they are numbered from 1 to N in the order of their arrival. Group i consists of C_i people, joins the end of the queue at time A_i, and leaves the restaurant B_i time units after entering.
Each group enters the restaurant by leaving the queue at the earliest time when both of the following two conditions are satisfied simultaneously:
- The group is at the front of the queue. In other words, the group is the earliest to have joined among those still in the queue at that point.
- When the number of people in that group and all groups currently in the restaurant (including those entering at exactly that time and excluding those leaving) are combined, there are K or fewer people.
Find the time when each group enters the restaurant.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 B_1 C_1 \vdots A_N B_N C_N
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain the time when group i enters the restaurant as an integer.
Sample Input 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
Sample Output 1
30 60 105 120
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 3.
- At time 60, group 2 joins the queue and immediately enters, making the number of customers in the restaurant 7.
- At time 90, group 3 joins the queue.
- At time 105, group 2 leaves, making the number of customers in the restaurant 3. Immediately after, group 3 enters, making the number of customers in the restaurant 8.
- At time 120, group 4 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 150, group 3 leaves, making the number of customers in the restaurant 5.
- At time 165, group 4 leaves, making the number of customers in the restaurant 3.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0.
Sample Input 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
Sample Output 2
30 330 330 330
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 60, group 2 joins the queue.
- At time 90, group 3 joins the queue.
- At time 120, group 4 joins the queue.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0. Immediately after, groups 2,3,4 enter, making the number of customers in the restaurant 9.
- At time 375, groups 2,3,4 leave, making the number of customers in the restaurant 0.
Sample Input 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
Sample Output 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
H 行 W 列のグリッドがあり、はじめすべてのマスは色 0 で塗られています。
これから i = 1, 2, \ldots, M の順で以下の操作を行います。
-
T_i = 1 のとき、A_i 行目のマスをすべて色 X_i に塗り替える
-
T_i = 2 のとき、A_i 列目のマスをすべて色 X_i に塗り替える
すべての操作を終えたとき、最終的に色 i で塗られたマスが存在するような各色 i についてその色で塗られたマスの個数を求めてください。
制約
- 1 \leq H, W, M \leq 2 \times 10^5
- T_i \in \lbrace 1, 2 \rbrace
- T_i = 1 なる i に対して 1 \leq A_i \leq H
- T_i = 2 なる i に対して 1 \leq A_i \leq W
- 0 \leq X_i \leq 2 \times 10^5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W M T_1 A_1 X_1 T_2 A_2 X_2 \vdots T_M A_M X_M
出力
色 i で塗られたマスが存在するような整数 i の個数を K として、K + 1 行出力せよ。
1 行目には K の値を出力せよ。
2 行目以降には色 i で塗られたマスが存在するような各色 i について、色の番号およびその色で塗られたマスの個数を出力せよ。
具体的には、i + 1 (1 \leq i \leq K) 行目には色の番号 c_i と色 c_i で塗られたマスの個数 x_i をこの順に空白区切りで出力せよ。
ただし、色の番号は昇順で出力せよ。すなわち、c_1 < c_2 < \ldots < c_K を満たすように出力せよ。また、x_i > 0 が必要であることに注意せよ。
入力例 1
3 4 4 1 2 5 2 4 0 1 3 3 1 3 2
出力例 1
3 0 5 2 4 5 3
操作によってグリッドの各マスの色は以下のように変化します。
0000 0000 0000 0000 0000 0000 → 5555 → 5550 → 5550 → 5550 0000 0000 0000 3333 2222
最終的に色 0 で塗られたマスは 5 つ、色 2 で塗られたマスは 4 つ、色 5 で塗られたマスは 3 つです。
入力例 2
1 1 5 1 1 1 1 1 10 2 1 100 1 1 1000 2 1 10000
出力例 2
1 10000 1
入力例 3
5 5 10 1 1 1 1 2 2 1 3 3 1 4 4 1 5 5 2 1 6 2 2 7 2 3 8 2 4 9 2 5 10
出力例 3
5 6 5 7 5 8 5 9 5 10 5
Score: 450 points
Problem Statement
There is a grid with H rows and W columns. Initially, all cells are painted with color 0.
You will perform the following operations in the order i = 1, 2, \ldots, M.
-
If T_i = 1, repaint all cells in the A_i-th row with color X_i.
-
If T_i = 2, repaint all cells in the A_i-th column with color X_i.
After all operations are completed, for each color i that exists on the grid, find the number of cells that are painted with color i.
Constraints
- 1 \leq H, W, M \leq 2 \times 10^5
- T_i \in \lbrace 1, 2 \rbrace
- 1 \leq A_i \leq H for each i such that T_i = 1,
- 1 \leq A_i \leq W for each i such that T_i = 2.
- 0 \leq X_i \leq 2 \times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W M T_1 A_1 X_1 T_2 A_2 X_2 \vdots T_M A_M X_M
Output
Let K be the number of distinct integers i such that there are cells painted with color i. Print K + 1 lines.
The first line should contain the value of K.
The second and subsequent lines should contain, for each color i that exists on the grid, the color number i and the number of cells painted with that color.
Specifically, the (i + 1)-th line (1 \leq i \leq K) should contain the color number c_i and the number of cells x_i painted with color c_i, in this order, separated by a space.
Here, print the color numbers in ascending order. That is, ensure that c_1 < c_2 < \ldots < c_K. Note also that x_i > 0 is required.
Sample Input 1
3 4 4 1 2 5 2 4 0 1 3 3 1 3 2
Sample Output 1
3 0 5 2 4 5 3
The operations will change the colors of the cells in the grid as follows:
0000 0000 0000 0000 0000 0000 → 5555 → 5550 → 5550 → 5550 0000 0000 0000 3333 2222
Eventually, there are five cells painted with color 0, four with color 2, and three with color 5.
Sample Input 2
1 1 5 1 1 1 1 1 10 2 1 100 1 1 1000 2 1 10000
Sample Output 2
1 10000 1
Sample Input 3
5 5 10 1 1 1 1 2 2 1 3 3 1 4 4 1 5 5 2 1 6 2 2 7 2 3 8 2 4 9 2 5 10
Sample Output 3
5 6 5 7 5 8 5 9 5 10 5
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる単純(自己ループおよび多重辺を持たない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。
下記の 2 つの条件をともに満たす整数列 (A_1, A_2, \ldots, A_k) を長さ k のパスと呼びます。
- すべての i = 1, 2, \dots, k について、1 \leq A_i \leq N 。
- すべての i = 1, 2, \ldots, k-1 について、頂点 A_i と頂点 A_{i+1} は辺で直接結ばれている。
空列も長さ 0 のパスとみなします。
S = s_1s_2\ldots s_N を 0 と 1 のみからなる長さ N の文字列とします。 パス A = (A_1, A_2, \ldots, A_k) が下記を満たすとき、パス A を S に関する良いパスと呼びます。
- すべての i = 1, 2, \ldots, N について、次を満たす。
- s_i = 0 ならば、A に含まれる i の個数は偶数である。
- s_i = 1 ならば、A に含まれる i の個数は奇数である。
S として考えられる文字列(すなわち、0 と 1 のみからなる長さ N の文字列)は 2^N 個ありますが、そのすべてにわたる「 S に関する良いパスのうち最短のものの長さ」の総和を出力してください。
この問題の制約下において、0 と 1 からなる長さ N のどのような文字列を S として選んでも、S に関する良いパスが少なくとも 1 つ存在することが示せます。
制約
- 2 \leq N \leq 17
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq u_i, v_i \leq N
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
3 2 1 2 2 3
出力例 1
14
- S = 000 のとき、空列 () は S に関する最短の良いパスであり、その長さは 0 です。
- S = 100 のとき、(1) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 010 のとき、(2) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 110 のとき、(1, 2) は S に関する最短の良いパスであり、その長さは 2 です。
- S = 001 のとき、(3) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 101 のとき、(1, 2, 3, 2) は S に関する最短の良いパスであり、その長さは 4 です。
- S = 011 のとき、(2, 3) は S に関する最短の良いパスであり、その長さは 2 です。
- S = 111 のとき、(1, 2, 3) は S に関する最短の良いパスであり、その長さは 3 です。
よって、求める答えは 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14 です。
入力例 2
5 5 4 2 2 3 1 3 2 1 1 5
出力例 2
108
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.
A sequence (A_1, A_2, \ldots, A_k) is said to be a path of length k if both of the following two conditions are satisfied:
- For all i = 1, 2, \dots, k, it holds that 1 \leq A_i \leq N.
- For all i = 1, 2, \ldots, k-1, Vertex A_i and Vertex A_{i+1} are directly connected by an edge.
An empty sequence is regarded as a path of length 0.
Let S = s_1s_2\ldots s_N be a string of length N consisting of 0 and 1. A path A = (A_1, A_2, \ldots, A_k) is said to be a good path with respect to S if the following conditions are satisfied:
- For all i = 1, 2, \ldots, N, it holds that:
- if s_i = 0, then A has even number of i's.
- if s_i = 1, then A has odd number of i's.
There are 2^N possible S (in other words, there are 2^N strings of length N consisting of 0 and 1). Find the sum of "the length of the shortest good path with respect to S" over all those S.
Under the Constraints of this problem, it can be proved that, for any string S of length N consisting of 0 and 1, there is at least one good path with respect to S.
Constraints
- 2 \leq N \leq 17
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq u_i, v_i \leq N
- The given graph is simple and connected.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
3 2 1 2 2 3
Sample Output 1
14
- For S = 000, the empty sequence () is the shortest good path with respect to S, whose length is 0.
- For S = 100, (1) is the shortest good path with respect to S, whose length is 1.
- For S = 010, (2) is the shortest good path with respect to S, whose length is 1.
- For S = 110, (1, 2) is the shortest good path with respect to S, whose length is 2.
- For S = 001, (3) is the shortest good path with respect to S, whose length is 1.
- For S = 101, (1, 2, 3, 2) is the shortest good path with respect to S, whose length is 4.
- For S = 011, (2, 3) is the shortest good path with respect to S, whose length is 2.
- For S = 111, (1, 2, 3) is the shortest good path with respect to S, whose length is 3.
Therefore, the sought answer is 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14.
Sample Input 2
5 5 4 2 2 3 1 3 2 1 1 5
Sample Output 2
108