実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の座席が横一列に並んでいます。各座席には最大で 1 人まで座ることができます。以下の条件を満たすように M 人の人を座席に座らせることができるかどうか判定してください。
隣り合う 2 つの席の両方に人が座ってはいけない。
制約
- 1 \le N, M \le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たすように座席に座らせることが可能ならば Yes を、不可能ならば No を出力せよ。
入力例 1
6 3
出力例 1
Yes
例えば、左から 1, 3, 6 番目に座らせれば可能です。したがって Yes を出力してください。
入力例 2
4 3
出力例 2
No
条件を満たすような座らせ方はありません。したがって No を出力してください。
入力例 3
5 3
出力例 3
Yes
入力例 4
44 7
出力例 4
Yes
Score : 100 points
Problem Statement
There are N seats arranged in a row. Each seat can accommodate at most one person. Determine whether it is possible to seat M people in the seats satisfying the following condition.
No two adjacent seats may both be occupied.
Constraints
- 1 \le N, M \le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Output Yes if it is possible to seat the people satisfying the condition, and No otherwise.
Sample Input 1
6 3
Sample Output 1
Yes
For example, seating people in the first, third, and sixth seats from the left is one valid way. Thus, output Yes.
Sample Input 2
4 3
Sample Output 2
No
There is no valid way to seat the people satisfying the condition. Thus, output No.
Sample Input 3
5 3
Sample Output 3
Yes
Sample Input 4
44 7
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が与えられます。 S の中で出現回数が最も多い文字をすべて取り除き、残った文字を元の順序を保ったまま連結して出力してください。
なお、出現回数が最大の文字が複数種類ある場合は、そのすべてを取り除いてください。
制約
- 1 \le |S| \le 100
- S は英小文字からなる文字列である
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
mississippi
出力例 1
mpp
mississippi に最も多く出現する文字は s と i でともに 4 回出現します。s と i をすべて取り除いた文字列は mpp となります。
入力例 2
atcoder
出力例 2
入力例 3
beginner
出力例 3
bgir
Score : 200 points
Problem Statement
You are given a string S consisting of lowercase English letters. Remove all occurrences of the most frequent character(s) in S, then output the remaining characters concatenated in their original order.
If there are multiple characters with the maximum frequency, remove all of them.
Constraints
- 1 \le |S| \le 100
- S is a string consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
mississippi
Sample Output 1
mpp
The most frequent characters in mississippi are s and i, each appearing four times. Removing all occurrences of s and i yields the string mpp.
Sample Input 2
atcoder
Sample Output 2
Sample Input 3
beginner
Sample Output 3
bgir
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英大文字からなる文字列 S,T が与えられます。
あなたは以下の 2 種類の操作を好きな順序で好きな回数(0 回でも良い)行うことができます。
- S の好きな位置(先頭および末尾を含む)に文字
Aを 1 つ挿入する。 - S に含まれる文字
Aを 1 つ選んで削除する。なお、残った文字は元の順序を保ったまま連結される。
S を T に一致させることが可能かどうか判定し、可能な場合は必要な操作回数の合計の最小値を求めてください。
制約
- S,T は英大文字からなる長さ 1 以上 3\times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S を T に一致させることが可能ならば必要な操作回数の合計の最小値を、不可能ならば -1 を出力せよ。
入力例 1
ABC BACA
出力例 1
3
以下のように、合計 3 回の操作で S を T に一致させることが可能です。
- S の 2 文字目と 3 文字目の間に
Aを 1 つ挿入する。S=ABACとなる。 - S の 1 文字目にある
Aを削除する。S=BACとなる。 - S の末尾に
Aを 1 つ挿入する。S=BACAとなる。
合計 2 回以下の操作で S を T に一致させることはできないため、答えは 3 です。
入力例 2
ABC ARC
出力例 2
-1
どのように操作を行っても、S を T に一致させることはできません。
入力例 3
ABC ABC
出力例 3
0
1 回も操作を行う必要がありません。
入力例 4
AAAWAZAAAABAAAU AWAAZABAAAAAUA
出力例 4
9
Score : 300 points
Problem Statement
You are given strings S and T consisting of uppercase English letters.
You may perform the following two types of operations any number of times (possibly zero) in any order:
- Insert one character
Aat any position in S (possibly the beginning or the end). - Choose one character
Ain S and delete it. The remaining characters are concatenated in their original order.
Determine whether it is possible to make S equal to T, and if so, find the minimum total number of operations required.
Constraints
- S and T are strings of uppercase English letters with length between 1 and 3\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S T
Output
If it is possible to make S equal to T, output the minimum total number of operations required; otherwise, output -1.
Sample Input 1
ABC BACA
Sample Output 1
3
It is possible to make S equal to T in three operations in total, as follows:
- Insert one
Abetween the second and third characters of S. Now, S=ABAC. - Delete the first character of S, which is
A. Now, S=BAC. - Insert one
Aat the end of S. Now, S=BACA.
It is impossible to make S equal to T in two or fewer operations, so the answer is 3.
Sample Input 2
ABC ARC
Sample Output 2
-1
No matter how operations are performed, it is impossible to make S equal to T.
Sample Input 3
ABC ABC
Sample Output 3
0
No operations need to be performed.
Sample Input 4
AAAWAZAAAABAAAU AWAAZABAAAAAUA
Sample Output 4
9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
A , B , C の 3 種類の文字のみからなる文字列 S が与えられます。
操作を以下のように定義します。
1 \le i \lt j \lt k \le |S| かつ S_i =
A, S_j =B, S_k =Cを満たす (i, j, k) の組を選び、S の i, j, k 文字目を取り除く。残った文字を元の順序を保ったまま左に詰める。
文字列 S に対して最大で何回操作を行うことができるかを求めてください。
制約
- 1 \le |S| \le 10 ^{6}
- S は
A,B,Cのみからなる文字列である
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ABACBCC
出力例 1
2
以下のように操作をすることで 2 回操作できます。
-
ABACBCCに対して (i, j, k) = (1, 2, 7) として操作する。残った文字列はACBCとなる。 -
ACBCに対して (i, j, k) = (1, 3, 4) として操作する。残った文字列はCとなる。
3 回以上操作することはできないため、答えは 2 となります。よって 2 と出力してください。
入力例 2
CBACBB
出力例 2
0
入力例 3
BBBAAABCBCBAACBBCAAC
出力例 3
5
Score : 400 points
Problem Statement
You are given a string S consisting of the three characters A, B, and C.
Define an operation as follows:
Choose a tuple (i, j, k) satisfying 1 \le i \lt j \lt k \le |S|, S_i =
A, S_j =B, and S_k =C, and remove the i-th, j-th, and k-th characters from S. The remaining characters are packed to the left in their original order.
Find the maximum number of times the operation can be performed on string S.
Constraints
- 1 \le |S| \le 10^{6}
- S is a string consisting of
A,B, andC.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
ABACBCC
Sample Output 1
2
The operation can be performed twice as follows:
-
For
ABACBCC, perform the operation with (i, j, k) = (1, 2, 7). The remaining string isACBC. -
For
ACBC, perform the operation with (i, j, k) = (1, 3, 4). The remaining string isC.
The operation cannot be performed three or more times, so the answer is 2. Thus, output 2.
Sample Input 2
CBACBB
Sample Output 2
0
Sample Input 3
BBBAAABCBCBAACBBCAAC
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 頂点 M 辺からなる単純連結無向グラフ G が与えられます。 ここで、N\geq 2 です。 頂点には 1 から N までの、辺には 1 から M までの番号がそれぞれ付けられており、辺 i は頂点 U_i と頂点 V_i を結んでいます。 また、各辺には コスト とよばれる値が定められており、辺 i のコストは 2^i です。
あなたは今から、G の連結成分の個数がちょうど 2 になるように、G の辺のうちいくつかを選んで削除します。 (なお、本問題の制約下でこれは必ず達成可能であることが証明できます。)
削除する辺のコストの和としてありうる最小値を 998244353 で割った余りを求めてください。 (998244353 で割った余りを最小化するのではないことに注意してください。)
制約
- 2\leq N \leq 2\times 10^5
- N-1\leq M \leq \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)
- 1\leq U_i < V_i \leq N
- i\neq j ならば (U_i, V_i) \neq (U_j, V_j)
- G は連結
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
削除する辺のコストの和としてありうる最小値を 998244353 で割った余りを出力せよ。
入力例 1
5 7 2 3 1 2 1 5 4 5 2 4 3 5 1 3
出力例 1
22

