実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
空の袋があります。 クエリが Q 個与えられるので、順番に処理してください。
クエリは次の 3 種類です。
1 x: 整数 x が書かれたボールを 1 つ袋に入れる。2 x: 整数 x が書かれたボールを 1 つ袋の中から取り出して外に捨てる。このクエリが与えられるとき、袋の中に整数 x が書かれたボールが存在することが保証される。3: 袋の中にあるボールに書かれている整数の種類数を出力する。
制約
- 1 \leq Q \leq 2 \times 10^{5}
- 1 \leq x \leq 10^{6}
- 2 種類目のクエリが与えられるとき、袋の中に整数 x が書かれたボールが存在する。
- 3 種類目のクエリが 1 つ以上存在する。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリ \text{query}_i は以下の 3 つの形式のいずれかで与えられる。
1 x
2 x
3
出力
3 種類目のクエリが K 個あるとき、K 行出力せよ。 i 行目(1 \leq i \leq K) では、i 番目の 3 種類目のクエリに対する答えを出力せよ。
入力例 1
8 1 3 1 1 1 4 3 2 1 3 1 5 3
出力例 1
3 2 3
はじめ、袋の中は空です。
1 番目のクエリ 1 3 で袋の中に 3 が書かれたボールが 1 つ入ります。
2 番目のクエリ 1 1 で袋の中に 1 が書かれたボールが 1 つ入ります。
3 番目のクエリ 1 4 で袋の中に 4 が書かれたボールが 1 つ入ります。
4 番目のクエリ 3 で袋の中に 1, 3, 4 の 3 種類のボールが入っているため、3 を出力します。
5 番目のクエリ 2 1 で袋の中から 1 が書かれたボールを 1 つ取り出します。
6 番目のクエリ 3 で袋の中に 3, 4 の 2 種類のボールが入っているため、2 を出力します。
7 番目のクエリ 1 5 で袋の中に 5 が書かれたボールが 1 つ入ります。
8 番目のクエリ 3 で袋の中に 3, 4, 5 の 3 種類のボールが入っているため、3 を出力します。
入力例 2
8 1 2 1 2 3 2 2 1 4 1 4 2 2 3
出力例 2
1 1
Score : 300 points
Problem Statement
You have an empty bag. You are given Q queries, which must be processed in order.
There are three types of queries.
1 x: Put one ball with the integer x written on it into the bag.2 x: Remove one ball with the integer x written on it from the bag and discard it. It is guaranteed that the bag has a ball with the integer x written on it when this query is given.3: Print the number of different integers written on the balls in the bag.
Constraints
- 1 \leq Q \leq 2 \times 10^{5}
- 1 \leq x \leq 10^{6}
- When a query of the second type is given, the bag has a ball with the integer x written on it.
- There is at least one query of the third type.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
The i-th query \text{query}_i is given in one of the following three formats:
1 x
2 x
3
Output
If there are K queries of the third type, print K lines. The i-th line (1 \leq i \leq K) should contain the answer to the i-th query of the third type.
Sample Input 1
8 1 3 1 1 1 4 3 2 1 3 1 5 3
Sample Output 1
3 2 3
Initially, the bag is empty.
For the first query 1 3, a ball with the integer 3 written on it enters the bag.
For the second query 1 1, a ball with the integer 1 written on it enters the bag.
For the third query 1 4, a ball with the integer 4 written on it enters the bag.
For the fourth query 3, the bag has balls with the integers 1, 3, 4, so print 3.
For the fifth query 2 1, a ball with the integer 1 written on it is removed from the bag.
For the sixth query 3, the bag has balls with the integers 3, 4, so print 2.
For the seventh query 1 5, a ball with the integer 5 written on it enters the bag.
For the eighth query 3, the bag has balls with the integers 3, 4, 5, so print 3.
Sample Input 2
8 1 2 1 2 3 2 2 1 4 1 4 2 2 3
Sample Output 2
1 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正の整数 L に対して、 レベル L のダンゴ文字列とは、以下の条件を満たす文字列です。
oと-からなる長さ L+1 の文字列である。- 先頭の文字と末尾の文字のうちちょうど一方が
-であり、そのほかの L 文字はすべてoである。
例えば、ooo- はレベル 3 のダンゴ文字列ですが、-ooo- や oo や o-oo- などはダンゴ文字列ではありません(より正確には、どのような正の整数 L に対してもレベル L のダンゴ文字列ではありません)。
2 種類の文字 o - からなる、長さ N の文字列 S が与えられます。
次の条件を満たすような正整数 X のうち、最大のものを求めてください。
- S の連続する部分文字列であって、レベル X のダンゴ文字列であるものが存在する。
ただし、そのような整数が存在しない場合、-1 と出力してください。
制約
- 1\leq N\leq 2\times10^5
- S は
o-からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S にレベル X のダンゴ文字列が含まれるような最大の X の値を 1 行で出力せよ。
そのような値が存在しない場合、-1 を出力せよ。
入力例 1
10 o-oooo---o
出力例 1
4
たとえば、S の 3 文字目から 7 文字目までに対応する部分文字列 oooo- は、レベル 4 のダンゴ文字列です。
S の部分文字列であってレベル 5 以上のダンゴ文字列であるようなものは存在しないため、4 と出力してください。
入力例 2
1 -
出力例 2
-1
S の連続する部分文字列は空文字列と - の 2 種類だけです。
これらはダンゴ文字列ではないため、-1 と出力してください。
入力例 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
出力例 3
7
Score : 300 points
Problem Statement
For a positive integer L, a level-L dango string is a string that satisfies the following conditions.
- It is a string of length L+1 consisting of
oand-. - Exactly one of the first character and the last character is
-, and the other L characters areo.
For instance, ooo- is a level-3 dango string, but none of -ooo-, oo, and o-oo- is a dango string (more precisely, none of them is a level-L dango string for any positive integer L).
You are given a string S of length N consisting of the two characters o and -.
Find the greatest positive integer X that satisfies the following condition.
- There is a contiguous substring of S that is a level-X dango string.
If there is no such integer, print -1.
Constraints
- 1\leq N\leq 2\times10^5
- S is a string of length N consisting of
oand-.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the greatest positive integer X such that S contains a level-X dango string, or -1 if there is no such integer.
Sample Input 1
10 o-oooo---o
Sample Output 1
4
For instance, the substring oooo- corresponding to the 3-rd through 7-th characters of S is a level-4 dango string.
No substring of S is a level-5 dango string or above, so you should print 4.
Sample Input 2
1 -
Sample Output 2
-1
Only the empty string and - are the substrings of S.
They are not dango strings, so you should print -1.
Sample Input 3
30 -o-o-oooo-oo-o-ooooooo--oooo-o
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
A, B, C のみからなる文字列 S が与えられます。
S^{(0)}:=S とし、i=1,2,3,\ldots について S^{(i)} を S^{(i-1)} の各文字を A → BC, B → CA, C → AB と同時に置き換えたものと定義します。
以下の Q 個のクエリに答えてください。i 個目のクエリの内容は以下の通りです。
- S^{(t_i)} の先頭から k_i 文字目を出力せよ。
制約
- S は
A,B,Cのみからなる長さ 1 以上 10^5 以下の文字列 - 1 \leq Q \leq 10^5
- 0 \leq t_i \leq 10^{18}
- 1 \leq k_i \leq \min(10^{18},\ S^{(t_i)} の長さ)
- Q, t_i, k_i は整数
入力
入力は以下の形式で標準入力から与えられる。
S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q
出力
Q 個のクエリを添字の昇順に、すなわち与えられる順に処理し、出力ごとに改行せよ。
入力例 1
ABC 4 0 1 1 1 1 3 1 6
出力例 1
A B C B
S^{(0)}=ABC, S^{(1)}=BCCAAB です。
よって各クエリへの答えは順に A, B, C, B となります。
入力例 2
CBBAACCCCC 5 57530144230160008 659279164847814847 29622990657296329 861239705300265164 509705228051901259 994708708957785197 176678501072691541 655134104344481648 827291290937314275 407121144297426665
出力例 2
A A C A A
Score : 400 points
Problem Statement
You are given a string S consisting of A, B, C.
Let S^{(0)}:=S. For i=1,2,3,\ldots, let S^{(i)} be the result of simultaneously replacing the characters of S^{(i-1)} as follows: A → BC, B → CA, C → AB.
Answer Q queries. The i-th query is as follows.
- Print the k_i-th character from the beginning of S^{(t_i)}.
Constraints
- S is a string of length between 1 and 10^5 (inclusive) consisting of
A,B,C. - 1 \leq Q \leq 10^5
- 0 \leq t_i \leq 10^{18}
- 1 \leq k_i \leq \min(10^{18}, the length of S^{(t_i)})
- Q, t_i, k_i are integers.
Input
Input is given from Standard Input in the following format:
S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q
Output
Process the Q queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.
Sample Input 1
ABC 4 0 1 1 1 1 3 1 6
Sample Output 1
A B C B
We have S^{(0)}=ABC, S^{(1)}=BCCAAB.
Thus, the answers to the queries are A, B, C, B in the given order.
Sample Input 2
CBBAACCCCC 5 57530144230160008 659279164847814847 29622990657296329 861239705300265164 509705228051901259 994708708957785197 176678501072691541 655134104344481648 827291290937314275 407121144297426665
Sample Output 2
A A C A A
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
Atcoder 国には 1 から N の番号がついた N 個の街と、1 から M の番号がついた M 本の電車が走っています。
電車 i は街 A_i を時刻 S_i に発車し、街 B_i に時刻 T_i に到着します。
正の整数 X_1 が与えられるので、0 以上の整数 X_2,\ldots,X_M を以下の条件を満たすように定める方法のうち、X_2+\ldots+X_M が最小になるものを求めてください。
- 条件:1 \leq i,j \leq M を満たす全ての組 (i,j) について、「B_i=A_j かつ T_i \leq S_j」ならば「T_i+X_i \leq S_j+X_j」
- すなわち、もともと乗り換え可能だった電車の組は、各電車 i の発車時刻・到着時刻を X_i 遅らせても乗り換え可能である
なお、X_2+\ldots+X_M が最小になるような X_2,\ldots,X_M の定め方は一意であることが証明できます。
制約
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i \neq B_i
- 0 \leq S_i < T_i \leq 10^9
- 1 \leq X_1 \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 A_1 B_1 S_1 T_1 \vdots A_M B_M S_M T_M
出力
条件を満たし、和を最小化するような X_2,\ldots,X_M を、この順に空白区切りで出力せよ。
入力例 1
3 6 15 1 2 10 20 1 2 20 30 2 3 25 40 2 3 35 50 3 1 15 30 3 1 45 60
出力例 1
0 10 0 0 5
街 1 から 2 へ行く電車 1 の到着が 15 遅れ、時刻 35 になりました。
街 2 での電車 1 から 3 への乗り換えのため、電車 3 の発車時刻を 10 遅らせて、時刻 35 発 時刻 50 着とします。
さらに街 3 での電車 3 から 6 への乗り換えのため、電車 6 の発車時刻を 5 遅らせて、時刻 50 発とします。
他の電車は発車を遅らせることなく、元々乗り換え可能だった電車の間を乗り換えることができるため、(X_2,X_3,X_4,X_5,X_6)=(0,10,0,0,5) は条件を満たします。
また、条件を満たすもののうち和がこれより小さいものは存在しないため、これが答えとなります。
入力例 2
10 9 100 1 10 0 1 10 2 1 100 10 3 1 100 10 4 1 100 10 5 1 100 10 6 1 100 10 7 1 100 10 8 1 100 10 9 1 100
出力例 2
100 100 100 100 100 100 100 100
入力例 3
4 4 10 1 2 0 1 1 2 0 10 2 3 100 200 2 4 100 200
出力例 3
0 0 0
Score : 475 points
Problem Statement
In the nation of Atcoder, there are N cities numbered 1 to N, and M trains numbered 1 to M. Train i departs from city A_i at time S_i and arrives at city B_i at time T_i.
Given a positive integer X_1, find a way to set non-negative integers X_2,\ldots,X_M that satisfies the following condition with the minimum possible value of X_2+\ldots+X_M.
- Condition: For all pairs (i,j) satisfying 1 \leq i,j \leq M, if B_i=A_j and T_i \leq S_j, then T_i+X_i \leq S_j+X_j.
- In other words, for any pair of trains that are originally possible to transfer between, it is still possible to transfer even after delaying the departure and arrival times of each train i by X_i.
It can be proved that such a way to set X_2,\ldots,X_M with the minimum possible value of X_2+\ldots+X_M is unique.
Constraints
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i \neq B_i
- 0 \leq S_i < T_i \leq 10^9
- 1 \leq X_1 \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 A_1 B_1 S_1 T_1 \vdots A_M B_M S_M T_M
Output
Print X_2,\ldots,X_M that satisfy the condition with the minimum possible sum, in that order, separated by spaces.
Sample Input 1
3 6 15 1 2 10 20 1 2 20 30 2 3 25 40 2 3 35 50 3 1 15 30 3 1 45 60
Sample Output 1
0 10 0 0 5
The arrival of train 1 from city 1 to 2 is delayed by 15 and becomes time 35.
To allow transfer from train 1 to 3 in city 2, the departure of train 3 is delayed by 10, making it depart at time 35 and arrive at time 50.
Further, to allow transfer from train 3 to 6 in city 3, the departure of train 6 is delayed by 5, making it depart at time 50.
Other trains can operate without delay while still allowing transfers between originally transferable trains, so (X_2,X_3,X_4,X_5,X_6)=(0,10,0,0,5) satisfies the condition.
Moreover, there is no solution with a smaller sum that satisfies the condition, so this is the answer.
Sample Input 2
10 9 100 1 10 0 1 10 2 1 100 10 3 1 100 10 4 1 100 10 5 1 100 10 6 1 100 10 7 1 100 10 8 1 100 10 9 1 100
Sample Output 2
100 100 100 100 100 100 100 100
Sample Input 3
4 4 10 1 2 0 1 1 2 0 10 2 3 100 200 2 4 100 200
Sample Output 3
0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
Atcoder 国には 1 から N の番号がついた N 個の街と 1 から M の番号がついた M 個の高速道路があります。
高速道路 i は街 A_i と街 B_i を双方向に結んでいます。
国王の高橋君は、新たに N-M-1 本の高速道路を建設し、次の 2 つの条件をともに満たそうとしています。
- すべての街同士は、高速道路をいくつか通って互いに行き来できる
- 各 i=1,\ldots,N について、街 i はちょうど D_i 本の高速道路と直接つながっている
条件を満たすような建設方法が存在するか判定し、存在するなら 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- i\neq j ならば、(A_i, B_i) \neq (A_j,B_j)
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
出力
条件を満たすような建設方法が存在しないとき -1 を出力せよ。
存在するとき、N-M-1 行出力せよ。i 行目には、i 番目に建設する高速道路が結ぶ 2 つの街の番号を空白区切りで出力せよ。
入力例 1
6 2 1 2 1 2 2 2 2 3 1 4
出力例 1
6 2 5 6 4 5
出力例のように、街 6 と2、街 5 と 6、街 4 と 5 をそれぞれ結ぶ高速道路を建設すると条件を満たすことができます。
この他にも、例えば 街 6 と4、街 5 と 6、街 2 と 5 を結ぶような高速道路を建設しても条件を満たすことができます。
入力例 2
5 1 1 1 1 1 4 2 3
出力例 2
-1
入力例 3
4 0 3 3 3 3
出力例 3
-1
Score : 500 points
Problem Statement
The Republic of Atcoder has N towns numbered 1 through N, and M highways numbered 1 through M.
Highway i connects Town A_i and Town B_i bidirectionally.
King Takahashi is going to construct (N-M-1) new highways so that the following two conditions are satisfied:
- One can travel between every pair of towns using some number of highways
- For each i=1,\ldots,N, exactly D_i highways are directly connected to Town i
Determine if there is such a way of construction. If it exists, print one.
Constraints
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- If i\neq j, then (A_i, B_i) \neq (A_j,B_j).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
Output
If there isn't a way of construction that satisfies the conditions, print -1.
If there is, print (N-M-1) lines. The i-th line should contain the indices of the two towns connected by the i-th highway to be constructed.
Sample Input 1
6 2 1 2 1 2 2 2 2 3 1 4
Sample Output 1
6 2 5 6 4 5
As in the Sample Output, the conditions can be satisfied by constructing highways connecting Towns 6 and 2, Towns 5 and 6, and Towns 4 and 5, respectively.
Another example to satisfy the conditions is to construct highways connecting Towns 6 and 4, Towns 5 and 6, and Towns 2 and 5, respectively.
Sample Input 2
5 1 1 1 1 1 4 2 3
Sample Output 2
-1
Sample Input 3
4 0 3 3 3 3
Sample Output 3
-1