Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
o, x からなる長さ N の文字列 S と整数 L,R が与えられます。
S の L 文字目から R 文字目までが全て o であるかどうか判定してください。
制約
- 1\leq N \leq 100
- 1\leq L \leq R \leq N
- S は
o,xのみからなる長さ N の文字列 - N,L,R は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R S
出力
S の L 文字目から R 文字目までが全て o なら Yes、そうでないなら No と出力せよ。
入力例 1
10 6 8 xoxxooooxo
出力例 1
Yes
S の 6 文字目から 8 文字目は全て o なので答えは Yes です。
入力例 2
9 6 8 xoxxoxoox
出力例 2
No
6 文字目が x なので答えは No です。
Score : 100 points
Problem Statement
You are given a string S of length N consisting of o and x, and integers L and R.
Determine whether all characters from the L-th through the R-th character of S are o.
Constraints
- 1\leq N \leq 100
- 1\leq L \leq R \leq N
- S is a string of length N consisting of
oandx. - N, L, and R are all integers.
Input
The input is given from Standard Input in the following format:
N L R S
Output
If all characters from the L-th through the R-th character of S are o, output Yes; otherwise, output No.
Sample Input 1
10 6 8 xoxxooooxo
Sample Output 1
Yes
All characters from the 6-th through the 8-th character of S are o, so the answer is Yes.
Sample Input 2
9 6 8 xoxxoxoox
Sample Output 2
No
The 6-th character is x, so the answer is No.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
. および # からなる文字列 S が与えられます。
以下の条件を全て満たす文字列 T のうち、 o の文字数が最大となるものを一つ求めてください。
- T の長さは S の長さと等しい。
- T は
.、#、oからなる。 - S_i=
#であるとき、またそのときに限り T_i=#である。 - T_i=T_j=
o(i < j) ならば、 T_{i+1},\ldots,T_{j-1} の中に#が 1 つ以上存在する。
制約
- S は
.および#からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
条件を全て満たす文字列 T のうち、 o の文字数が最大となるものを一つ出力せよ。
そのような文字列が複数ある場合、どれを出力しても正答となる。
入力例 1
#..#.
出力例 1
#o.#o
T= #o.#o とすると全ての条件を満たすことが確認できます。
全ての条件を満たす T であって、 o の文字数が 2 より多い文字列は存在しないので #o.#o を出力すると正答となります。
この他にも #.o#o を出力しても正答となります。
入力例 2
#
出力例 2
#
入力例 3
.....
出力例 3
..o..
この他にも o....、.o...、...o.、....o を出力しても正答となります。
入力例 4
...#..#.##.#.
出力例 4
o..#.o#o##o#o
Score : 250 points
Problem Statement
You are given a string S consisting of . and #.
Among all strings T that satisfy all of the following conditions, find one with the maximum number of os.
- The length of T is equal to the length of S.
- T consists of
.,#, oro. - T_i=
#if and only if S_i=#. - If T_i=T_j=
o(i < j), then there exists at least one#among T_{i+1},\ldots,T_{j-1}.
Constraints
- S is a string consisting of
.and#with length between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Output one string with the maximum number of os among all strings T that satisfy all conditions.
If there are multiple such strings, printing any of them will be considered correct.
Sample Input 1
#..#.
Sample Output 1
#o.#o
Setting T= #o.#o satisfies all conditions.
There is no string T that satisfies all conditions and has more than two os, so outputting #o.#o is correct.
Outputting #.o#o is also correct.
Sample Input 2
#
Sample Output 2
#
Sample Input 3
.....
Sample Output 3
..o..
Outputting o...., .o..., ...o., or ....o is also correct.
Sample Input 4
...#..#.##.#.
Sample Output 4
o..#.o#o##o#o
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の文字列 S_1,\ldots,S_N が与えられます。
全ての要素が 1 以上 N 以下であるような長さ K の数列 (A_1,\ldots,A_K) に対し、
文字列 f(A_1,\ldots,A_K) を S_{A_1}+S_{A_2}+\dots+S_{A_K} と定めます。ここで + は文字列の連結を表します。
N^K 個の数列全てについての f(A_1,\dots,A_K) を辞書順に並べたとき、小さい方から X 番目の文字列を求めてください。
制約
- 1\leq N \leq 10
- 1\leq K \leq 5
- 1\leq X \leq N^K
- S_i は英小文字からなる長さ 10 以下の文字列
- N,K,X は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K X S_1 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 2 6 abc xxx abc
出力例 1
abcxxx
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
であり、これらを辞書順に並べた abcabc, abcabc, abcabc, abcabc, abcxxx, abcxxx, xxxabc, xxxabc, xxxxxx の 6 番目は abcxxx です。
入力例 2
5 5 416 a aa aaa aa a
出力例 2
aaaaaaa
Score : 300 points
Problem Statement
You are given N strings S_1,\ldots,S_N.
For a sequence (A_1,\ldots,A_K) of length K where all elements are between 1 and N, inclusive,
define the string f(A_1,\ldots,A_K) as S_{A_1}+S_{A_2}+\dots+S_{A_K}. Here, + represents string concatenation.
When all f(A_1,\dots,A_K) for the N^K sequences are sorted in lexicographical order, find the X-th smallest string.
Constraints
- 1\leq N \leq 10
- 1\leq K \leq 5
- 1\leq X \leq N^K
- S_i is a string consisting of lowercase English letters with length at most 10.
- N, K, and X are all integers.
Input
The input is given from Standard Input in the following format:
N K X S_1 \vdots S_N
Output
Output the answer.
Sample Input 1
3 2 6 abc xxx abc
Sample Output 1
abcxxx
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
When these are sorted in lexicographical order: abcabc, abcabc, abcabc, abcabc, abcxxx, abcxxx, xxxabc, xxxabc, xxxxxx, the 6-th is abcxxx.
Sample Input 2
5 5 416 a aa aaa aa a
Sample Output 2
aaaaaaa
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) と正整数 M が与えられます。
A の要素を自由に並び替えることが出来るとき、 \displaystyle \sum_{i=1}^N \left((A_i+B_i) \bmod M\right) としてありうる最小値を求めて下さい。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T \le 10^5
- 1\le N\le 3\times 10^5
- 1\le M\le 10^9
- 0\le A_i,B_i < M
- 全てのテストケースにおける N の総和は 3\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケース \text{case}_i は以下の形式で与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
T 行出力せよ。
j 行目には j 番目のテストケースについて、 \displaystyle \sum_{i=1}^N \left((A_i+B_i) \bmod M\right) としてありうる最小値を出力せよ。
入力例 1
3 3 6 3 1 4 2 0 1 1 1000000000 999999999 999999999 10 201 144 150 176 154 110 187 38 136 111 46 96 109 73 63 85 1 156 7 13 171
出力例 1
5 999999998 619
1 つ目のテストケースについて、 A を 4,3,1 と並び替えると (A_i+B_i)\bmod M はそれぞれ 0,3,2 となり、これらの総和は 5 となります。
Score : 400 points
Problem Statement
You are given length-N sequences A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N) consisting of non-negative integers, and a positive integer M.
When you can freely rearrange the elements of A, find the minimum possible value of \displaystyle \sum_{i=1}^N \left((A_i+B_i) \bmod M\right).
T test cases are given, so find the answer for each of them.
Constraints
- 1\le T \le 10^5
- 1\le N\le 3\times 10^5
- 1\le M\le 10^9
- 0\le A_i,B_i < M
- The sum of N over all test cases is at most 3\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case \text{case}_i is given in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Output T lines.
The j-th line should contain the minimum possible value of \displaystyle \sum_{i=1}^N \left((A_i+B_i) \bmod M\right) for the j-th test case.
Sample Input 1
3 3 6 3 1 4 2 0 1 1 1000000000 999999999 999999999 10 201 144 150 176 154 110 187 38 136 111 46 96 109 73 63 85 1 156 7 13 171
Sample Output 1
5 999999998 619
For the first test case, if we rearrange A as 4,3,1, then (A_i+B_i)\bmod M becomes 0,3,2, respectively, and their sum is 5.
Time Limit: 3.5 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
AtCoder 国には 1 から N の番号がついた N 個の街と、M 本の道路、K 個の空港があります。
i 番目の道路は街 A_i と街 B_i を双方向に結び、移動に C_i 時間かかります。
空港は街 D_1,\ldots,D_K にあり、空港がある街同士は T 時間で移動できます。
Q 個のクエリが与えられるので順に処理してください。クエリは次の 3 種類のいずれかです。
1 x y t: 街 x と街 y を双方向に t 時間で結ぶ道路が作られる。2 x: 街 x に空港が作られる。3: f(x,y) を、街 x から街 y へ道路と空港を使って到達可能なときその最短時間、到達不可能なとき 0 とする。\sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y) を求める。
制約
- 1 \leq N \leq 500
- 0 \leq M \leq 10^5
- 1 \leq A_i < B_i \leq N
- 1 \leq C_i \leq 10^9
- 0 \leq K \leq N
- 1 \leq T \leq 10^9
- 1 \leq D_1 < \dots < D_K \leq N
- 1 \leq Q \leq 1000
- 1 種類目のクエリにおいて、1\leq x < y \leq N, 1 \leq t \leq 10^9
- 2 種類目のクエリにおいて、1 \leq x \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1 B_1 C_1
\vdots
A_M B_M C_M
K T
D_1 \dots D_K
Q
\mathrm{Query}_1
\vdots
\mathrm{Query}_Q
\mathrm{Query}_i は i 番目のクエリを表し、その形式と意味は問題文中に与えられた通りである。
出力
3 種類目のクエリの解を順に改行区切りで出力せよ。
入力例 1
4 1 1 2 10 2 100 1 3 5 3 1 2 3 60 3 2 4 3
出力例 1
440 280 900
AtCoder 国には 4 つの街があり、最初、街 1 と街 2 の間を 10 時間で移動できる道路と、街 1 と街 3 の間を 100 時間で移動できる空港があります。
- 最初、f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=100,f(2,3)=f(3,2)=110 であり、その他は 0 なので、\sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=440 です。
- 新たに街 2 と街 3 を 60 時間で結ぶ道路が作られました。
- f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=70,f(2,3)=f(3,2)=60 であり、その他は 0 なので、\sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=280 です。
- 新たに街 4 に空港ができました。
- f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=70,f(1,4)=f(4,1)=100,f(2,3)=f(3,2)=60,f(2,4)=f(4,2)=110,f(3,4)=f(4,3)=100 であり、その他は 0 なので、\sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=900 です。
2つの街の間に複数の道路が作られることや、1つの街に複数の空港が作られることもあります。
Score : 450 points
Problem Statement
AtCoder Country has N cities numbered from 1 to N, M roads, and K airports.
The i-th road connects cities A_i and B_i bidirectionally and takes C_i hours to travel. There are airports in cities D_1,\ldots,D_K, and you can travel between cities with airports in T hours.
Process Q queries in order. Each query is one of the following three types:
1 x y t: A road connecting cities x and y bidirectionally in t hours is built.2 x: An airport is built in city x.3: Let f(x,y) be the smallest number of hours needed to reach city y from city x using roads and airports if reachable, and 0 if unreachable. Find \sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y).
Constraints
- 1 \leq N \leq 500
- 0 \leq M \leq 10^5
- 1 \leq A_i < B_i \leq N
- 1 \leq C_i \leq 10^9
- 0 \leq K \leq N
- 1 \leq T \leq 10^9
- 1 \leq D_1 < \dots < D_K \leq N
- 1 \leq Q \leq 1000
- For type 1 queries: 1\leq x < y \leq N, 1 \leq t \leq 10^9.
- For type 2 queries: 1 \leq x \leq N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
\vdots
A_M B_M C_M
K T
D_1 \dots D_K
Q
\mathrm{Query}_1
\vdots
\mathrm{Query}_Q
\mathrm{Query}_i represents the i-th query, whose format and meaning are as given in the problem statement.
Output
Output the answers to type 3 queries in order, separated by newlines.
Sample Input 1
4 1 1 2 10 2 100 1 3 5 3 1 2 3 60 3 2 4 3
Sample Output 1
440 280 900
AtCoder Country has four cities. Initially, there is a road connecting cities 1 and 2 in 10 hours, and airports connecting cities 1 and 3 in 100 hours.
- Initially, f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=100, f(2,3)=f(3,2)=110, and others are 0, so \sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=440.
- A new road connecting cities 2 and 3 in 60 hours is built.
- f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=70, f(2,3)=f(3,2)=60, and others are 0, so \sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=280.
- A new airport is built in city 4.
- f(1,2)=f(2,1)=10, f(1,3)=f(3,1)=70, f(1,4)=f(4,1)=100, f(2,3)=f(3,2)=60, f(2,4)=f(4,2)=110, f(3,4)=f(4,3)=100, and others are 0, so \sum_{x=1}^{N}\sum_{y=1}^{N}f(x,y)=900.
Multiple roads may exist between some pair of cities. Also, a city may have multiple airports.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
1 から N までの番号がついた N 頂点からなる木 T と整数 K が与えられます。 i 番目 (1\le i\le N - 1) の辺は頂点 U_i と頂点 V_i を結んでいます。また、頂点 i (1\le i\le N) には整数 A_i が書かれています。はじめ、全ての頂点は白色で塗られています。
あなたは以下の行動を 0 回以上 K 回以下行います:
- 木 T のパスであってそのパスに含まれるどの頂点も白色で塗られているようなパスを選ぶ。その後、選んだパスに含まれる頂点を全て黒色で塗る。
行動を終えた後に黒色で塗られた頂点に書かれた整数の総和としてあり得る最大値を求めてください。
制約
- 2\le N\le 2\times 10^5
- 1\le K\le 5
- 1\le A_i\le 10^9
- 1\le U_i < V_i \le N
- 与えられるグラフは木
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K
A_1 A_2 \ldots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
答えを出力せよ。
入力例 1
4 1 1 2 4 8 1 2 1 3 1 4
出力例 1
13
頂点 3,4 を両端に持つパスを選ぶと、頂点 1,3,4 を黒く塗ることができます。この場合の黒色で塗られた頂点に書かれた整数の総和は 1+4+8=13 です。
黒色で塗られた頂点に書かれた整数の総和を 13 より大きくすることはできないので、 13 を出力してください。
入力例 2
7 2 1 2 3 4 5 6 7 1 2 1 3 2 4 2 5 3 6 3 7
出力例 2
27
例えば以下のように操作することで黒色で塗られた頂点に書かれた整数の総和を 27 にすることができます:
- 頂点 4,5 を両端に持つパスを選ぶ。頂点 2,4,5 を黒く塗る。
- 頂点 6,7 を両端に持つパスを選ぶ。頂点 3,6,7 を黒く塗る。
黒色で塗られた頂点に書かれた整数の総和を 27 より大きくすることはできないので、 27 を出力してください。
入力例 3
11 3 1 9 1 3 7 9 10 9 7 3 4 7 8 2 7 5 7 3 4 7 11 1 9 1 10 3 6 1 7 3 7
出力例 3
52
Score : 525 points
Problem Statement
You are given a tree T with N vertices numbered from 1 to N, and an integer K. The i-th edge (1\le i\le N - 1) connects vertices U_i and V_i. Also, vertex i (1\le i\le N) has an integer A_i written on it. Initially, all vertices are colored white.
You perform the following action at least 0 times and at most K times:
- Choose a path in tree T such that all vertices on the path are colored white. Then, color all vertices on the chosen path black.
Find the maximum possible sum of integers written on vertices that are colored black after finishing the actions.
Constraints
- 2\le N\le 2\times 10^5
- 1\le K\le 5
- 1\le A_i\le 10^9
- 1\le U_i < V_i \le N
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K
A_1 A_2 \ldots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Output the answer.
Sample Input 1
4 1 1 2 4 8 1 2 1 3 1 4
Sample Output 1
13
If you choose the path with vertices 3 and 4 as endpoints, you can color vertices 1,3,4 black. In this case, the sum of integers written on the colored vertices is 1+4+8=13.
You cannot make the sum of integers written on black vertices greater than 13, so output 13.
Sample Input 2
7 2 1 2 3 4 5 6 7 1 2 1 3 2 4 2 5 3 6 3 7
Sample Output 2
27
For example, you can make the sum of integers written on black vertices 27 by performing operations as follows:
- Choose the path with vertices 4 and 5 as endpoints. Color vertices 2,4,5 black.
- Choose the path with vertices 6 and 7 as endpoints. Color vertices 3,6,7 black.
You cannot make the sum of integers written on black vertices greater than 27, so output 27.
Sample Input 3
11 3 1 9 1 3 7 9 10 9 7 3 4 7 8 2 7 5 7 3 4 7 11 1 9 1 10 3 6 1 7 3 7
Sample Output 3
52
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 625 点
問題文
N 個の文字列 S_1,\ldots,S_N が与えられます。
全ての要素が 1 以上 N 以下であるような長さ K の数列 (A_1,\ldots,A_K) に対し、
文字列 f(A_1,\ldots,A_K) を S_{A_1}+S_{A_2}+\dots+S_{A_K} と定めます。ここで + は文字列の連結を表します。
N^K 個の数列全てについての f(A_1,\dots,A_K) のうち辞書順最小の文字列を求めてください。
制約
- 1\leq N \leq 10^5
- 1\leq K \leq 10^5
- S_i は英小文字からなる長さ 10 以下の文字列
- N,K は整数
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 2 abc xxx abc
出力例 1
abcabc
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
であり、これらのうち辞書順最小は abcabc です。
入力例 2
4 3 abcd abc ab a
出力例 2
aaa
入力例 3
3 2 cba cb c
出力例 3
cbac
Score : 625 points
Problem Statement
You are given N strings S_1,\ldots,S_N.
For a sequence (A_1,\ldots,A_K) of length K where all elements are between 1 and N, inclusive,
define the string f(A_1,\ldots,A_K) as S_{A_1}+S_{A_2}+\dots+S_{A_K}. Here, + represents string concatenation.
Among all f(A_1,\dots,A_K) for the N^K sequences, find the lexicographically smallest string.
Constraints
- 1\leq N \leq 10^5
- 1\leq K \leq 10^5
- S_i is a string consisting of lowercase English letters with length at most 10.
- N and K are integers.
Input
The input is given from Standard Input in the following format:
N K S_1 \vdots S_N
Output
Output the answer.
Sample Input 1
3 2 abc xxx abc
Sample Output 1
abcabc
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
Among these, the lexicographically smallest is abcabc.
Sample Input 2
4 3 abcd abc ab a
Sample Output 2
aaa
Sample Input 3
3 2 cba cb c
Sample Output 3
cbac