辺 1,2,4 の 3 本の辺(上図において点線で示されている辺)を削除すると、G の連結成分の個数はちょうど 2 になります。
このとき、削除する辺のコストの和は 2^1+2^2+2^4=22 であり、これが最小です。
入力例 2
2 1 1 2
出力例 2
2
入力例 3
8 16 2 7 5 7 6 8 1 7 4 7 1 3 2 8 5 8 4 8 2 5 3 4 3 8 1 4 1 8 4 6 1 2
出力例 3
54
Score : 475 points
Problem Statement
You are given a simple connected undirected graph G with N vertices and M edges. Here, N \geq 2. The vertices are numbered 1 to N, and the edges are numbered 1 to M; edge i connects vertices U_i and V_i. Each edge has a value called a cost, and the cost of edge i is 2^i.
You will now choose some edges of G to delete so that the number of connected components of G becomes exactly 2. (It can be proved that this is always achievable under the constraints of this problem.)
Find the minimum possible sum of costs of the deleted edges, modulo 998244353. (Note that you are not minimizing the remainder modulo 998244353.)
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)
- 1 \leq U_i < V_i \leq N
- (U_i, V_i) \neq (U_j, V_j) if i \neq j
- G is connected.
- All input values 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
Output the minimum possible sum of costs of the deleted edges, modulo 998244353.
Sample Input 1
5 7 2 3 1 2 1 5 4 5 2 4 3 5 1 3
Sample Output 1
22

