Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
不思議なボタンがあります。 このボタンを押すと飴を 1 つもらえますが、前回飴をもらってからの経過時間が C 秒未満である場合はもらえません。
高橋君はこのボタンを N 回押してみることにしました。 i 回目にボタンを押すのは今から T_i 秒後です。
高橋君は何個の飴をもらうことができますか?
制約
- 1\leq N \leq 100
- 1\leq C\leq 1000
- 0\leq T_1 < T_2 < \dots < T_N \leq 1000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N C T_1 T_2 \dots T_N
出力
高橋君がもらうことのできる飴の個数を出力せよ。
入力例 1
6 5 1 3 7 8 10 12
出力例 1
3
高橋君はボタンを 6 回押します。
- 1 回目(今から 1 秒後):初めてボタンを押すときは必ず飴を 1 つもらえます。
- 2 回目(今から 3 秒後):前回飴をもらってからの経過時間が 3-1=2<C 秒なので、飴はもらえません。
- 3 回目(今から 7 秒後):前回飴をもらってからの経過時間が 7-1=6\geq C 秒なので、飴を 1 つもらえます。
- 4 回目(今から 8 秒後):前回飴をもらってからの経過時間が 8-7=1<C 秒なので、飴はもらえません。
- 5 回目(今から 10 秒後):前回飴をもらってからの経過時間が 10-7=3<C 秒なので、飴はもらえません。
- 6 回目(今から 12 秒後):前回飴をもらってからの経過時間が 12-7=5\geq C 秒なので、飴を 1 つもらえます。
よって、高橋君は飴を 3 個もらうことができます。
入力例 2
3 2 0 2 4
出力例 2
3
入力例 3
10 3 0 3 4 6 9 12 15 17 19 20
出力例 3
7
Score : 150 points
Problem Statement
There is a mysterious button. When you press this button, you receive one candy, unless less than C seconds have elapsed since you last received a candy.
Takahashi decided to press this button N times. He will press the button for the i-th time T_i seconds from now.
How many candies will he receive?
Constraints
- 1 \leq N \leq 100
- 1 \leq C \leq 1000
- 0 \leq T_1 < T_2 < \dots < T_N \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C T_1 T_2 \dots T_N
Output
Print the number of candies that Takahashi will receive.
Sample Input 1
6 5 1 3 7 8 10 12
Sample Output 1
3
Takahashi will press the button six times.
- 1st press (1 second from now): You always receive a candy when pressing the button for the first time.
- 2nd press (3 seconds from now): 3 - 1 = 2 < C seconds have elapsed since he last received a candy, so he does not receive a candy.
- 3rd press (7 seconds from now): 7 - 1 = 6 \geq C seconds have elapsed since he last received a candy, so he receives a candy.
- 4th press (8 seconds from now): 8 - 7 = 1 < C second has elapsed since he last received a candy, so he does not receive a candy.
- 5th press (10 seconds from now): 10 - 7 = 3 < C seconds have elapsed since he last received a candy, so he does not receive a candy.
- 6th press (12 seconds from now): 12 - 7 = 5 \geq C seconds have elapsed since he last received a candy, so he receives a candy.
Therefore, he receives three candies.
Sample Input 2
3 2 0 2 4
Sample Output 2
3
Sample Input 3
10 3 0 3 4 6 9 12 15 17 19 20
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
A から偶数だけすべて取り出し、もとの順番を保って出力してください。
制約
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A には 1 つ以上偶数が含まれる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
A から偶数を取り出した列を、空白区切りで 1 行に出力せよ。
入力例 1
5 1 2 3 5 6
出力例 1
2 6
A=(1,2,3,5,6) です。 このうち偶数なのは A _ 2=2,A _ 5=6 の 2 つなので、2 と 6 をこの順に空白区切りで出力してください。
入力例 2
5 2 2 2 3 3
出力例 2
2 2 2
A の中には同じ要素がある場合もあります。
入力例 3
10 22 3 17 8 30 15 12 14 11 17
出力例 3
22 8 30 12 14
Score : 100 points
Problem Statement
You are given a sequence of N integers: A=(A _ 1,A _ 2,\ldots,A _ N).
Print all even numbers in A without changing the order.
Constraints
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A contains at least one even number.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
Print a line containing the sequence of all even numbers in A, with spaces in between.
Sample Input 1
5 1 2 3 5 6
Sample Output 1
2 6
We have A=(1,2,3,5,6). Among them are two even numbers, A _ 2=2 and A _ 5=6, so print 2 and 6 in this order, with a space in between.
Sample Input 2
5 2 2 2 3 3
Sample Output 2
2 2 2
A may contain equal elements.
Sample Input 3
10 22 3 17 8 30 15 12 14 11 17
Sample Output 3
22 8 30 12 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 A,B が与えられます。
A+B を(十進法で)計算する時、繰り上がりが生じないなら Easy 、生じるなら Hard と出力してください。
制約
- A,B は整数
- 1 \le A,B \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
繰り上がりが生じないなら Easy 、生じるなら Hard と出力せよ。
入力例 1
229 390
出力例 1
Hard
229+390 を計算する際、十の位から百の位へと繰り上がりが発生します。よって、答えは Hard です。
入力例 2
123456789 9876543210
出力例 2
Easy
繰り上がりは発生しません。答えは Easy です。
また、入力が 32bit 整数に収まらないこともあります。
Score : 200 points
Problem Statement
You are given positive integers A and B.
Let us calculate A+B (in decimal). If it does not involve a carry, print Easy; if it does, print Hard.
Constraints
- A and B are integers.
- 1 \le A,B \le 10^{18}
Input
Input is given from Standard Input in the following format:
A B
Output
If the calculation does not involve a carry, print Easy; if it does, print Hard.
Sample Input 1
229 390
Sample Output 1
Hard
When calculating 229+390, we have a carry from the tens digit to the hundreds digit, so the answer is Hard.
Sample Input 2
123456789 9876543210
Sample Output 2
Easy
We do not have a carry here; the answer is Easy.
Note that the input may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
人 1, 人 2, \dots 人 N の N 人の人がいます。人 i の姓は s_i、名は t_i です。
N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。
- a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
- a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。
N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes を、そうでないならば No を出力してください。
制約
- 2 \leq N \leq 100
- N は整数である。
- s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N s_1 t_1 s_2 t_2 \vdots s_N t_N
出力
N 人すべてにあだ名をつけることが可能ならば Yes を、そうでないならば No を出力せよ。
入力例 1
3 tanaka taro tanaka jiro suzuki hanako
出力例 1
Yes
a_1 = taro, a_2 = jiro, a_3 = hanako とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3 は suzuki でもよいです。)
ここで、a_1 = tanaka とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka であるため、あだ名の条件の 2 つ目を満たさなくなるからです。
入力例 2
3 aaa bbb xxx aaa bbb yyy
出力例 2
No
問題文の条件を満たすあだ名のつけ方は存在しません。
入力例 3
2 tanaka taro tanaka taro
出力例 3
No
同姓同名である人の組が存在する場合もあります。
入力例 4
3 takahashi chokudai aoki kensho snu ke
出力例 4
Yes
a_1 = chokudai, a_2 = kensho, a_3 = ke とすればよいです。
Score : 200 points
Problem Statement
There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.
Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.
- a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
- a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.
Is it possible to give nicknames to all the N people? If it is possible, print Yes; otherwise, print No.
Constraints
- 2 \leq N \leq 100
- N is an integer.
- s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
N s_1 t_1 s_2 t_2 \vdots s_N t_N
Output
If it is possible to give nicknames to all the N people, print Yes; otherwise, print No.
Sample Input 1
3 tanaka taro tanaka jiro suzuki hanako
Sample Output 1
Yes
The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro, a_2 = jiro, a_3 = hanako. (a_3 may be suzuki, too.)
However, note that we cannot let a_1 = tanaka, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka too.
Sample Input 2
3 aaa bbb xxx aaa bbb yyy
Sample Output 2
No
There is no way to give nicknames satisfying the conditions in the Problem Statement.
Sample Input 3
2 tanaka taro tanaka taro
Sample Output 3
No
There may be a pair of people with the same family name and the same given name.
Sample Input 4
3 takahashi chokudai aoki kensho snu ke
Sample Output 4
Yes
We can let a_1 = chokudai, a_2 = kensho, and a_3 = ke.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。 この定義は A 問題と同様です。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
K 番目に小さい 321-like Number を求めてください。
制約
- 入力は全て整数
- 1 \le K
- 321-like Number は K 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
K
出力
K 番目に小さい 321-like Number を整数として出力せよ。
入力例 1
15
出力例 1
32
321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。
入力例 2
321
出力例 2
9610
入力例 3
777
出力例 3
983210
Score : 300 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
Find the K-th smallest 321-like Number.
Constraints
- All input values are integers.
- 1 \le K
- At least K 321-like Numbers exist.
Input
The input is given from Standard Input in the following format:
K
Output
Print the K-th smallest 321-like Number as an integer.
Sample Input 1
15
Sample Output 1
32
The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.
Sample Input 2
321
Sample Output 2
9610
Sample Input 3
777
Sample Output 3
983210
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の商品があります。i = 1, 2, \ldots, N について、i 番目の商品の値段は A_i 円です。
高橋君は K 枚のクーポンを持っています。
1 枚のクーポンは 1 つの商品に対して使用することができ、1 つの商品に対してはクーポンを何枚でも( 0 枚でもよい)使用することができます。
値段が a 円の商品に対して k 枚のクーポンを使用すると、その商品を \max\lbrace a - kX, 0\rbrace 円で買うことができます。
高橋君がすべての商品を買うために支払う合計金額の最小値を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K, X \leq 10^9
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K X A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 4 7 8 3 10 5 13
出力例 1
12
1 番目の商品に対してクーポン 1 枚、3 番目の商品に対してクーポン 1 枚、5 番目の商品に対してクーポン 2 枚を使用すると、
- 1 番目の商品を \max\lbrace A_1-X, 0 \rbrace = 1 円で買うことができ、
- 2 番目の商品を \max\lbrace A_2, 0 \rbrace = 3 円で買うことができ、
- 3 番目の商品を \max\lbrace A_3-X, 0 \rbrace = 3 円で買うことができ、
- 4 番目の商品を \max\lbrace A_4, 0 \rbrace = 5 円で買うことができ、
- 5 番目の商品を \max\lbrace A_5-2X, 0 \rbrace = 0 円で買うことができます。
よって、すべての商品を 1 + 3 + 3 + 5 + 0 = 12 円で買うことができ、これが最小です。
入力例 2
5 100 7 8 3 10 5 13
出力例 2
0
入力例 3
20 815 60 2066 3193 2325 4030 3725 1669 1969 763 1653 159 5311 5341 4671 2374 4513 285 810 742 2981 202
出力例 3
112
Score : 300 points
Problem Statement
There are N items in a shop. For each i = 1, 2, \ldots, N, the price of the i-th item is A_i yen (the currency of Japan).
Takahashi has K coupons.
Each coupon can be used on one item. You can use any number of coupons, possibly zero, on the same item. Using k coupons on an item with a price of a yen allows you to buy it for \max\lbrace a - kX, 0\rbrace yen.
Print the minimum amount of money Takahashi needs to buy all the items.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K, X \leq 10^9
- 1 \leq A_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K X A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 4 7 8 3 10 5 13
Sample Output 1
12
By using 1 coupon on the 1-st item, 1 coupon on the 3-rd item, and 2 coupons on the 5-th item, Takahashi can:
- buy the 1-st item for \max\lbrace A_1-X, 0 \rbrace = 1 yen,
- buy the 2-nd item for \max\lbrace A_2, 0 \rbrace = 3 yen,
- buy the 3-rd item for \max\lbrace A_3-X, 0 \rbrace = 3 yen,
- buy the 4-th item for \max\lbrace A_4, 0 \rbrace = 5 yen,
- buy the 5-th item for \max\lbrace A_5-2X, 0 \rbrace = 0 yen,
for a total of 1 + 3 + 3 + 5 + 0 = 12 yen, which is the minimum possible.
Sample Input 2
5 100 7 8 3 10 5 13
Sample Output 2
0
Sample Input 3
20 815 60 2066 3193 2325 4030 3725 1669 1969 763 1653 159 5311 5341 4671 2374 4513 285 810 742 2981 202
Sample Output 3
112
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
1 から N までの番号が付けられた N 個の商品がベルトコンベア上を流れています。 ベルトコンベアには印字機が取り付けられており、商品 i は今から T_i [μs] 後に印字機の範囲に入り、その D_i [μs] 後に印字機の範囲から出ます。
キーエンスの印字機は、印字機の範囲内にある商品 1 つに一瞬で印字することができます(特に、商品が印字機の範囲に入る瞬間や範囲から出る瞬間に印字することも可能です)。 ただし、1 度印字すると、次に印字するまでに 1 [μs] のチャージ時間が必要です。 印字機が印字をする商品とタイミングをうまく選んだとき、最大で何個の商品に印字することができますか?
制約
- 1\leq N \leq 2\times 10^5
- 1\leq T_i,D_i \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T_1 D_1 T_2 D_2 \vdots T_N D_N
出力
印字機が印字することのできる商品の数の最大値を出力せよ。
入力例 1
5 1 1 1 1 2 1 1 2 1 4
出力例 1
4
以下、今から t [μs] 後のことを単に時刻 t とよびます。
例えば、次のようにして 4 個の商品に印字することができます。
- 時刻 1 : 商品 1,2,4,5 が印字機の範囲に入る。商品 4 に印字する。
- 時刻 2 : 商品 3 が印字機の範囲に入り、商品 1,2 が印字機の範囲から出る。商品 1 に印字する。
- 時刻 3 : 商品 3,4 が印字機の範囲から出る。商品 3 に印字する。
- 時刻 4.5 : 商品 5 に印字する。
- 時刻 5 : 商品 5 が印字機の範囲から出る。
5 個の商品すべてに印字することはできないため、答えは 4 です。
入力例 2
2 1 1 1000000000000000000 1000000000000000000
出力例 2
2
入力例 3
10 4 1 1 2 1 4 3 2 5 1 5 1 4 1 2 1 4 1 2 4
出力例 3
6
Score : 450 points
Problem Statement
There are N products labeled 1 to N flowing on a conveyor belt. A Keyence printer is attached to the conveyor belt, and product i enters the range of the printer T_i microseconds from now and leaves it D_i microseconds later.
The Keyence printer can instantly print on one product within the range of the printer (in particular, it is possible to print at the moment the product enters or leaves the range of the printer). However, after printing once, it requires a charge time of 1 microseconds before it can print again. What is the maximum number of products the printer can print on when the product and timing for the printer to print are chosen optimally?
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq T_i,D_i \leq 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T_1 D_1 T_2 D_2 \vdots T_N D_N
Output
Print the maximum number of products the printer can print on.
Sample Input 1
5 1 1 1 1 2 1 1 2 1 4
Sample Output 1
4
Below, we will simply call the moment t microseconds from now time t.
For example, you can print on four products as follows:
- Time 1 : Products 1,2,4,5 enter the range of the printer. Print on product 4.
- Time 2 : Product 3 enters the range of the printer, and products 1,2 leave the range of the printer. Print on product 1.
- Time 3 : Products 3,4 leave the range of the printer. Print on product 3.
- Time 4.5 : Print on product 5.
- Time 5 : Product 5 leaves the range of the printer.
It is impossible to print on all five products, so the answer is 4.
Sample Input 2
2 1 1 1000000000000000000 1000000000000000000
Sample Output 2
2
Sample Input 3
10 4 1 1 2 1 4 3 2 5 1 5 1 4 1 2 1 4 1 2 4
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
それぞれのマスには整数が書かれています。 i = 1, 2, \ldots, N について、マス (r_i, c_i) には正整数 a_i が書かれており、それら以外のマスには 0 が書かれています。
はじめ、マス (R, C) にコマが置かれています。 高橋君は、コマを「いま置かれているマスから別のマスに移動させる」ことを好きな回数だけ行うことができます。 ただし、コマを移動する際には下記の 2 つの条件をともに満たさなければなりません。
- 移動先のマスに書かれている整数は、移動前のマスに書かれている整数より真に大きい。
- 移動先のマスは移動前のマスと同じ行にある、または、移動先のマスは移動前のマスと同じ列にある。
i = 1, 2, \ldots, N のそれぞれについて、(R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力してください。
制約
- 2 \leq H, W \leq 2 \times 10^5
- 1 \leq N \leq \min(2 \times 10^5, HW)
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- 1 \leq a_i \leq 10^9
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N r_1 c_1 a_1 r_2 c_2 a_2 \vdots r_N c_N a_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には (R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力せよ。
入力例 1
3 3 7 1 1 4 1 2 7 2 1 3 2 3 5 3 1 2 3 2 5 3 3 5
出力例 1
1 0 2 0 3 1 0
マス目に書かれた整数は下記の通りです。
4 7 0 3 0 5 2 5 5
- (R, C) = (r_1, c_1) = (1, 1) の場合、(1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
- (R, C) = (r_2, c_2) = (1, 2) の場合、一度もコマの移動を行うことができません。
- (R, C) = (r_3, c_3) = (2, 1) の場合、(2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 2 回行うことができます。
- (R, C) = (r_4, c_4) = (2, 3) の場合、一度もコマの移動を行うことができません。
- (R, C) = (r_5, c_5) = (3, 1) の場合、(3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 3 回行うことができます。
- (R, C) = (r_6, c_6) = (3, 2) の場合、(3, 2) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
- (R, C) = (r_7, c_7) = (3, 3) の場合、一度もコマの移動を行うことができません。
入力例 2
5 7 20 2 7 8 2 6 4 4 1 9 1 5 4 2 2 7 5 5 2 1 7 2 4 6 6 1 4 1 2 1 10 5 6 9 5 3 3 3 7 9 3 6 3 4 3 4 3 3 10 4 2 1 3 5 4 1 2 6 4 7 9
出力例 2
2 4 1 5 3 6 6 2 7 0 0 4 1 5 3 0 5 2 4 0
Score : 500 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
Each square contains an integer. For each i = 1, 2, \ldots, N, the square (r_i, c_i) contains a positive integer a_i. The other square contains a 0.
Initially, there is a piece on the square (R, C). Takahashi can move the piece to a square other than the square it occupies now, any number of times. However, when moving the piece, both of the following conditions must be satisfied.
- The integer written on the square to which the piece is moved is strictly greater than the integer written on the square from which the piece is moved.
- The squares to and from which the piece is moved are in the same row or the same column.
For each i = 1, 2, \ldots, N, print the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).
Constraints
- 2 \leq H, W \leq 2 \times 10^5
- 1 \leq N \leq \min(2 \times 10^5, HW)
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- 1 \leq a_i \leq 10^9
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W N r_1 c_1 a_1 r_2 c_2 a_2 \vdots r_N c_N a_N
Output
Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).
Sample Input 1
3 3 7 1 1 4 1 2 7 2 1 3 2 3 5 3 1 2 3 2 5 3 3 5
Sample Output 1
1 0 2 0 3 1 0
The grid contains the following integers.
4 7 0 3 0 5 2 5 5
- When (R, C) = (r_1, c_1) = (1, 1), you can move the piece once by moving it as (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_2, c_2) = (1, 2), you cannot move the piece at all.
- When (R, C) = (r_3, c_3) = (2, 1), you can move the piece twice by moving it as (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_4, c_4) = (2, 3), you cannot move the piece at all.
- When (R, C) = (r_5, c_5) = (3, 1), you can move the piece three times by moving it as (3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_6, c_6) = (3, 2), you can move the piece once by moving it as (3, 2) \rightarrow (1, 2).
- When (R, C) = (r_7, c_7) = (3, 3), you cannot move the piece at all.
Sample Input 2
5 7 20 2 7 8 2 6 4 4 1 9 1 5 4 2 2 7 5 5 2 1 7 2 4 6 6 1 4 1 2 1 10 5 6 9 5 3 3 3 7 9 3 6 3 4 3 4 3 3 10 4 2 1 3 5 4 1 2 6 4 7 9
Sample Output 2
2 4 1 5 3 6 6 2 7 0 0 4 1 5 3 0 5 2 4 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
N 個の頂点と M 本の辺からなる単純な無向グラフが与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。 また、i = 1, 2, \ldots, N について、頂点 i には正整数 W_i が割り当てられており、A_i 個のコマが置かれています。
グラフ上にコマが存在する限り、下記の操作を繰り返します。
- まず、グラフ上のコマを 1 個選んで取り除き、そのコマが置かれていた頂点を x とおく。
- x に隣接するいくつかの頂点からなる(空でも良い)集合 S であって、\sum_{y \in S} W_y \lt W_x であるものを選び、S に含まれる頂点それぞれに 1 個ずつコマを置く。
操作を行う回数としてあり得る最大値を出力してください。
なお、どのように操作を行っても、有限回の操作の後にはグラフ上にコマが存在しない状態に至ることが証明出来ます。
制約
- 入力される値はすべて整数
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
出力例 1
5
以下の説明では、各頂点にあるコマの個数を、数列 A = (A_1, A_2, \ldots, A_N) として表します。 はじめ、A = (1, 0, 0, 0, 0, 1) です。
下記の手順で操作を行うことを考えます。
- 頂点 1 にあるコマを 1 個取り除き、頂点 2, 3 にコマを 1 個ずつ置く。その結果、A = (0, 1, 1, 0, 0, 1) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 1) となる。
- 頂点 6 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 0) となる。
- 頂点 3 にあるコマを 1 個取り除き、頂点 2 にコマを 1 個置く。その結果、A = (0, 1, 0, 0, 0, 0) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 0, 0, 0, 0) となる。
上記の手順で操作を行う回数は 5 回であり、これが操作を行う回数としてあり得る最大値です。
入力例 2
2 1 1 2 1 2 0 0
出力例 2
0
この入力例では、はじめからグラフ上にコマが存在しません。
入力例 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
出力例 3
1380
Score: 475 points
Problem Statement
You are given a simple undirected graph consisting of N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge connects vertices u_i and v_i. Also, for i = 1, 2, \ldots, N, vertex i is assigned a positive integer W_i, and there are A_i pieces placed on it.
As long as there are pieces on the graph, repeat the following operation:
- First, choose and remove one piece from the graph, and let x be the vertex on which the piece was placed.
- Choose a (possibly empty) set S of vertices adjacent to x such that \sum_{y \in S} W_y \lt W_x, and place one piece on each vertex in S.
Print the maximum number of times the operation can be performed.
It can be proved that, regardless of how the operation is performed, there will be no pieces on the graph after a finite number of iterations.
Constraints
- All input values are integers.
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
Input
The 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 W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
Sample Output 1
5
In the following explanation, let A = (A_1, A_2, \ldots, A_N) represent the numbers of pieces on the vertices. Initially, A = (1, 0, 0, 0, 0, 1).
Consider performing the operation as follows:
- Remove one piece from vertex 1 and place one piece each on vertices 2 and 3. Now, A = (0, 1, 1, 0, 0, 1).
- Remove one piece from vertex 2. Now, A = (0, 0, 1, 0, 0, 1).
- Remove one piece from vertex 6. Now, A = (0, 0, 1, 0, 0, 0).
- Remove one piece from vertex 3 and place one piece on vertex 2. Now, A = (0, 1, 0, 0, 0, 0).
- Remove one piece from vertex 2. Now, A = (0, 0, 0, 0, 0, 0).
In this procedure, the operation is performed five times, which is the maximum possible number of times.
Sample Input 2
2 1 1 2 1 2 0 0
Sample Output 2
0
In this sample input, there are no pieces on the graph from the beginning.
Sample Input 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
Sample Output 3
1380