実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
英小文字からなる長さ M の文字列 N 個 S_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。
これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。
- 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i を 1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。
制約
- 2 \le N \le 8
- 1 \le M \le 5
- S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
- S_i は互いに異なる。
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 4 bbed abcd abed fbed
出力例 1
Yes
abcd , abed , bbed , fbed の順に並び替えると条件を満たします。
入力例 2
2 5 abcde abced
出力例 2
No
どのように並び替えても条件を満たすことは出来ません。
入力例 3
8 4 fast face cast race fact rice nice case
出力例 3
Yes
Score : 250 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.
Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:
- for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.
Constraints
- 2 \le N \le 8
- 1 \le M \le 5
- S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
- S_i are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print Yes if one can obtain a conforming sequence; print No otherwise.
Sample Input 1
4 4 bbed abcd abed fbed
Sample Output 1
Yes
One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.
Sample Input 2
2 5 abcde abced
Sample Output 2
No
No matter how the strings are rearranged, the condition is never satisfied.
Sample Input 3
8 4 fast face cast race fact rice nice case
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
WAtCoder には N 人のユーザがおり、1 から N までの番号がつけられています。 また、M 個のコンテストページがあり、1 から M までの番号がつけられています。 はじめ、すべてのユーザはどのコンテストページの閲覧権限も持っていません。
Q 個のクエリが与えられるので、順に処理してください。クエリは 3 種類あり、以下のいずれかの形式で与えられます。
1 X Y: ユーザ X にコンテストページ Y の閲覧権限を付与する。2 X: ユーザ X にすべてのコンテストページの閲覧権限を付与する。3 X Y: ユーザ X がコンテストページ Y を閲覧できるかを答える。
クエリの中で、あるユーザがすでに閲覧権限を持っているコンテストページについて、重ねて閲覧権限を付与されることもあります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X \leq N
- 1 \leq Y \leq M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリ \mathrm{query}_i は以下の 3 種類のいずれかの形式で与えられる。
1 X Y
2 X
3 X Y
出力
3 種類目のクエリのそれぞれについて、ユーザ X がコンテストページ Y を閲覧できるならば Yes を、そうでなければ No を改行区切りで出力せよ。
入力例 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
出力例 1
No Yes Yes
- 1 つ目のクエリで、ユーザ 1 にコンテストページ 2 の閲覧権限を付与します。
- 2 つ目のクエリの時点で、ユーザ 1 が閲覧できるコンテストページは 2 のみです。コンテストページ 1 の閲覧権限を持っていないので、
Noを出力します。 - 3 つ目のクエリの時点で、ユーザ 1 はコンテストページ 2 の閲覧権限を持っているので、
Yesを出力します。 - 4 つ目のクエリで、ユーザ 2 にすべてのコンテストページの閲覧権限を付与します。
- 5 つ目のクエリの時点で、ユーザ 2 が閲覧できるコンテストページは 1,2,3 です。コンテストページ 3 の閲覧権限を持っているので、
Yesを出力します。
入力例 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
出力例 2
No No No Yes
Score : 300 points
Problem Statement
There are N users on WAtCoder, numbered from 1 to N, and M contest pages, numbered from 1 to M. Initially, no user has view permission for any contest page.
You are given Q queries to process in order. Each query is of one of the following three types:
1 X Y: Grant user X view permission for contest page Y.2 X: Grant user X view permission for all contest pages.3 X Y: Answer whether user X can view contest page Y.
It is possible for a user to be granted permission for the same contest page multiple times.
Constraints
- 1 \le N \le 2\times 10^5
- 1 \le M \le 2\times 10^5
- 1 \le Q \le 2\times 10^5
- 1 \le X \le N
- 1 \le Y \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each \mathrm{query}_i is in one of the following formats:
1 X Y
2 X
3 X Y
Output
For each query of the third type, print Yes if user X can view contest page Y, otherwise print No, each on its own line.
Sample Input 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
Sample Output 1
No Yes Yes
- In the first query, user 1 is granted permission to view contest page 2.
- At the second query, user 1 can view only page 2; they cannot view page 1, so print
No. - At the third query, user 1 can view page 2, so print
Yes. - In the fourth query, user 2 is granted permission to view all pages.
- At the fifth query, user 2 can view pages 1,2,3; they can view page 3, so print
Yes.
Sample Input 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
Sample Output 2
No No No Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
あなたは最初、空文字列 S を持っています。
さらに、文字列がいくつか入った袋 1,2,\dots,N があります。
袋 i には A_i 個の文字列 S_{i,1},S_{i,2},\dots,S_{i,A_i} が入っています。
これから、以下の手順を i=1,2,\dots,N について繰り返します。
- 以下のふたつの行動のうち、どちらかを選択して行う。
- 1 円を支払い、袋 i からちょうどひとつの文字列を選択して S の末尾に連結する。
- 何もしない。
文字列 T が与えられるとき、最終的に S と T を一致させるために必要な最小の金額を求めてください。
但し、どのようにしても最終的な S を T に一致させることができない場合、 -1 と出力してください。
制約
- T は長さ 1 以上 100 以下の英小文字からなる文字列
- N は 1 以上 100 以下の整数
- A_i は 1 以上 10 以下の整数
- S_{i,j} は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
答えを整数として出力せよ。
入力例 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
出力例 1
2
例えば、以下のようにすると 2 円で最終的な S と T を一致させることができ、これが必要な金額の最低値であることが示せます。
- i=1 について、袋 1 から
abcを選択し S の末尾に連結する。 S=abcとなる。 - i=2 について、何もしない。
- i=3 について、袋 3 から
deを選択し S の末尾に連結する。 S=abcdeとなる。
入力例 2
abcde 3 2 ab abc 3 f c bcde 1 e
出力例 2
-1
どのようにしても最終的な S と T を一致させることができないので、 -1 と出力してください。
入力例 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
出力例 3
4
Score: 425 points
Problem Statement
You initially have an empty string S.
Additionally, there are bags 1, 2, \dots, N, each containing some strings.
Bag i contains A_i strings S_{i,1}, S_{i,2}, \dots, S_{i,A_i}.
You will repeat the following steps for i = 1, 2, \dots, N:
- Choose and perform one of the following two actions:
- Pay 1 yen, select exactly one string from bag i, and concatenate it to the end of S.
- Do nothing.
Given a string T, find the minimum amount of money required to make the final S equal T.
If there is no way to make the final S equal T, print -1.
Constraints
- T is a string consisting of lowercase English letters with length between 1 and 100, inclusive.
- N is an integer between 1 and 100, inclusive.
- A_i is an integer between 1 and 10, inclusive.
- S_{i,j} is a string consisting of lowercase English letters with length between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
Print the answer as an integer.
Sample Input 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
Sample Output 1
2
For example, doing the following makes the final S equal T with two yen, which can be shown to be the minimum amount required.
- For i=1, select
abcfrom bag 1 and concatenate it to the end of S, making S=abc. - For i=2, do nothing.
- For i=3, select
defrom bag 3 and concatenate it to the end of S, making S=abcde.
Sample Input 2
abcde 3 2 ab abc 3 f c bcde 1 e
Sample Output 2
-1
There is no way to make the final S equal T, so print -1.
Sample Input 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
二次元平面上の第一象限上に N 個のフの字があります。
i\ (1 \leq i \leq N) 個目のフの字は、(x_i-1,y_i) と (x_i,y_i) を結ぶ線分と (x_i,y_i-1) と (x_i,y_i) を結ぶ線分の 2 つを組み合わせた図形です。
あなたは、N 個のフの字から 0 個以上を選び、削除することができます。
適切に削除するフの字を選んだとき、原点から全体が見えるフの字の個数は最大でいくつになりますか?
ここで、原点からあるフの字(便宜上 i 個目のフの字とする)の全体が見える必要十分条件は、以下の通りです。
- 原点、(x_i-1,y_i)、(x_i,y_i)、(x_i,y_i-1) の 4 点を頂点とする四角形の内部(境界を除く)と他のフの字が共通部分を持たない。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq x_i,y_i \leq 10^9
- (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
x_1 y_1
x_2 y_2
\hspace{0.45cm}\vdots
x_N y_N
出力
原点から全体が見えるフの字の個数の最大値を出力せよ。
入力例 1
3 1 1 2 1 1 2
出力例 1
2
1 個目のフの字を削除したとき原点からは 2 個目のフの字と 3 個目のフの字の 2 つが見えるようになり、これが最大です。
1 つのフの字も削除しない場合、原点からは 1 個目のフの字のみしか見えません。
入力例 2
10 414598724 87552841 252911401 309688555 623249116 421714323 605059493 227199170 410455266 373748111 861647548 916369023 527772558 682124751 356101507 249887028 292258775 110762985 850583108 796044319
出力例 2
10
すべてのフの字を削除せずに残すのが最善です。
Score : 500 points
Problem Statement
There are N 7's drawn in the first quadrant of a plane.
The i-th 7 is a figure composed of a segment connecting (x_i-1,y_i) and (x_i,y_i), and a segment connecting (x_i,y_i-1) and (x_i,y_i).
You can choose zero or more from the N 7's and delete them.
What is the maximum possible number of 7's that are wholly visible from the origin after the optimal deletion?
Here, the i-th 7 is wholly visible from the origin if and only if:
- the interior (excluding borders) of the quadrilateral whose vertices are the origin, (x_i-1,y_i), (x_i,y_i), (x_i,y_i-1) does not intersect with the other 7's.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq x_i,y_i \leq 10^9
- (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
\hspace{0.45cm}\vdots
x_N y_N
Output
Print the maximum possible number of 7's that are wholly visible from the origin.
Sample Input 1
3 1 1 2 1 1 2
Sample Output 1
2
If the first 7 is deleted, the other two 7's ― the second and third ones ― will be wholly visible from the origin, which is optimal.
If no 7's are deleted, only the first 7 is wholly visible from the origin.
Sample Input 2
10 414598724 87552841 252911401 309688555 623249116 421714323 605059493 227199170 410455266 373748111 861647548 916369023 527772558 682124751 356101507 249887028 292258775 110762985 850583108 796044319
Sample Output 2
10
It is best to keep all 7's.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。
(フェイズ 1 )
- ジャッジが 1 以上 10^9 以下の整数 N を決める。この整数は隠されている。
- あなたは 1 以上 110 以下の整数 M を出力する。
- さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq A_i \leq M を満たす、長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力する。
(フェイズ 2 )
- ジャッジから、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられる。ここで、 B_i = f^N(i) である。 f(i) は 1 以上 M 以下の整数 i に対し f(i)=A_i で定められ、 f^N(i) は i を f(i) で置き換える操作を N 回行った際に得られる整数である。
- あなたは、B の情報から、ジャッジが決めた整数 N を特定し、N を出力する。
上記の手順を行った後、直ちにプログラムを終了することで正解となります。
制約
- N は 1 以上 10^9 以下の整数
入出力
この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
(フェイズ 1 )
- まず、1 以上 110 以下の整数 M を出力してください。出力後、必ず改行してください。
M
- その後、空白区切りで 1 以上 M 以下の整数からなる長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力してください。出力後、必ず改行してください。
A_1 A_2 \ldots A_M
(フェイズ 2 )
- まず、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が入力から与えられます。
B_1 B_2 \ldots B_M
- 整数 N を求め、出力してください。出力後、必ず改行してください。
N
不正な出力がなされた場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 答えを出力したら(または
-1を受け取ったら)直ちにプログラムを正常終了してください。そうしなかった場合、ジャッジ結果は不定です。 - 特に、余計な改行も不正なフォーマットの出力とみなされるので注意してください。
入出力例
以下は、N = 2 の場合の入出力例です。
| 入力 | 出力 | 説明 |
|---|---|---|
| ジャッジは N=2 と決めました。N は入力としては与えられず、隠されています。 | ||
4 |
M を出力します。 | |
2 3 4 4 |
A=(2,3,4,4) を出力します。 | |
3 4 4 4 |
f^2(1)=3,f^2(2)=4,f^2(3)=4,f^2(4)=4 なので、ジャッジは B=(3,4,4,4) をあなたのプログラムに与えます。 | |
2 |
B から N=2 であると特定しました。 N を出力し、プログラムを正常終了させてください。 | |
Score : 500 points
Problem Statement
This is an interactive task, where your and the judge's programs interact via Standard Input and Output.
You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.
(Phase 1)
- The judge decides an integer N between 1 and 10^9 (inclusive), which is hidden.
- You print an integer M between 1 and 110 (inclusive).
- You also print an integer sequence A=(A_1,A_2,\ldots,A_M) of length M such that 1 \leq A_i \leq M for all i = 1, 2, \ldots, M.
(Phase 2)
- The judge gives you an integer sequence B=(B_1,B_2,\ldots,B_M) of length M. Here, B_i = f^N(i). f(i) is defined by f(i)=A_i for all integers i between 1 and M (inclusive), and f^N(i) is the integer resulting from replacing i with f(i) N times.
- Based on the given B, you identify the integer N that the judge has decided, and print N.
After the procedure above, terminate the program immediately to be judged correct.
Constraints
- N is an integer between 1 and 10^9 (inclusive).
Input and Output
This is an interactive task, where your and the judge's programs interact via Standard Input and Output.
(Phase 1)
- First, print an integer M between 1 and 110 (inclusive). It must be followed by a newline.
M
- Then, print a sequence A=(A_1,A_2,\ldots,A_M) of length M consisting of integers between 1 and M (inclusive), with spaces in between. It must be followed by a newline.
A_1 A_2 \ldots A_M
(Phase 2)
- First, an integer sequence B=(B_1,B_2,\ldots,B_M) of length M is given from the input.
B_1 B_2 \ldots B_M
- Find the integer N and print it. It must be followed by a newline.
N
If you print something illegal, the judge prints -1. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.
Notes
- After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
- If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.
- After you print the answer (or you receive
-1), immediately terminate the program normally. Otherwise, the verdict will be indeterminate. - Note that an excessive newline is also considered an invalid input.
Sample Interaction
The following is a sample interaction with N = 2.
| Input | Output | Description |
|---|---|---|
| The judge has decided that N=2. N is hidden: it is not given as an input. | ||
4 |
You print M. | |
2 3 4 4 |
You print A=(2,3,4,4). | |
3 4 4 4 |
f^2(1)=3,f^2(2)=4,f^2(3)=4, and f^2(4)=4, so the judge gives B=(3,4,4,4) to your program. | |
2 |
Based on B, you have identified that N=2. Print N and terminate the program normally. | |