Deleting edges 1, 2, 4 (the edges shown as dashed lines in the figure above) makes the number of connected components of G exactly 2.
In this case, the sum of costs of the deleted edges is 2^1+2^2+2^4=22, which is the minimum.
Sample Input 2
2 1 1 2
Sample Output 2
2
Sample Input 3
8 16 2 7 5 7 6 8 1 7 4 7 1 3 2 8 5 8 4 8 2 5 3 4 3 8 1 4 1 8 4 6 1 2
Sample Output 3
54
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N の番号がついた N 頂点の木 T が与えられます。i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。
正整数 k について「長さ k のムカデグラフ」を以下の手順で得られる頂点数 3k のグラフと定義します。
- 頂点数 k のパスグラフを用意する。
- そのパスグラフの各頂点 v に対して、v のみに隣接する 2 つの頂点を新たに追加する。
例えば、長さ 4 のムカデグラフは下図のようになります。

木 T の部分グラフとして含まれる「長さ x のムカデグラフ」のうち、最大の x を求めて下さい。
Q 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \le Q
- 3 \le N \le 2 \times 10^5
- 1 \le A_i, B_i \le N
- 与えられるグラフは木
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{case}_1
\text{case}_2
\vdots
\text{case}_Q
各テストケースは以下の形式で与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
出力
Q 行出力せよ。
i 行目には i 番目のテストケースについて、答えを出力せよ。
入力例 1
3 8 1 3 2 4 2 5 2 6 3 5 4 7 5 8 5 1 5 1 2 1 3 3 4 15 13 3 4 13 11 1 14 9 9 13 13 6 1 10 7 9 13 2 10 5 3 12 15 13 9 10 12 8
出力例 1
2 1 3
1 番目のテストケースについて、頂点 2,3,4,5,6,8 の 6 頂点からなるグラフは「長さ 2 のムカデグラフ」となっています。
長さ 3 以上のムカデグラフは部分グラフに含まれないため、答えは 2 です。よって 2 と出力してください。
Score : 500 points
Problem Statement
You are given a tree T with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.
For a positive integer k, define a "centipede graph of length k" as a graph with 3k vertices obtained by the following procedure:
- Prepare a path graph with k vertices.
- For each vertex v of the path graph, add two new vertices adjacent only to v.
For example, a centipede graph of length 4 is as shown in the figure below.

