実行時間制限: 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 番の受験生は数学で A_i 点、英語で B_i 点を取りました。
試験の合格者は次のように決められます。
- 数学の点が高い方から X 人を合格とする。
- 次に、この時点でまだ合格となっていない受験者のうち、英語の点が高い方から Y 人を合格とする。
- 次に、この時点でまだ合格となっていない受験者のうち、数学と英語の合計点が高い方から Z 人を合格とする。
- ここまでで合格となっていない受験者は、不合格とする。
ただし、 1. から 3. までのどの段階についても、同点であった場合は受験生の番号の小さい方を優先します。入出力例も参照してください。
以上の手順で合格者を決める時、合格となった受験生の番号を小さい方から順に改行区切りで出力してください。
制約
- 入力は全て整数
- 1 \le N \le 1000
- 0 \le X,Y,Z \le N
- 1 \le X+Y+Z \le N
- 0 \le A_i,B_i \le 100
入力
入力は以下の形式で標準入力から与えられる。
N X Y Z A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
合格となった受験生の番号を小さい方から順に改行区切りで出力せよ。
入力例 1
6 1 0 2 80 60 80 60 70 70 40 20 50 90 90 80
出力例 1
1 4 5
- まず、数学の点が高い方から 1 人が合格となります。
- 数学の最高点は 80 点で 1 番の受験生と 3 番の受験生が並んでいますが、受験生の番号が小さい方が優先され 1 番の受験生が合格となります。
- 次に、まだ合格となっていない受験者のうち、英語の点が高い方から 0 人が合格となります。
- 明らかに、ここで合格者が増えることはありません。
- 次に、まだ合格となっていない受験者のうち、数学と英語の合計点が高い方から 2 人が合格となります。
- まず、まだ合格となっていない受験者の中で、合計点が 160 点と最も高い 5 番の受験生が合格となります。
- 次に、まだ合格となっていない受験者の中で、合計点が 150 点の 4 番の受験生と 6 番の受験生が並んでいます。受験生の番号の小さい方が優先され、 4 番の受験生が合格となります。
以上より、合格となる受験生の番号は 1,4,5 なので、小さい方から出力してください。
入力例 2
5 2 1 2 0 100 0 100 0 0 0 100 100 0
出力例 2
1 2 3 4 5
全員が合格となることもあります。
入力例 3
15 4 3 2 30 65 20 95 100 45 70 85 20 35 95 50 40 15 85 0 25 45 35 65 70 80 90 40 55 20 20 45 75 100
出力例 3
2 4 5 6 7 8 11 14 15
Score : 200 points
Problem Statement
N examinees took an entrance exam.
The examinee numbered i scored A_i points in math and B_i points in English.
The admissions are determined as follows.
- X examinees with the highest math scores are admitted.
- Then, among the examinees who are not admitted yet, Y examinees with the highest English scores are admitted.
- Then, among the examinees who are not admitted yet, Z examinees with the highest total scores in math and English are admitted.
- Those examinees who are not admitted yet are rejected.
Here, in each of the steps 1. to 3., ties are broken by examinees' numbers: an examinee with the smaller examinee's number is prioritized. See also Sample Input and Output.
Print the examinees' numbers of the admitted examinees determined by the steps above in ascending order, separated by newlines.
Constraints
- All values in input are integers.
- 1 \le N \le 1000
- 0 \le X,Y,Z \le N
- 1 \le X+Y+Z \le N
- 0 \le A_i,B_i \le 100
Input
Input is given from Standard Input in the following format:
N X Y Z A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print the examinees' number of the admitted examinees in ascending order, separated by newlines.
Sample Input 1
6 1 0 2 80 60 80 60 70 70 40 20 50 90 90 80
Sample Output 1
1 4 5
- First, 1 examinee with the highest math score is admitted.
- Examinee 1 is tied with Examinee 3, scoring the highest 80 points in math, and the tie is broken by the examinees' numbers, so Examinee 1 is admitted.
- Then, among the examinees who are not admitted yet, 0 examinees with the highest English scores are admitted.
- Obviously, it does not affect the admissions.
- Then, among the examinees who are not admitted yet, 2 examinees with the highest total scores in math and English are admitted.
- First, among the examinees who are not admitted yet, Examinee 5 is admitted, scoring the highest total score of 160 points.
- Next, among the examinees who are not admitted yet, Examinee 4 is tied with Examinee 6, scoring a total score of 150 points. The tie is broken by the examinees' numbers, and Examinee 4 is admitted.
Therefore, the examinees' numbers of the admitted examinees are 1, 4, and 5. Print them in ascending order.
Sample Input 2
5 2 1 2 0 100 0 100 0 0 0 100 100 0
Sample Output 2
1 2 3 4 5
All examinees may be admitted.
Sample Input 3
15 4 3 2 30 65 20 95 100 45 70 85 20 35 95 50 40 15 85 0 25 45 35 65 70 80 90 40 55 20 20 45 75 100
Sample Output 3
2 4 5 6 7 8 11 14 15
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。
N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X 個 (X>0) 存在するならば、X を文字列として扱って S_i+
(+X+)を出力する。
制約
- 1 \leq N \leq 2\times 10^5
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文中の指示にしたがって、N 行出力せよ。
入力例 1
5 newfile newfile newfolder newfile newfolder
出力例 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
入力例 2
11 a a a a a a a a a a a
出力例 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Score : 300 points
Problem Statement
For two strings A and B, let A+B denote the concatenation of A and B in this order.
You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:
- if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
- if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+
(+X+), treating X as a string.
Constraints
- 1 \leq N \leq 2\times 10^5
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines as specified in the Problem Statement.
Sample Input 1
5 newfile newfile newfolder newfile newfolder
Sample Output 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
Sample Input 2
11 a a a a a a a a a a a
Sample Output 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君と青木君は、それぞれ N 個のボールに M 本のひもを取り付けたおもちゃを持っています。
高橋君のおもちゃにおいて、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール A_i とボール B_i を結んでいます。
青木君のおもちゃにおいても同様に、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール C_i とボール D_i を結んでいます。
それぞれのおもちゃにおいて、同一のボールを結ぶようなひもは存在せず、2 つのボールを 2 本以上の異なるひもが結んでいることはありません。
すぬけ君は、2 人のおもちゃが同じ形であるかどうか気になっています。
ここで、2 人のおもちゃが同じ形であるとは、以下の条件を満たす数列 P が存在することをいいます。
- P は (1, \dots, N) を並べ替えて得られる。
- 任意の 1 以上 N 以下の整数 i, j に対し、以下が成り立つ。
- 高橋君のおもちゃにおいてボール i, j がひもで繋がれていることと、青木君のおもちゃにおいてボール P_i, P_j がひもで繋がれていることは同値である。
2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力してください。
制約
- 1 \leq N \leq 8
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
- (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
- 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
- (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M C_1 D_1 \vdots C_M D_M
出力
2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力せよ。
入力例 1
4 4 1 2 1 3 1 4 3 4 1 3 1 4 2 3 3 4
出力例 1
Yes
高橋君のおもちゃは下図の左側のような形をしており、青木君のおもちゃは下図の右側のような形をしています。

次の図から、2 人のおもちゃが同じ形であることがわかります。例えば P = (3, 2, 1, 4) とすると問題文中の条件を満たします。

入力例 2
5 6 1 2 1 3 1 4 3 4 3 5 4 5 1 2 1 3 1 4 1 5 3 5 4 5
出力例 2
No
2 人のおもちゃは同じ形ではありません。

入力例 3
8 0
出力例 3
Yes
Score : 300 points
Problem Statement
Takahashi and Aoki each have a toy made by attaching M cords to N balls.
In Takahashi's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball A_i and B_i.
Similarly, in Aoki's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball C_i and D_i.
In each toy, no cord ties a ball to itself, and no two balls are tied by two or more different cords.
Snuke is wondering whether the two toys have the same shape.
Here, they are said to have the same shape when there is a sequence P that satisfies the conditions below.
- P is a permutation of (1, \dots, N).
- For every pair of integers i, j between 1 and N (inclusive), the following holds.
- Balls i and j in Takahashi's toy are tied by a cord if and only if Balls P_i and P_j in Aoki's toy are tied by a cord.
If the two toys have the same shape, print Yes; otherwise, print No.
Constraints
- 1 \leq N \leq 8
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
- (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
- 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
- (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M C_1 D_1 \vdots C_M D_M
Output
If the two toys have the same shape, print Yes; otherwise, print No.
Sample Input 1
4 4 1 2 1 3 1 4 3 4 1 3 1 4 2 3 3 4
Sample Output 1
Yes
Takahashi's toy is illustrated on the left in the figure below, and Aoki's is illustrated on the right.

The following figure shows that the two toys have the same shape. The condition in the statement is satisfied when P = (3, 2, 1, 4), for example.

Sample Input 2
5 6 1 2 1 3 1 4 3 4 3 5 4 5 1 2 1 3 1 4 1 5 3 5 4 5
Sample Output 2
No
The two toys do not have the same shape.

Sample Input 3
8 0
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 個の頂点と M 本の辺からなる単純な(すなわち、自己ループも多重辺も含まない)無向グラフ G が与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。
1 \leq u \lt v \leq N を満たす整数の組 (u, v) であって、下記の 2 つの条件をともに満たすものの個数を出力してください。
- グラフ G において、頂点 u と頂点 v を結ぶ辺は存在しない。
- グラフ G に、頂点 u と頂点 v を結ぶ辺を追加して得られるグラフは、二部グラフである。
二部グラフとは?
無向グラフが二部グラフであるとは、下記の条件を満たすように各頂点を黒または白のどちらかの色で塗ることができることを言います。
- 同じ色に塗られた頂点どうしを結ぶ辺は存在しない。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- グラフ G は単純
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
5 4 4 2 3 1 5 2 3 2
出力例 1
2
問題文中の条件を満たす整数の組 (u, v) は、(1, 4) と (1, 5) の 2 つです。よって、2 を出力します。
他の組については、例えば、(1, 3) はグラフ G において頂点 1 と頂点 3 を結ぶ辺が存在することから、
(4, 5) はグラフ G に頂点 4 と頂点 5 を結ぶ辺を追加して得られるグラフが二部グラフではないことから、
それぞれ問題文中の条件を満たしません。
入力例 2
4 3 3 1 3 2 1 2
出力例 2
0
与えられるグラフが二部グラフであったり連結であるとは限らないことに注意してください。
入力例 3
9 11 4 9 9 1 8 2 8 3 9 2 8 4 6 7 4 6 7 5 4 5 7 8
出力例 3
9
Score : 400 points
Problem Statement
You are given a simple undirected graph G with N vertices and M edges (a simple graph does not contain self-loops or multi-edges). For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i.
Print the number of pairs of integers (u, v) that satisfy 1 \leq u \lt v \leq N and both of the following conditions.
- The graph G does not have an edge connecting vertex u and vertex v.
- Adding an edge connecting vertex u and vertex v in the graph G results in a bipartite graph.
What is a bipartite graph?
An undirected graph is said to be bipartite if and only if one can paint each vertex black or white to satisfy the following condition.
- No edge connects vertices painted in the same color.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- The graph G is simple.
- All values in the input are integers.
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
Output
Print the answer.
Sample Input 1
5 4 4 2 3 1 5 2 3 2
Sample Output 1
2
We have two pairs of integers (u, v) that satisfy the conditions in the problem statement: (1, 4) and (1, 5). Thus, you should print 2.
The other pairs do not satisfy the conditions. For instance, for (1, 3), the graph G has an edge connecting vertex 1 and vertex 3; for (4, 5), adding an edge connecting vertex 4 and vertex 5 in the graph G does not result in a bipartite graph.
Sample Input 2
4 3 3 1 3 2 1 2
Sample Output 2
0
Note that the given graph may not be bipartite or connected.
Sample Input 3
9 11 4 9 9 1 8 2 8 3 9 2 8 4 6 7 4 6 7 5 4 5 7 8
Sample Output 3
9