実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 N と長さ N の数列 S=(S_1,\ldots,S_N) が与えられます。
長さ N の数列 A=(A_1,\ldots,A_N) であって、k=1,\ldots,N の全てについて以下の条件を満たすものを求めてください。
- A_1+A_2+\ldots+A_k = S_k
なお、このような数列 A は必ず存在し、一意に定まります。
制約
- 1 \leq N \leq 10
- -10^9\leq S_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N S_1 \ldots S_N
出力
全ての条件を満たす数列 A=(A_1,\ldots,A_N) の各要素を、順に空白区切りで出力せよ。
入力例 1
3 3 4 8
出力例 1
3 1 4
- A_1=3=S_1
- A_1+A_2=3+1=4=S_2
- A_1+A_2+A_3=3+1+4=8=S_3
であり、たしかに全ての条件を満たしています。
入力例 2
10 314159265 358979323 846264338 -327950288 419716939 -937510582 97494459 230781640 628620899 -862803482
出力例 2
314159265 44820058 487285015 -1174214626 747667227 -1357227521 1035005041 133287181 397839259 -1491424381
Score : 200 points
Problem Statement
You are given an integer N and a sequence S=(S_1,\ldots,S_N) of length N.
Find a sequence A=(A_1,\ldots,A_N) of length N that satisfies the following condition for all k=1,\ldots,N:
- A_1+A_2+\ldots+A_k = S_k.
Such a sequence A always exists and is unique.
Constraints
- 1 \leq N \leq 10
- -10^9\leq S_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N S_1 \ldots S_N
Output
Print the elements of a sequence A=(A_1,\ldots,A_N) that satisfies all the conditions in order, separated by spaces.
Sample Input 1
3 3 4 8
Sample Output 1
3 1 4
The sequence in the output actually satisfies all the conditions:
- A_1=3=S_1;
- A_1+A_2=3+1=4=S_2;
- A_1+A_2+A_3=3+1+4=8=S_3.
Sample Input 2
10 314159265 358979323 846264338 -327950288 419716939 -937510582 97494459 230781640 628620899 -862803482
Sample Output 2
314159265 44820058 487285015 -1174214626 747667227 -1357227521 1035005041 133287181 397839259 -1491424381
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の人がいます。i \, (1 \leq i \leq N) 人目の人の姓は S_i、名は T_i です。
同姓同名であるような人の組が存在するか、すなわち 1 \leq i \lt j \leq N かつ S_i=S_j かつ T_i=T_j を満たすような整数対 (i,j) が存在するか判定してください。
制約
- 2 \leq N \leq 1000
- N は整数
- S_i,T_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N
出力
同姓同名であるような人の組が存在するなら Yes を、存在しないなら No を出力せよ。
入力例 1
3 tanaka taro sato hanako tanaka taro
出力例 1
Yes
1 人目の人と 3 人目の人が同姓同名です。
入力例 2
3 saito ichiro saito jiro saito saburo
出力例 2
No
同姓同名であるような人の組は存在しません。
入力例 3
4 sypdgidop bkseq bajsqz hh ozjekw mcybmtt qfeysvw dbo
出力例 3
No
Score : 200 points
Problem Statement
There are N people. The family name and given name of the i-th person (1 \leq i \leq N) are S_i and T_i, respectively.
Determine whether there is a pair of people with the same family and given names. In other words, determine whether there is a pair of integers (i,j) such that 1 \leq i \lt j \leq N, S_i=S_j, and T_i=T_j.
Constraints
- 2 \leq N \leq 1000
- N is an integer.
- Each of S_i and T_i is a string of length between 1 and 10 (inclusive) consisting of English lowercase letters.
Input
Input is given from Standard Input in the following format:
N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N
Output
If there is a pair of people with the same family and given names, print Yes; otherwise, print No.
Sample Input 1
3 tanaka taro sato hanako tanaka taro
Sample Output 1
Yes
The first and third persons have the same family and given names.
Sample Input 2
3 saito ichiro saito jiro saito saburo
Sample Output 2
No
No two persons have the same family and given names.
Sample Input 3
4 sypdgidop bkseq bajsqz hh ozjekw mcybmtt qfeysvw dbo
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
非負整数 N が与えられるので、以下の条件を満たす非負整数 x を昇順に全て出力してください。
- x を 2 進数として表記した時に 1 となる位の集合が、 N を 2 進数として表記した時に 1 となる位の集合の部分集合となる。
- すなわち、全ての非負整数 k について、「 x の 2^k の位が 1 ならば、 N の 2^k の位は 1 」が成り立つ。
制約
- N は整数
- 0 \le N < 2^{60}
- N を 2 進数として表記した時、 1 となる位は 15 個以下である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを 1 行に 1 つずつ、10 進法の整数として昇順に出力せよ。
入力例 1
11
出力例 1
0 1 2 3 8 9 10 11
N = 11_{(10)} を 2 進数で表記すると、 1011_{(2)} となります。
条件を満たす非負整数 x は以下の通りです。
- 0000_{(2)}=0_{(10)}
- 0001_{(2)}=1_{(10)}
- 0010_{(2)}=2_{(10)}
- 0011_{(2)}=3_{(10)}
- 1000_{(2)}=8_{(10)}
- 1001_{(2)}=9_{(10)}
- 1010_{(2)}=10_{(10)}
- 1011_{(2)}=11_{(10)}
入力例 2
0
出力例 2
0
入力例 3
576461302059761664
出力例 3
0 524288 549755813888 549756338176 576460752303423488 576460752303947776 576461302059237376 576461302059761664
入力は 32bit 符号付き整数に収まらない可能性があります。
Score : 300 points
Problem Statement
You are given a non-negative integer N. Print all non-negative integers x that satisfy the following condition in ascending order.
- The set of the digit positions containing 1 in the binary representation of x is a subset of the set of the digit positions containing 1 in the binary representation of N.
- That is, the following holds for every non-negative integer k: if the digit in the "2^ks" place of x is 1, the digit in the 2^ks place of N is 1.
Constraints
- N is an integer.
- 0 \le N < 2^{60}
- In the binary representation of N, at most 15 digit positions contain 1.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as decimal integers in ascending order, each in its own line.
Sample Input 1
11
Sample Output 1
0 1 2 3 8 9 10 11
The binary representation of N = 11_{(10)} is 1011_{(2)}.
The non-negative integers x that satisfy the condition are:
- 0000_{(2)}=0_{(10)}
- 0001_{(2)}=1_{(10)}
- 0010_{(2)}=2_{(10)}
- 0011_{(2)}=3_{(10)}
- 1000_{(2)}=8_{(10)}
- 1001_{(2)}=9_{(10)}
- 1010_{(2)}=10_{(10)}
- 1011_{(2)}=11_{(10)}
Sample Input 2
0
Sample Output 2
0
Sample Input 3
576461302059761664
Sample Output 3
0 524288 549755813888 549756338176 576460752303423488 576460752303947776 576461302059237376 576461302059761664
The input may not fit into a 32-bit signed integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
人 1, 人 2,\ldots, 人 N の N 人が一列に並んでいます。
並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。
A _ i\ (1\leq i\leq N) は次のような情報を表しています。
- A _ i=-1 のとき、人 i は先頭に並んでいる。
- A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。
列に並んでいる人の番号を先頭から順番に出力してください。
制約
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
- 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
人 s _ 1, 人 s _ 2,\ldots, 人 s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。
入力例 1
6 4 1 -1 5 3 2
出力例 1
3 5 4 1 2 6
先頭から、人 3, 人 5, 人 4, 人 1, 人 2, 人 6 がこの順に列に並んでいるとき、与えられた情報と一致しています。
実際、
- 人 1 は人 4 のすぐ後ろに並んでいます。
- 人 2 は人 1 のすぐ後ろに並んでいます。
- 人 3 は先頭に並んでいます。
- 人 4 は人 5 のすぐ後ろに並んでいます。
- 人 5 は人 3 のすぐ後ろに並んでいます。
- 人 6 は人 2 のすぐ後ろに並んでいます。
となり、与えられた情報と一致していることが確認できます。
よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。
入力例 2
10 -1 1 2 3 4 5 6 7 8 9
出力例 2
1 2 3 4 5 6 7 8 9 10
入力例 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
出力例 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Score: 300 points
Problem Statement
There are N people standing in a line: person 1, person 2, \ldots, person N.
You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
A _ i\ (1\leq i\leq N) represents the following information:
- if A _ i=-1, person i is at the front of the line;
- if A _ i\neq -1, person i is right behind person A _ i.
Print the people's numbers in the line from front to back.
Constraints
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
- There is exactly one way to arrange the N people consistent with the information given.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.
Sample Input 1
6 4 1 -1 5 3 2
Sample Output 1
3 5 4 1 2 6
If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:
- person 1 is standing right behind person 4,
- person 2 is standing right behind person 1,
- person 3 is at the front of the line,
- person 4 is standing right behind person 5,
- person 5 is standing right behind person 3, and
- person 6 is standing right behind person 2.
Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.
Sample Input 2
10 -1 1 2 3 4 5 6 7 8 9
Sample Output 2
1 2 3 4 5 6 7 8 9 10
Sample Input 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
Sample Output 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
英小文字、(、) からなる文字列のうち、以下の手順によって空文字列になるものを良い文字列と呼びます:
- まず、英小文字をすべて削除する。
- 次に、連続する
()が存在する限り、それを削除する。
例えば、((a)ba) は英小文字をすべて削除すると (()) となり、2 文字目と 3 文字目に連続する () を削除すると () となり、最終的に空文字列にすることができるので良い文字列です。
良い文字列 S が与えられます。 S の i 文字目を S_i で表します。
各英小文字 a , b , \ldots , z に対して、その文字が書かれたボールが 1 つあります。
また、空の箱があります。
高橋君は i = 1,2, \ldots ,|S| に対してこの順に気を失わない限り操作を行います。
- S_i が英小文字ならば、その英小文字が書かれたボールを箱に入れる。ただし、そのボールがすでに箱に入っている場合、高橋君は気を失う。
- S_i が
(ならば、何もしない。 - S_i が
)ならば、i 未満の整数 j であって、S の j 番目から i 番目までの文字からなる文字列が良い文字列となる最大の整数 j を取る。(このような整数 j は必ず存在することが証明できる。)j 番目から i 番目までの操作で箱に入れたボールをすべて、箱から取り出す。
高橋君が気を失わずに一連の操作を完了させられるか判定してください。
制約
- 1 \leq |S| \leq 3 \times 10^5
- S は良い文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
高橋君が気を失わずに一連の操作を完了させられる場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
((a)ba)
出力例 1
Yes
i = 1 のとき、高橋君は何もしません。
i = 2 のとき、高橋君は何もしません。
i = 3 のとき、高橋君は a の書かれたボールを箱の中に入れます。
i = 4 のとき、4 未満の整数 j であって、S の j 番目から 4 番目までの文字からなる文字列が良い文字列となる最大の整数は 2 であるため、高橋君は a の書かれたボールを箱から取り出します。
i = 5 のとき、高橋君は b の書かれたボールを箱の中に入れます。
i = 6 のとき、高橋君は a の書かれたボールを箱の中に入れます。
i = 7 のとき、7 未満の整数 j であって、S の j 番目から 7 番目までの文字からなる文字列が良い文字列となる最大の整数は 1 であるため、高橋君は a の書かれたボールと b の書かれたボールを箱から取り出します。
したがってこの場合の答えは Yes となります。
入力例 2
(a(ba))
出力例 2
No
i = 1 のとき、高橋君は何もしません。
i = 2 のとき、高橋君は a の書かれたボールを箱の中に入れます。
i = 3 のとき、高橋君は何もしません。
i = 4 のとき、高橋君は b の書かれたボールを箱の中に入れます。
i = 5 のとき、a の書かれたボールはすでに箱に入っているため、高橋君は気を失い、これ以降の操作は行われません。
したがってこの場合の答えは No となります。
入力例 3
(((())))
出力例 3
Yes
入力例 4
abca
出力例 4
No
Score : 400 points
Problem Statement
A string consisting of lowercase English letters, (, and ) is said to be a good string if you can make it an empty string by the following procedure:
- First, remove all lowercase English letters.
- Then, repeatedly remove consecutive
()while possible.
For example, ((a)ba) is a good string, because removing all lowercase English letters yields (()), from which we can remove consecutive () at the 2-nd and 3-rd characters to obtain (), which in turn ends up in an empty string.
You are given a good string S. We denote by S_i the i-th character of S.
For each lowercase English letter a, b, \ldots, and z, we have a ball with the letter written on it.
Additionally, we have an empty box.
For each i = 1,2, \ldots ,|S| in this order, Takahashi performs the following operation unless he faints.
- If S_i is a lowercase English letter, put the ball with the letter written on it into the box. If the ball is already in the box, he faints.
- If S_i is
(, do nothing. - If S_i is
), take the maximum integer j less than i such that the j-th through i-th characters of S form a good string. (We can prove that such an integer j always exists.) Take out from the box all the balls that he has put in the j-th through i-th operations.
Determine if Takahashi can complete the sequence of operations without fainting.
Constraints
- 1 \leq |S| \leq 3 \times 10^5
- S is a good string.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if he can complete the sequence of operations without fainting; print No otherwise.
Sample Input 1
((a)ba)
Sample Output 1
Yes
For i = 1, he does nothing.
For i = 2, he does nothing.
For i = 3, he puts the ball with a written on it into the box.
For i = 4, j=2 is the maximum integer less than 4 such that the j-th through 4-th characters of S form a good string, so he takes out the ball with a written on it from the box.
For i = 5, he puts the ball with b written on it into the box.
For i = 6, he puts the ball with a written on it into the box.
For i = 7, j=1 is the maximum integer less than 7 such that the j-th through 7-th characters of S form a good string, so he takes out the ball with a written on it, and another with b, from the box.
Therefore, the answer to this case is Yes.
Sample Input 2
(a(ba))
Sample Output 2
No
For i = 1, he does nothing.
For i = 2, he puts the ball with a written on it into the box.
For i = 3, he does nothing.
For i = 4, he puts the ball with b written on it into the box.
For i = 5, the ball with a written on it is already in the box, so he faints, aborting the sequence of operations.
Therefore, the answer to this case is No.
Sample Input 3
(((())))
Sample Output 3
Yes
Sample Input 4
abca
Sample Output 4
No