Find the maximum x such that a "centipede graph of length x" is contained as a subgraph of tree T.
Q test cases are given; solve each of them.
Constraints
- 1 \le Q
- 3 \le N \le 2 \times 10^5
- 1 \le A_i, B_i \le N
- The given graph is a tree.
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{case}_1
\text{case}_2
\vdots
\text{case}_Q
Each test case is given in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
Output
Output Q lines.
The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 8 1 3 2 4 2 5 2 6 3 5 4 7 5 8 5 1 5 1 2 1 3 3 4 15 13 3 4 13 11 1 14 9 9 13 13 6 1 10 7 9 13 2 10 5 3 12 15 13 9 10 12 8
Sample Output 1
2 1 3
For the first test case, the graph consisting of the six vertices 2,3,4,5,6,8 forms a "centipede graph of length 2".
No centipede graph of length 3 or more is contained as a subgraph, so the answer is 2. Thus, output 2.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
すぬけくんは、主催するプログラミングコンテストに用いる問題の選定をしています。 すぬけくんは 1 から N までの番号が付けられた N 個の問題案を持っており、問題案 i の 難易度 は i、ジャンル は K_i、面白さ は A_i です。
より幅広い実力の参加者に楽しんでもらいたいすぬけくんは、Div. 1、Div. 2 と呼ばれる 2 つのコンテストの同時開催を企画しており、各コンテストでは ジャンルの相異なる 4 つの問題を N 個の問題案の中から選んで出題します。 ただし、問題案の節約のため、Div. 1 の難易度下位 2 問と Div. 2 の難易度上位 2 問には共通のものを使い、合計で 6 問のみを用いる予定です。
より形式的には以下の通りです。
- すぬけくんは、N 個の問題案の中から 6 つの問題案を選ぶ。選ぶ問題案の番号を昇順に i_1,i_2,\dots,i_6 とする。
- 問題案 i_1,i_2,i_3,i_4 が Div. 2 に用いられ、これらは相異なるジャンルのものでなければならない。すなわち、K_{i_1},K_{i_2},K_{i_3},K_{i_4} は相異なる必要がある。
- 問題案 i_3,i_4,i_5,i_6 が Div. 1 に用いられ、これらは相異なるジャンルのものでなければならない。すなわち、K_{i_3},K_{i_4},K_{i_5},K_{i_6} は相異なる必要がある。
条件を満たすような 6 問の選び方が存在するか判定し、存在する場合は選ぶ 6 問の面白さの総和の最大値を求めてください。
制約
- 6\leq N \leq 10^5
- 1\leq K_i \leq N
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K_1 A_1 K_2 A_2 \vdots K_N A_N
出力
条件を満たすような 6 問の選び方が存在するならば選ぶ 6 問の面白さの総和の最大値を、存在しないならば -1 を出力せよ。
入力例 1
8 1 9 3 4 2 3 1 5 4 6 3 3 4 1 5 7
出力例 1
32
例として、問題案 1,2,3,5,6,8 を選ぶことを考えます。 このとき、K_{i_1}=1,K_{i_2}=3,K_{i_3}=2,K_{i_4}=4 は相異なり、また K_{i_3}=2,K_{i_4}=4,K_{i_5}=3,K_{i_6}=5 も相異なるため、これは条件を満たす選び方です。 また、このときの面白さの総和は 9+4+3+6+3+7=32 であり、これが最大です。
入力例 2
7 1 1 2 1 1 1 3 1 4 1 2 1 3 1
出力例 2
-1
条件を満たすような 6 問の選び方は存在しません。
入力例 3
16 1 593 3 449 15 991 9 310 1 355 15 68 3 431 15 580 14 757 14 218 14 934 9 328 3 676 3 355 1 221 6 80
出力例 3
3971
Score : 625 points
Problem Statement
Snuke is choosing problems to use in programming contests he is hosting. Snuke has N problem candidates numbered 1 to N, where problem candidate i has difficulty i, genre K_i, and interest A_i.
Hoping to entertain participants of a wider range of skill levels, he plans to simultaneously hold two contests called Div. 1 and Div. 2, each featuring four problems chosen from the N problem candidates with pairwise distinct genres. However, to conserve problem candidates, the two easier problems in Div. 1 and the two harder problems in Div. 2 will be the same, so only six problems in total will be used.
More formally, the following holds:
- Snuke chooses six problem candidates from the N candidates. Let the numbers of the chosen problem candidates in ascending order be i_1, i_2, \dots, i_6.
- Problem candidates i_1, i_2, i_3, i_4 are used in Div. 2, and they must be of pairwise distinct genres. That is, K_{i_1}, K_{i_2}, K_{i_3}, K_{i_4} must be pairwise distinct.
- Problem candidates i_3, i_4, i_5, i_6 are used in Div. 1, and they must be of pairwise distinct genres. That is, K_{i_3}, K_{i_4}, K_{i_5}, K_{i_6} must be pairwise distinct.
Determine whether there exists a valid choice of six problems satisfying the conditions, and if so, find the maximum total interest of the six chosen problems.
Constraints
- 6 \leq N \leq 10^5
- 1 \leq K_i \leq N
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K_1 A_1 K_2 A_2 \vdots K_N A_N
Output
If there exists a valid choice of six problems satisfying the conditions, output the maximum total interest of the six chosen problems; otherwise, output -1.
Sample Input 1
8 1 9 3 4 2 3 1 5 4 6 3 3 4 1 5 7
Sample Output 1
32
For example, consider choosing problem candidates 1, 2, 3, 5, 6, 8. In this case, K_{i_1}=1, K_{i_2}=3, K_{i_3}=2, K_{i_4}=4 are pairwise distinct, and K_{i_3}=2, K_{i_4}=4, K_{i_5}=3, K_{i_6}=5 are also pairwise distinct, so this is a valid choice. The total interest in this case is 9+4+3+6+3+7=32, which is the maximum.
Sample Input 2
7 1 1 2 1 1 1 3 1 4 1 2 1 3 1
Sample Output 2
-1
There is no valid choice of six problems satisfying the conditions.
Sample Input 3
16 1 593 3 449 15 991 9 310 1 355 15 68 3 431 15 580 14 757 14 218 14 934 9 328 3 676 3 355 1 221 6 80
Sample Output 3
3971