実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。
高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。
-
今高橋君がいる点を (x,y) とする。体力を 1 消費し、S の i 番目の文字 S_i に応じて以下の点に移動する。
- S_i が
R
のとき: (x+1,y) - S_i が
L
のとき: (x-1,y) - S_i が
U
のとき: (x,y+1) - S_i が
D
のとき: (x,y-1)
- S_i が
-
高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。
高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。
制約
- 1\leq N,M,H,K\leq 2\times 10^5
- S は
R
,L
,U
,D
からなる長さ N の文字列 - |x_i|,|y_i| \leq 2\times 10^5
- (x_i,y_i) は互いに異なる
- S 以外の入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M H K S x_1 y_1 \vdots x_M y_M
出力
高橋君が一度も倒れることなく N 回の移動を行える場合 Yes
を、そうでない場合 No
を出力せよ。
入力例 1
4 2 3 1 RUDL -1 -1 1 0
出力例 1
Yes
初め高橋君の体力は 3 です。以下で移動を説明します。
-
1 回目の移動: S_i が
R
なので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。 -
2 回目の移動: S_i が
U
なので点 (1,1) に移動する。高橋君の体力は 1 に減る。 -
3 回目の移動: S_i が
D
なので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。 -
4 回目の移動: S_i が
L
なので点 (0,0) に移動する。高橋君の体力は 0 に減る。
以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes
を出力してください。体力は 0 になってもいいことに注意してください。
入力例 2
5 2 1 5 LDRLD 0 0 -1 -1
出力例 2
No
初め高橋君の体力は 1 です。以下で移動を説明します。
-
1 回目の移動: S_i が
L
なので点 (-1,0) に移動する。高橋君の体力は 0 に減る。 -
2 回目の移動: S_i が
D
なので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。
以上より、高橋君は倒れてしまうので、No
を出力してください。
高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。
Score : 300 points
Problem Statement
On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).
Takahashi will make N moves. The i-th move is as follows.
-
Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:
- (x+1,y) if S_i is
R
; - (x-1,y) if S_i is
L
; - (x,y+1) if S_i is
U
; - (x,y-1) if S_i is
D
.
- (x+1,y) if S_i is
-
If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.
Determine if Takahashi can complete the N moves without being stunned.
Constraints
- 1\leq N,M,H,K\leq 2\times 10^5
- S is a string of length N consisting of
R
,L
,U
, andD
. - |x_i|,|y_i| \leq 2\times 10^5
- (x_i, y_i) are pairwise distinct.
- All values in the input are integers, except for S.
Input
The input is given from Standard Input in the following format:
N M H K S x_1 y_1 \vdots x_M y_M
Output
Print Yes
if he can complete the N moves without being stunned; print No
otherwise.
Sample Input 1
4 2 3 1 RUDL -1 -1 1 0
Sample Output 1
Yes
Initially, Takahashi's health is 3. We describe the moves below.
-
1-st move: S_i is
R
, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1. -
2-nd move: S_i is
U
, so he moves to point (1,1). His health reduces to 1. -
3-rd move: S_i is
D
, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1. -
4-th move: S_i is
L
, so he moves to point (0,0). His health reduces to 0.
Thus, he can make the 4 moves without collapsing, so Yes
should be printed. Note that the health may reach 0.
Sample Input 2
5 2 1 5 LDRLD 0 0 -1 -1
Sample Output 2
No
Initially, Takahashi's health is 1. We describe the moves below.
-
1-st move: S_i is
L
, so he moves to point (-1,0). His health reduces to 0. -
2-nd move: S_i is
D
, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.
Thus, he will be stunned, so No
should be printed.
Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。
高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。
- 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。
操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 \vdots x_Q
出力
a_1,\ldots,a_N を空白区切りで出力せよ。
入力例 1
5 5 1 2 3 4 5
出力例 1
1 2 3 5 4
操作は以下のように行われます。
- 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
- 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
- 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。
入力例 2
7 7 7 7 7 7 7 7 7
出力例 2
1 2 3 4 5 7 6
入力例 3
10 6 1 5 2 9 6 6
出力例 3
1 2 3 4 5 7 6 8 10 9
Score : 300 points
Problem Statement
N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.
Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.
- Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.
Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q x_1 \vdots x_Q
Output
Print a_1,\ldots,a_N, with spaces in between.
Sample Input 1
5 5 1 2 3 4 5
Sample Output 1
1 2 3 5 4
The operations are performed as follows.
- Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
- Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
- Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.
Sample Input 2
7 7 7 7 7 7 7 7 7
Sample Output 2
1 2 3 4 5 7 6
Sample Input 3
10 6 1 5 2 9 6 6
Sample Output 3
1 2 3 4 5 7 6 8 10 9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数列 (A_1,\ldots,A_K) を使って、高橋君と青木君が石取りゲームをします。
最初、山には N 個の石があります。高橋君から順に、二人が交互に次の操作を行います。
- 現在山にある石の個数以下であるような A_i を 1 つ選ぶ。山から A_i 個の石を取り除く。
山から石がなくなったとき、ゲームは終了します。
二人がともに、ゲーム終了までに自分が取り除いた石の個数を最大化しようとするとき、高橋君は何個の石を取り除くことができますか?
制約
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_K
出力
答えを出力せよ。
入力例 1
10 2 1 4
出力例 1
5
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
この例では高橋君は 5 個の石を取り除くことができます。高橋君が 6 個以上の石を取り除くことはできないためこれが最大です。
高橋君は 5 個の石を取り除くことができるゲームの進行の例には、他にも次のようなものがあります。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
入力例 2
11 4 1 2 3 6
出力例 2
8
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 6 個の石を取り除く。
- 青木君が山から 3 個の石を取り除く。
- 高橋君が山から 2 個の石を取り除く。
この例では高橋君は 8 個の石を取り除くことができます。高橋君が 9 個以上の石を取り除くことはできないためこれが最大です。
入力例 3
10000 10 1 2 4 8 16 32 64 128 256 512
出力例 3
5136
Score : 400 points
Problem Statement
Takahashi and Aoki will play a game of taking stones using a sequence (A_1, \ldots, A_K).
There is a pile that initially contains N stones. The two players will alternately perform the following operation, with Takahashi going first.
- Choose an A_i that is at most the current number of stones in the pile. Remove A_i stones from the pile.
The game ends when the pile has no stones.
If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?
Constraints
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_K
Output
Print the answer.
Sample Input 1
10 2 1 4
Sample Output 1
5
Below is one possible progression of the game.
- Takahashi removes 4 stones from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 1 stone from the pile.
- Aoki removes 1 stone from the pile.
In this case, Takahashi removes 5 stones. There is no way for him to remove 6 or more stones, so this is the maximum.
Below is another possible progression of the game where Takahashi removes 5 stones.
- Takahashi removes 1 stone from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 4 stones from the pile.
- Aoki removes 1 stone from the pile.
Sample Input 2
11 4 1 2 3 6
Sample Output 2
8
Below is one possible progression of the game.
- Takahashi removes 6 stones.
- Aoki removes 3 stones.
- Takahashi removes 2 stones.
In this case, Takahashi removes 8 stones. There is no way for him to remove 9 or more stones, so this is the maximum.
Sample Input 3
10000 10 1 2 4 8 16 32 64 128 256 512
Sample Output 3
5136
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
1 から N の番号がついた N 本のジュースがあります。 このうちちょうど 1 本が腐っていることが判明しました。 そのジュースを微量でも飲むと、翌日お腹を壊してしまいます。
高橋君は翌日までに腐ったジュースを特定しなければなりません。 高橋君はそのために必要な最小の数の友人を呼び、それぞれに N 本のジュースのうちの一部を振る舞うことにしました。 各友人には何本でもジュースを与えることができ、各ジュースは何人の友人にでも与えることができます。
呼ぶ友人の数とジュースの与え方を出力して、翌日に各友人がお腹を壊したかどうかの情報を受け取り、腐ったジュースの番号を出力してください。
制約
- N は整数
- 2 \leq N \leq 100
入出力
この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
対話を行う前にジャッジは、腐ったジュースの番号 X として 1 以上 N 以下の整数を秘密裏に選択します。 X の値はあなたには与えられません。また、対話の途中で X の値が制約および以前の出力に矛盾しない範囲で変わる場合があります。
まず、ジャッジから N が入力から与えられます。
N
あなたは呼ぶ友人の数 M を出力し改行してください。
M
さらに、あなたは次に述べる M 回の出力からなる手続きを行ってください。 i = 1, 2, \ldots, M について i 回目の出力では、 i 番目の友人に飲ませるジュースの本数 K_i および、それら K_i 本のジュースの番号を昇順に並べた列 A_{i, 1}, A_{i, 2}, \ldots, A_{i, K_i} を下記の形式で空白区切りで出力し、改行してください。
K_i A_{i, 1} A_{i, 2} \ldots A_{i, K_i}
その後ジャッジから、各友人が翌日にお腹を壊したかどうかの情報が、0
と 1
のみからなる長さ M の文字列 S として与えられます。
S
i = 1, 2, \ldots, M について、S の i 文字目が 1
のとき、かつそのときに限り、i 番目の友人がお腹を壊したことを表します。
それに対し、あなたは腐ったジュースの番号 X' を出力し、改行してください。
X'
その後、直ちにプログラムを終了してください。
あなたが出力した M が N 本のジュースから腐ったジュースを特定するために必要な最小の友人の数であり、かつ、あなたが出力した X' が腐ったジュースの番号 X と一致していれば、正解となります。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。 特に、プログラムの実行中に実行時エラーが起こった場合に、ジャッジ結果が TLE になる可能性があることに注意してください。 ではなく や
- X' を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
- この問題のジャッジはアダプティブです。つまり、制約および以前の出力に矛盾しない範囲で X の値が変わる場合があります。
入出力例
以下は、N = 3 の場合の入出力例です。
入力 | 出力 | 説明 |
---|---|---|
3 |
ジュースの本数 N が与えられます。 | |
2 |
呼ぶ友人の数 M を出力します。 | |
2 1 2 |
1 人目の友人にジュース 1 とジュース 2 を与えます。 | |
1 2 |
2 人目の友人に、ジュース 2 を与えます。 | |
10 |
翌日に各友人がお腹を壊したかどうかを表す文字列 S が与えられます。 | |
1 |
腐ったジュースの番号を出力します。 | |
Score: 425 points
Problem Statement
This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).
There are N bottles of juice, numbered 1 to N. It has been discovered that exactly one of these bottles has gone bad. Even a small sip of the spoiled juice will cause stomach upset the next day.
Takahashi must identify the spoiled juice by the next day. To do this, he decides to call the minimum necessary number of friends and serve them some of the N bottles of juice. He can give any number of bottles to each friend, and each bottle of juice can be given to any number of friends.
Print the number of friends to call and how to distribute the juice, then receive information on whether each friend has an upset stomach the next day, and print the spoiled bottle's number.
Constraints
- N is an integer.
- 2 \leq N \leq 100
Input/Output
This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).
Before the interaction, the judge secretly selects an integer X between 1 and N as the spoiled bottle's number. The value of X is not given to you. Also, the value of X may change during the interaction as long as it is consistent with the constraints and previous outputs.
First, the judge will give you N as input.
N
You should print the number of friends to call, M, followed by a newline.
M
Next, you should perform the following procedure to print M outputs. For i = 1, 2, \ldots, M, the i-th output should contain the number K_i of bottles of juice you will serve to the i-th friend, and the K_i bottles' numbers in ascending order, A_{i, 1}, A_{i, 2}, \ldots, A_{i, K_i}, separated by spaces, followed by a newline.
K_i A_{i, 1} A_{i, 2} \ldots A_{i, K_i}
Then, the judge will inform you whether each friend has a stomach upset the next day by giving you a string S of length M consisting of 0
and 1
.
S
For i = 1, 2, \ldots, M, the i-th friend has a stomach upset if and only if the i-th character of S is 1
.
You should respond by printing the number of the spoiled juice bottle X', followed by a newline.
X'
Then, terminate the program immediately.
If the M you printed is the minimum necessary number of friends to identify the spoiled juice out of the N bottles, and the X' you printed matches the spoiled bottle's number X, then your program is considered correct.
Notes
- Each output should end with a newline and flushing Standard Output. Otherwise, you may receive a TLE verdict.
- The verdict is indeterminate if your program prints an invalid output during the interaction or terminates prematurely. In particular, note that if a runtime error occurs during the execution of the program, the verdict may be TLE instead of . or
- Terminate the program immediately after printing X'. Otherwise, the verdict is indeterminate.
- The judge for this problem is adaptive, meaning that the value of X may change as long as it is consistent with the constraints and previous outputs.
Sample Input/Output
Below is an interaction with N = 3.
Input | Output | Description |
---|---|---|
3 |
The number of bottles, N, is given. | |
2 |
Print the number of friends to call, M. | |
2 1 2 |
Serve juice 1 and juice 2 to the first friend. | |
1 2 |
Serve juice 2 to the second friend. | |
10 |
The string S is given, indicating whether each friend has a stomach upset the next day. | |
1 |
Print the spoiled bottle's number. | |
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
あなたは店で N 個の商品を買おうとしています。 i 個目の商品の定価は P_i 円です。
また、あなたは M 枚のクーポンを持っています。i 枚目のクーポンを使うと、定価が L_i 円以上の商品を一つ選び、その商品を定価より D_i 円低い価格で買うことができます。
ここで、一つのクーポンは一回までしか使えません。また、複数のクーポンを同じ商品に重ねて使うことはできません。
クーポンを使わなかった商品は定価で買うことになります。 N 個すべての商品を買うのに必要な最小の金額を求めてください。
制約
- 1\leq N,M\leq 2\times 10^5
- 1\leq P_i\leq 10^9
- 1\leq D_i \leq L_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P_1 \ldots P_N L_1 \ldots L_M D_1 \ldots D_M
出力
答えを整数として出力せよ。
入力例 1
3 3 4 3 1 4 4 2 2 3 1
出力例 1
4
2 枚目のクーポンを 1 個目の商品に、 3 枚目のクーポンを 2 個目の商品に使うことを考えます。
このとき、1 個目の商品を 4-3=1 円、2 個目の商品を 3-1=2 円、3 個目の商品を 1 円で買うことになるので、 1+2+1=4 円で全ての商品を買うことができます。
入力例 2
10 5 9 7 1 5 2 2 5 5 7 6 7 2 7 8 2 3 2 4 1 2
出力例 2
37
Score : 500 points
Problem Statement
You are in a store to buy N items. The regular price of the i-th item is P_i yen (the currency in Japan).
You have M coupons. You can use the i-th coupon to buy an item whose regular price is at least L_i yen at a D_i-yen discount.
Here, each coupon can be used only once. Besides, multiple coupons cannot be used for the same item.
If no coupon is used for an item, you will buy it for a regular price. Find the minimum possible total amount of money required to buy all the N items.
Constraints
- 1\leq N,M\leq 2\times 10^5
- 1\leq P_i\leq 10^9
- 1\leq D_i \leq L_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P_1 \ldots P_N L_1 \ldots L_M D_1 \ldots D_M
Output
Print the answer as an integer.
Sample Input 1
3 3 4 3 1 4 4 2 2 3 1
Sample Output 1
4
Consider using the 2-nd coupon for the 1-st item, and the 3-rd coupon for the 2-nd item.
Then, you buy the 1-st item for 4-3=1 yen, 2-nd item for 3-1=2 yen, and 3-rd item for 1 yen. Thus, you can buy all the items for 1+2+1=4 yen.
Sample Input 2
10 5 9 7 1 5 2 2 5 5 7 6 7 2 7 8 2 3 2 4 1 2
Sample Output 2
37