実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1,2,\dots,N が 1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。
- 全ての i (1 \leq i \leq N) に対して Q の p_i 番目の要素が i である。
ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。
制約
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) は長さ N の順列である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N p_1 p_2 \dots p_N
出力
数列 Q を空白区切りで 1 行で出力せよ。
q_1 q_2 \dots q_N
入力例 1
3 2 3 1
出力例 1
3 1 2
以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。
- i = 1 のとき p_i = 2, q_2 = 1
- i = 2 のとき p_i = 3, q_3 = 2
- i = 3 のとき p_i = 1, q_1 = 3
入力例 2
3 1 2 3
出力例 2
1 2 3
全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。
入力例 3
5 5 3 2 4 1
出力例 3
5 3 2 4 1
Score : 300 points
Problem Statement
We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.
- For every i (1 \leq i \leq N), the p_i-th element of Q is i.
It can be proved that there exists a unique Q that satisfies the condition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N p_1 p_2 \dots p_N
Output
Print the sequence Q in one line, with spaces in between.
q_1 q_2 \dots q_N
Sample Input 1
3 2 3 1
Sample Output 1
3 1 2
The permutation Q=(3,1,2) satisfies the condition, as follows.
- For i = 1, we have p_i = 2, q_2 = 1.
- For i = 2, we have p_i = 3, q_3 = 2.
- For i = 3, we have p_i = 1, q_1 = 3.
Sample Input 2
3 1 2 3
Sample Output 2
1 2 3
If p_i = i for every i (1 \leq i \leq N), we will have P = Q.
Sample Input 3
5 5 3 2 4 1
Sample Output 3
5 3 2 4 1
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。
高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。
- 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う
その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。
高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
6 1 2 4 6 7 271
出力例 1
4
『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。
入力例 2
10 1 1 1 1 1 1 1 1 1 1
出力例 2
5
高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。
- 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う
入力例 3
1 5
出力例 3
0
高橋君は 1 巻を読めません。
Score : 300 points
Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.
Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:
- Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
6 1 2 4 6 7 271
Sample Output 1
4
Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.
Sample Input 2
10 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:
- Sell two books of Volume 1 and buy a book of Volume 2 instead.
- Sell two books of Volume 1 and buy a book of Volume 3 instead.
- Sell two books of Volume 1 and buy a book of Volume 4 instead.
- Sell two books of Volume 1 and buy a book of Volume 5 instead.
Sample Input 3
1 5
Sample Output 3
0
Takahashi cannot read Volume 1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
ジャッジが 0 と 1 のみからなる長さ N の文字列 S = S_1S_2\ldots S_N を持っています。 文字列 S は、S_1 = 0 および S_N = 1 を満たします。
あなたには S の長さ N が与えられますが、S 自体は与えられません。 その代わり、あなたはジャッジに対して以下の質問を 20 回まで行うことができます。
- 1 \leq i \leq N を満たす整数 i を選び、S_i の値を尋ねる。
1 \leq p \leq N-1 かつ S_p \neq S_{p+1} を満たす整数 p を 1 個出力してください。
なお、本問題の条件下でそのような整数 p が必ず存在することが示せます。
制約
- 2 \leq N \leq 2 \times 10^5
入出力
最初に、文字列 S の長さ N を標準入力から受け取ってください。
N
次に、あなたはジャッジに対して問題文中の質問を 20 回まで繰り返すことができます。
質問は、以下の形式で標準出力に出力してください。 ここで、i は 1 \leq i \leq N を満たす整数でなければなりません。
? i
これに対する応答として、S_i の値が次の形式で標準入力から与えられます。
S_i
ここで、S_i は 0 または 1 です。
問題文中の条件を満たす整数 p を見つけたら、解答を以下の形式で出力してください。 その後、ただちにプログラムを終了してください。
! p
答えが複数ある場合、どれを出力しても正解とみなされます。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
- 文字列 S はあなたとジャッジの対話の開始時に固定され、あなたが行った質問などに応じて変更されることはありません。
入出力例
以下は、N = 7, S = 0010011 の場合の入出力例です。
| 入力 | 出力 | 説明 |
|---|---|---|
7 |
N が与えられます。 | |
? 1 |
S_1 が何かをジャッジに質問します。 | |
0 |
質問に対する答えとして S_1 = 0 がジャッジから返されます。 | |
? 6 |
S_6 が何かをジャッジに質問します。 | |
1 |
質問に対する答えとして S_6 = 1 がジャッジから返されます。 | |
? 5 |
S_5 が何かをジャッジに質問します。 | |
0 |
質問に対する答えとして S_5 = 0 がジャッジから返されます。 | |
! 5 |
問題文中の条件を満たす整数 p として、p = 5 を解答します。 | |
解答した p = 5 について、1 \leq p \leq N-1 かつ S_p \neq S_{p+1} が成り立ちます。 よって、この後ただちにプログラムを終了することで、正解と判定されます。
Score : 400 points
Problem Statement
This is an interactive task, where your program and the judge interact via Standard Input and Output.
The judge has a string of length N consisting of 0 and 1: S = S_1S_2\ldots S_N. Here, S_1 = 0 and S_N = 1.
You are given the length N of S, but not the contents of S. Instead, you can ask the judge at most 20 questions as follows.
- Choose an integer i such that 1 \leq i \leq N and ask the value of S_i.
Print an integer p such that 1 \leq p \leq N-1 and S_p \neq S_{p+1}.
It can be shown that such p always exists under the settings of this problem.
Constraints
- 2 \leq N \leq 2 \times 10^5
Input and Output
First, receive the length N of the string S from Standard Input:
N
Then, you get to ask the judge at most 20 questions as described in the problem statement.
Print each question to Standard Output in the following format, where i is an integer satisfying 1 \leq i \leq N:
? i
In response to this, the value of S_i will be given from Standard Input in the following format:
S_i
Here, S_i is 0 or 1.
When you find an integer p satisfying the condition in the problem statement, print it in the following format, and immediately quit the program:
! p
If multiple solutions exist, you may print any of them.
Notes
- Print a newline and flush Standard Output at the end of each message. Otherwise, you may get a TLE verdict.
- If there is malformed output during the interaction or your program quits prematurely, the verdict will be indeterminate.
- After printing the answer, immediately quit the program. Otherwise, the verdict will be indeterminate.
- The string S will be fixed at the start of the interaction and will not be changed according to your questions or other factors.
Sample Input and Output
In the following interaction, N = 7 and S = 0010011.
| Input | Output | Description |
|---|---|---|
7 |
N is given. | |
? 1 |
Ask the value of S_1. | |
0 |
The judge responds with S_1 = 0. | |
? 6 |
Ask the value of S_6. | |
1 |
The judge responds with S_6 = 1. | |
? 5 |
Ask the value of S_5. | |
0 |
The judge responds with S_5 = 0. | |
! 5 |
Present p = 5 as an integer satisfying the condition. | |
For the presented p = 5, we have 1 \leq p \leq N-1 and S_p \neq S_{p+1}. Thus, if the program immediately quits here, this case will be judged as correctly solved.
実行時間制限: 6 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) があり、最初全ての項が 0 です。
入力で与えられる整数 K を用いて関数 f(A) を以下のように定義します。
- A を降順に (広義単調減少となるように) ソートしたものを B とする。
- このとき、 f(A)=B_1 + B_2 + \dots + B_K とする。
この数列に合計 Q 回の更新を行うことを考えます。
数列 A に対し以下の更新を i=1,2,\dots,Q の順に行い、各更新ごとにその時点での f(A) の値を出力してください。
- A_{X_i} を Y_i に変更する。
制約
- 入力は全て整数
- 1 \le K \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le X_i \le N
- 0 \le Y_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
出力
全体で Q 行出力せよ。そのうち i 行目には i 回目の更新を終えた時点での f(A) の値を整数として出力せよ。
入力例 1
4 2 10 1 5 2 1 3 3 4 2 2 10 1 0 4 0 3 1 2 0 3 0
出力例 1
5 6 8 8 15 13 13 11 1 0
この入力では N=4,K=2 です。 Q=10 回の更新を行います。
- 1 回目の更新を受けて A=(5,0,0,0) となります。このとき f(A)=5 です。
- 2 回目の更新を受けて A=(5,1,0,0) となります。このとき f(A)=6 です。
- 3 回目の更新を受けて A=(5,1,3,0) となります。このとき f(A)=8 です。
- 4 回目の更新を受けて A=(5,1,3,2) となります。このとき f(A)=8 です。
- 5 回目の更新を受けて A=(5,10,3,2) となります。このとき f(A)=15 です。
- 6 回目の更新を受けて A=(0,10,3,2) となります。このとき f(A)=13 です。
- 7 回目の更新を受けて A=(0,10,3,0) となります。このとき f(A)=13 です。
- 8 回目の更新を受けて A=(0,10,1,0) となります。このとき f(A)=11 です。
- 9 回目の更新を受けて A=(0,0,1,0) となります。このとき f(A)=1 です。
- 10 回目の更新を受けて A=(0,0,0,0) となります。このとき f(A)=0 です。
Score : 475 points
Problem Statement
We have a sequence A=(A_1,A_2,\dots,A_N) of length N. Initially, all the terms are 0.
Using an integer K given in the input, we define a function f(A) as follows:
- Let B be the sequence obtained by sorting A in descending order (so that it becomes monotonically non-increasing).
- Then, let f(A)=B_1 + B_2 + \dots + B_K.
We consider applying Q updates on this sequence.
Apply the following operation on the sequence A for i=1,2,\dots,Q in this order, and print the value f(A) at that point after each update.
- Change A_{X_i} to Y_i.
Constraints
- All input values are integers.
- 1 \le K \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le X_i \le N
- 0 \le Y_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
Output
Print Q lines in total. The i-th line should contain the value f(A) as an integer when the i-th update has ended.
Sample Input 1
4 2 10 1 5 2 1 3 3 4 2 2 10 1 0 4 0 3 1 2 0 3 0
Sample Output 1
5 6 8 8 15 13 13 11 1 0
In this input, N=4 and K=2. Q=10 updates are applied.
- The 1-st update makes A=(5, 0,0,0). Now, f(A)=5.
- The 2-nd update makes A=(5, 1,0,0). Now, f(A)=6.
- The 3-rd update makes A=(5, 1,3,0). Now, f(A)=8.
- The 4-th update makes A=(5, 1,3,2). Now, f(A)=8.
- The 5-th update makes A=(5,10,3,2). Now, f(A)=15.
- The 6-th update makes A=(0,10,3,2). Now, f(A)=13.
- The 7-th update makes A=(0,10,3,0). Now, f(A)=13.
- The 8-th update makes A=(0,10,1,0). Now, f(A)=11.
- The 9-th update makes A=(0, 0,1,0). Now, f(A)=1.
- The 10-th update makes A=(0, 0,0,0). Now, f(A)=0.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
黒板に 1 以上 M 以下の整数からなる集合 N 個 S_1,S_2,\dots,S_N が書かれています。ここで、S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。
あなたは、以下の操作を好きな回数(0 回でもよい)行うことが出来ます。
- 1 個以上の共通した要素を持つ 2 個の集合 X,Y を選ぶ。X,Y の 2 個を黒板から消し、新たに X\cup Y を黒板に書く。
ここで、X\cup Y とは X か Y の少なくともどちらかに含まれている要素のみからなる集合を意味します。
1 と M が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
1 と M が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。
入力例 1
3 5 2 1 2 2 2 3 3 3 4 5
出力例 1
2
まず、\lbrace 1,2 \rbrace と \lbrace 2,3 \rbrace を選んで消し、\lbrace 1,2,3 \rbrace を追加します。
そして、\lbrace 1,2,3 \rbrace と \lbrace 3,4,5 \rbrace を選んで消し、\lbrace 1,2,3,4,5 \rbrace を追加します。
すると 2 回の操作で 1 と M を両方含む集合を作ることが出来ます。1 回の操作では目標を達成できないため、答えは 2 です。
入力例 2
1 2 2 1 2
出力例 2
0
始めから S_1 が 1,M を共に含むため、必要な操作回数の最小値は 0 回です。
入力例 3
3 5 2 1 3 2 2 4 3 2 4 5
出力例 3
-1
入力例 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
出力例 4
2
Score : 500 points
Problem Statement
On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M. Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace.
You may perform the following operation any number of times (possibly zero):
- choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead.
Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y.
Determine if one can obtain a set containing both 1 and M. If it is possible, find the minimum number of operations required to obtain it.
Constraints
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
If one can obtain a set containing both 1 and M, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.
Sample Input 1
3 5 2 1 2 2 2 3 3 3 4 5
Sample Output 1
2
First, choose and remove \lbrace 1,2 \rbrace and \lbrace 2,3 \rbrace to obtain \lbrace 1,2,3 \rbrace.
Then, choose and remove \lbrace 1,2,3 \rbrace and \lbrace 3,4,5 \rbrace to obtain \lbrace 1,2,3,4,5 \rbrace.
Thus, one can obtain a set containing both 1 and M with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 2.
Sample Input 2
1 2 2 1 2
Sample Output 2
0
S_1 already contains both 1 and M, so the minimum number of operations required is 0.
Sample Input 3
3 5 2 1 3 2 2 4 3 2 4 5
Sample Output 3
-1
Sample Input 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
Sample Output 4
2