実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S が与えられます。
S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。
ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。
制約
- 1 \leq \lvert S \rvert \leq 10^6
- S は英小文字のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。
入力例 1
kasaka
出力例 1
Yes
kasaka の先頭に a を 1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。
入力例 2
atcoder
出力例 2
No
atcoder の先頭に a をいくつ付け加えても回文となる事はありません。
入力例 3
php
出力例 3
Yes
php はそれ自体回文です。S の先頭に付け加える a は 0 個でも許されるため、Yes を出力します。
Score : 300 points
Problem Statement
Given is a string S consisting of lowercase English letters.
Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.
Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.
Constraints
- 1 \leq \lvert S \rvert \leq 10^6
- S consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.
Sample Input 1
kasaka
Sample Output 1
Yes
By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.
Sample Input 2
atcoder
Sample Output 2
No
Adding any number of a's at the beginning of atcoder does not make it a palindrome.
Sample Input 3
php
Sample Output 3
Yes
php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
文字列 S に対して操作を Q 回行います。 i 回目 (1\leq i\leq Q) の操作は文字の組 (c _ i,d _ i) で表され、次のような操作に対応します。
- S に含まれる文字 c _ i をすべて文字 d _ i で置き換える。
すべての操作が終わったあとの文字列 S を出力してください。
制約
- 1\leq N\leq2\times10^5
- S は英小文字からなる長さ N の文字列
- 1\leq Q\leq2\times10^5
- c _ i,d _ i は英小文字 (1\leq i\leq Q)
- N,Q は整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
出力
すべての操作が終わったあとの S を出力せよ。
入力例 1
7 atcoder 4 r a t e d v a r
出力例 1
recover
S は atcoder → atcodea → aecodea → aecovea → recover と変化します。
たとえば、4 番目の操作では S={}aecovea に含まれる a (1 文字目と 7 文字目)をすべて r に置き換えるので S={}recover となります。
すべての操作が終わったときには S={}recover となっているため、recover を出力してください。
入力例 2
3 abc 4 a a s k n n z b
出力例 2
abc
c _ i=d _ i であるような操作や S に c _ i が含まれないような操作もあります。
入力例 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
出力例 3
laklimamriiamrmrllrmlrkramrjimrial
Score: 350 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
You will perform an operation Q times on the string S. The i-th operation (1\leq i\leq Q) is represented by a pair of characters (c _ i,d _ i), which corresponds to the following operation:
- Replace all occurrences of the character c _ i in S with the character d _ i.
Print the string S after all operations are completed.
Constraints
- 1\leq N\leq2\times10^5
- S is a string of length N consisting of lowercase English letters.
- 1\leq Q\leq2\times10^5
- c _ i and d _ i are lowercase English letters (1\leq i\leq Q).
- N and Q are integers.
Input
The input is given from Standard Input in the following format:
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
Output
Print the string S after all operations are completed.
Sample Input 1
7 atcoder 4 r a t e d v a r
Sample Output 1
recover
S changes as follows: atcoder → atcodea → aecodea → aecovea → recover.
For example, in the fourth operation, all occurrences of a in S={}aecovea (the first and seventh characters) are replaced with r, resulting in S={}recover.
After all operations are completed, S={}recover, so print recover.
Sample Input 2
3 abc 4 a a s k n n z b
Sample Output 2
abc
There may be operations where c _ i=d _ i or S does not contain c _ i.
Sample Input 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
Sample Output 3
laklimamriiamrmrllrmlrkramrjimrial
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
1 から N が書かれた N 枚のカードが裏向きで積まれた山札があり、上から i 枚目のカードには整数 P_i が書かれています。
この山札を使って、以下の行動を N ターン繰り返します。
- 山札の一番上のカードを引いて、そこに書かれた整数を X とする。
- 場に見えている表向きのカードであって書かれた整数が X 以上であるもののうち、書かれた整数が最小のものの上に、引いたカードを表向きで重ねる。もし場にそのようなカードがなければ、引いたカードをどれにも重ねずに表向きで場に置く。
- その後、表向きのカードが K 枚重ねられた山が場にあればその山のカードを全て食べる。食べられたカードは全て場から消滅する。
各カードについて、何ターン目に食べられるか、あるいは最後まで食べられないかを求めてください。
制約
- 入力は全て整数
- 1 \le K \le N \le 2 \times 10^5
- P は (1,2,\dots,N) の順列 ( (1,2,\dots,N) を並べ替えて得られる列 ) である
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
N 行出力せよ。
そのうち i (1 \le i \le N) 行目には、整数 i が書かれたカードについて、以下のように出力せよ。
- もし i が書かれたカードが食べられるなら、それが何ターン目であるかを整数として出力せよ。
- そうでないなら -1 と出力せよ。
入力例 1
5 2 3 5 2 1 4
出力例 1
4 3 3 -1 4
この入力では、 P=(3,5,2,1,4),K=2 です。
- 1 ターン目に、 3 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 2 ターン目に、 5 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 3 ターン目に、 2 が書かれたカードが 3 が書かれたカードの上に表向きで重ねられます。
- この時点で上から 2,3 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
- 4 ターン目に、 1 が書かれたカードが 5 が書かれたカードの上に表向きで重ねられます。
- この時点で上から 1,5 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
- 5 ターン目に、 4 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
- 4 が書かれたカードは、最後まで食べられませんでした。
入力例 2
5 1 1 2 3 4 5
出力例 2
1 2 3 4 5
K=1 である場合、全てのカードは場に置かれたターンに食べられます。
入力例 3
15 3 3 14 15 9 2 6 5 13 1 7 10 11 8 12 4
出力例 3
9 9 9 15 15 6 -1 -1 6 -1 -1 -1 -1 6 15
Score : 400 points
Problem Statement
There is a deck consisting of N face-down cards with an integer from 1 through N written on them. The integer on the i-th card from the top is P_i.
Using this deck, you will perform N moves, each consisting of the following steps:
- Draw the topmost card from the deck. Let X be the integer written on it.
- Stack the drawn card, face up, onto the card with the smallest integer among the face-up topmost cards on the table with an integer greater than or equal to X written on them. If there is no such card on the table, put the drawn card on the table, face up, without stacking it onto any card.
- Then, if there is a pile consisting of K face-up cards on the table, eat all those cards. The eaten cards all disappear from the table.
For each card, find which of the N moves eats it. If the card is not eaten until the end, report that fact.
Constraints
- All values in input are integers.
- 1 \le K \le N \le 2 \times 10^5
- P is a permutation of (1,2,\dots,N) (i.e. a sequence obtained by rearranging (1,2,\dots,N)).
Input
Input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print N lines.
The i-th line (1 \le i \le N) should describe the card with the integer i written on it. Specifically,
- if the card with i written on it is eaten in the x-th move, print x;
- if that card is not eaten in any move, print -1.
Sample Input 1
5 2 3 5 2 1 4
Sample Output 1
4 3 3 -1 4
In this input, P=(3,5,2,1,4) and K=2.
- In the 1-st move, the card with 3 written on it is put on the table, face up, without stacked onto any card.
- In the 2-nd move, the card with 5 written on it is put on the table, face up, without stacked onto any card.
- In the 3-rd move, the card with 2 written on it is stacked, face up, onto the card with 3 written on it.
- Now there is a pile consisting of K=2 face-up cards, on which 2 and 3 from the top are written, so these cards are eaten.
- In the 4-th move, the card with 1 written on it is stacked, face up, onto the card with 5 written on it.
- Now there is a pile consisting of K=2 face-up cards, on which 1 and 5 from the top are written, so these cards are eaten.
- In the 5-th move, the card with 4 written on it is put on the table, face up, without stacked onto any card.
- The card with 4 written on it was not eaten until the end.
Sample Input 2
5 1 1 2 3 4 5
Sample Output 2
1 2 3 4 5
If K=1, every card is eaten immediately after put on the table within a single move.
Sample Input 3
15 3 3 14 15 9 2 6 5 13 1 7 10 11 8 12 4
Sample Output 3
9 9 9 15 15 6 -1 -1 6 -1 -1 -1 -1 6 15
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
最初 N 頂点 0 辺の無向グラフがあり、各頂点には 1 から N まで番号がついています。
Q 個のクエリが与えられるので、順に処理し、各クエリの後における「他のどの頂点とも辺で結ばれていない頂点」の数を出力してください。
i 個目のクエリは \mathrm{query}_i であり、各クエリは次の 2 種類いずれかです。
-
1 u v: 頂点 u と頂点 v を辺で結ぶ。このクエリが与えられる直前の時点で、頂点 u と頂点 v は辺で結ばれていない事が保証される。 -
2 v: 頂点 v と他の頂点を結ぶ辺をすべて削除する。(頂点 v 自体は削除しない。)
制約
- 2 \leq N\leq 3\times 10^5
- 1 \leq Q\leq 3\times 10^5
- 1 番目の種類のクエリにおいて、1\leq u,v\leq N, u\neq v
- 2 番目の種類のクエリにおいて、1\leq v\leq N
- 1 番目の種類のクエリの直前の時点で、そのクエリの u,v について頂点 u と頂点 v は辺で結ばれていない。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
出力
Q 行出力せよ。
i 行目 (1\leq i\leq Q) には、i 個目のクエリを処理した後の「他のどの頂点とも辺で結ばれていない頂点」の数を出力せよ。
入力例 1
3 7 1 1 2 1 1 3 1 2 3 2 1 1 1 2 2 2 1 1 2
出力例 1
1 0 0 1 0 3 1
1 個目のクエリの後で、頂点 1 と頂点 2 は互いに結ばれており、頂点 3 のみが他のどの頂点とも辺で結ばれていません。
よって、1 行目には 1 を出力します。
また、3 個目のクエリの後でどの相異なる 2 頂点の間も辺で結ばれていますが、4 個目のクエリによって、
頂点 1 と他の頂点を結ぶ辺、すなわち 頂点 1 と頂点 2 を結ぶ辺および頂点 1 と頂点 3 を結ぶ辺が削除されます。
この結果として、頂点 2 と頂点 3 は互いに結ばれているが、頂点 1 は他のどの頂点とも辺で結ばれていない状態となります。
よって、3 行目には 0 を、4 行目には 1 を出力します。
入力例 2
2 1 2 1
出力例 2
2
2 番目の種類のクエリを行う直前の時点で、すでにその頂点と他の頂点を結ぶ辺が 1 本も存在しないこともあります。
Score : 425 points
Problem Statement
There is an undirected graph with N vertices numbered 1 through N, and initially with 0 edges.
Given Q queries, process them in order. After processing each query,
print the number of vertices that are not connected to any other vertices by an edge.
The i-th query, \mathrm{query}_i, is of one of the following two kinds.
-
1 u v: connect vertex u and vertex v with an edge. It is guaranteed that, when this query is given, vertex u and vertex v are not connected by an edge. -
2 v: remove all edges that connect vertex v and the other vertices. (Vertex v itself is not removed.)
Constraints
- 2 \leq N\leq 3\times 10^5
- 1 \leq Q\leq 3\times 10^5
- For each query of the first kind, 1\leq u,v\leq N and u\neq v.
- For each query of the second kind, 1\leq v\leq N.
- Right before a query of the first kind is given, there is no edge between vertices u and v.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Output
Print Q lines.
The i-th line (1\leq i\leq Q) should contain the number of vertices that are not connected to any other vertices by an edge.
Sample Input 1
3 7 1 1 2 1 1 3 1 2 3 2 1 1 1 2 2 2 1 1 2
Sample Output 1
1 0 0 1 0 3 1
After the first query, vertex 1 and vertex 2 are connected to each other by an edge, but vertex 3 is not connected to any other vertices.
Thus, 1 should be printed in the first line.
After the third query, all pairs of different vertices are connected by an edge.
However, the fourth query asks to remove all edges that connect vertex 1 and the other vertices, specifically to remove the edge between vertex 1 and vertex 2, and another between vertex 1 and vertex 3.
As a result, vertex 2 and vertex 3 are connected to each other, while vertex 1 is not connected to any other vertices by an edge.
Thus, 0 and 1 should be printed in the third and fourth lines, respectively.
Sample Input 2
2 1 2 1
Sample Output 2
2
When the query of the second kind is given, there may be no edge that connects that vertex and the other vertices.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
数直線上に 1 体のタコ型ロボットと N 個の宝があります。
i (1\leq i\leq N) 個目の宝はそれぞれ座標 X_i にあります。
タコ型ロボットは 1 つの頭と N 本の足を持っており、i 本目の足の長さは L_i (1\leq i\leq N) です。
タコ型ロボットが次のようにして N 個の宝すべてを掴む事ができるような整数 k の個数を求めてください。
- 頭を座標 k におく。
- i=1,2,\ldots,N の順に、「頭から距離 L_i 以下の範囲、すなわち k-L_i\leq x\leq k+L_i をみたす座標 x にまだ掴んでいない宝が存在する場合、そのうちの 1 つを選んで掴む」ことを繰り返す。
制約
- 1 \leq N\leq 200
- -10^{18} \leq X_1<X_2<\cdots<X_N\leq 10^{18}
- 1\leq L_1\leq L_2\leq\cdots\leq L_N\leq 10^{18}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 X_2 \ldots X_N L_1 L_2 \ldots L_N
出力
問題文の条件をみたす整数 k の個数を出力せよ。
入力例 1
3 -6 0 7 3 5 10
出力例 1
6
k=-3,-2,-1,2,3,4 が条件をみたします。例えば、k=-3 のときは、次のようにして 3 個の宝をすべて掴む事ができます。
- 1 本目の足は -6\leq x\leq 0 にある宝を掴む事ができる。このうち座標 -6 にある 1 個目の宝を掴む。
- 2 本目の足は -8\leq x\leq 2 にある宝を掴む事ができる。このうち座標 0 にある 2 個目の宝を掴む。
- 3 本目の足は -13\leq x\leq 7 にある宝を掴む事ができる。このうち座標 7 にある 3 個目の宝を掴む。
入力例 2
1 0 1000000000000000000
出力例 2
2000000000000000001
-10^{18} 以上 10^{18} 以下のすべての整数が k として条件をみたします。
入力例 3
2 -100 100 1 1
出力例 3
0
条件をみたす k は存在しません。
Score : 575 points
Problem Statement
There is an octopus-shaped robot and N treasures on a number line.
The i-th treasure (1\leq i\leq N) is located at coordinate X_i.
The robot has one head and N legs, and the i-th leg (1\leq i\leq N) has a length of L_i.
Find the number of integers k such that the robot can grab all N treasures as follows.
- Place the head at coordinate k.
- Repeat the following for i=1,2,\ldots,N in this order: if there is a treasure that has not been grabbed yet within a distance of L_i from the head, that is, at a coordinate x satisfying k-L_i\leq x\leq k+L_i, choose one such treasure and grab it.
Constraints
- 1 \leq N\leq 200
- -10^{18} \leq X_1<X_2<\cdots<X_N\leq 10^{18}
- 1\leq L_1\leq L_2\leq\cdots\leq L_N\leq 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \ldots X_N L_1 L_2 \ldots L_N
Output
Print the number of integers k that satisfy the condition in the statement.
Sample Input 1
3 -6 0 7 3 5 10
Sample Output 1
6
k=-3,-2,-1,2,3,4 satisfy the condition. For example, when k=-3, the robot can grab all three treasures as follows.
- The first leg can grab treasures at coordinates x satisfying -6\leq x\leq 0. Among them, grab the first treasure at coordinate -6.
- The second leg can grab treasures at coordinates x satisfying -8\leq x\leq 2. Among them, grab the second treasure at coordinate 0.
- The third leg can grab treasures at coordinates x satisfying -13\leq x\leq 7. Among them, grab the third treasure at coordinate 7.
Sample Input 2
1 0 1000000000000000000
Sample Output 2
2000000000000000001
All integers k from -10^{18} to 10^{18} satisfy the condition.
Sample Input 3
2 -100 100 1 1
Sample Output 3
0
No k satisfies the conditions.