実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君の家には、高橋君、高橋君の父、高橋君の母の 3 人が住んでおり、全員が毎晩風呂で髪を洗います。
風呂には、高橋君の父、高橋君の母、高橋君の順に入り、それぞれシャンプーを A,B,C ミリリットル使います。
今朝の時点で、ボトルには V ミリリットルのシャンプーが残っていました。このまま補充しない時、初めてシャンプーが不足するのは誰が使おうとした時ですか?
制約
- 1 \leq V,A,B,C \leq 10^5
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
V A B C
出力
初めてシャンプーが不足するのが、高橋君の父が使おうとしたときならば F、高橋君の母が使おうとしたときならば M、高橋君が使おうとしたときならば T を出力せよ。
入力例 1
25 10 11 12
出力例 1
T
シャンプーは 25 ミリリットル残っています。
- まず高橋君の父が 10 ミリリットル使い、残りは 15 ミリリットルになります。
- 次に高橋君の母が 11 ミリリットル使い、残りは 4 ミリリットルになります。
- 最後に高橋君が 12 ミリリットル使おうとしますが、4 ミリリットルしか残っておらず、不足しています。
入力例 2
30 10 10 10
出力例 2
F
シャンプーは 30 ミリリットル残っています。
- まず高橋君の父が 10 ミリリットル使い、残りは 20 ミリリットルになります。
- 次に高橋君の母が 10 ミリリットル使い、残りは 10 ミリリットルになります。
- 続いて高橋君が 10 ミリリットル使い、残りは 0 ミリリットルになります。
- 翌日、高橋君の父が 10 ミリリットル使おうとしますが、0 ミリリットルしか残っておらず、不足しています。
入力例 3
100000 1 1 1
出力例 3
M
Score : 100 points
Problem Statement
Three people live in Takahashi's house: Takahashi, his father, and his mother. All of them wash their hair in the bathroom each night.
His father, his mother, and Takahashi take a bath in this order and use A, B, and C milliliters of shampoo, respectively.
This morning, the bottle contained V milliliters of shampoo. Without refilling, who will be the first to run short of shampoo to wash their hair?
Constraints
- 1 \leq V,A,B,C \leq 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
V A B C
Output
If the first person to run short of shampoo to wash their hair is Takahashi's father, print F; if it is Takahashi's mother, print M; if it is Takahashi, print T.
Sample Input 1
25 10 11 12
Sample Output 1
T
Now, they have 25 milliliters of shampoo.
- First, Takahashi's father uses 10 milliliters, leaving 15.
- Next, Takahashi's mother uses 11 milliliters, leaving 4.
- Finally, Takahashi tries to use 12 milliliters and runs short of shampoo since only 4 is remaining.
Sample Input 2
30 10 10 10
Sample Output 2
F
Now, they have 30 milliliters of shampoo.
- First, Takahashi's father uses 10 milliliters, leaving 20.
- Next, Takahashi's mother uses 10 milliliters, leaving 10.
- Then, Takahashi uses 10 milliliters, leaving 0.
- Next day, Takahashi's father tries to use 10 milliliters and runs short of shampoo since only 0 is remaining.
Sample Input 3
100000 1 1 1
Sample Output 3
M
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
(1,2,…,N) を並び替えた数列 P と整数 X が与えられます。 数列 P の i 番目の項の値は P_i です。 P_k = X を満たす k を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq X \leq N
- P は (1,2,…,N) を並び替えてできる数列
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X P_1 P_2 \ldots P_N
出力
答えを出力せよ。
入力例 1
4 3 2 3 1 4
出力例 1
2
P = (2,3,1,4) なので、P_2 = 3 です。したがって、2 を出力します。
入力例 2
5 2 3 5 1 4 2
出力例 2
5
入力例 3
6 6 1 2 3 4 5 6
出力例 3
6
Score : 100 points
Problem Statement
You are given a sequence P that is a permutation of (1,2,…,N), and an integer X. The i-th term of P has a value of P_i. Print k such that P_k = X.
Constraints
- 1 \leq N \leq 100
- 1 \leq X \leq N
- P is a permutation of (1,2,…,N).
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N X P_1 P_2 \ldots P_N
Output
Print the answer.
Sample Input 1
4 3 2 3 1 4
Sample Output 1
2
We have P = (2,3,1,4), so P_2 = 3. Thus, you should print 2.
Sample Input 2
5 2 3 5 1 4 2
Sample Output 2
5
Sample Input 3
6 6 1 2 3 4 5 6
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \leq K \leq N \leq 100
- K, N は整数
- S_i は英小文字からなる長さ 10 以下の文字列
- i \neq j ならば S_i \neq S_j
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを改行区切りで出力せよ。
入力例 1
5 3 abc aaaaa xyz a def
出力例 1
aaaaa abc xyz
このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc 、2 位の人のハンドルネームは aaaaa 、3 位の人のハンドルネームは xyz 、4 位の人のハンドルネームは a 、5 位の人のハンドルネームは def でした。
上位 3 人のハンドルネームは abc、aaaaa、xyz であるため、これを辞書順に並べ替えて aaaaa 、abc 、xyz の順に出力します。
入力例 2
4 4 z zyx zzz rbg
出力例 2
rbg z zyx zzz
入力例 3
3 1 abc arc agc
出力例 3
abc
Score : 200 points
Problem Statement
There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.
What is lexicographical order?
Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.
Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.
- Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
- If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.
Constraints
- 1 \leq K \leq N \leq 100
- K and N are integers.
- S_i is a string of length 10 consisting of lowercase English letters.
- S_i \neq S_j if i \neq j.
Input
The input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the nicknames, separated by newlines.
Sample Input 1
5 3 abc aaaaa xyz a def
Sample Output 1
aaaaa abc xyz
This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.
The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.
Sample Input 2
4 4 z zyx zzz rbg
Sample Output 2
rbg z zyx zzz
Sample Input 3
3 1 abc arc agc
Sample Output 3
abc
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 行 N 列のマス目が与えられます。上から i 行目、左から j 列目のマスには整数 A_{i,j} が書かれています。ここで、A_{i,j} は 0 か 1 であることが保証されます。
マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目を出力してください。
ただし外側のマスとは、1 行目、N 行目、1 列目、N 列目のいずれか 1 つ以上に属するマスの集合のことを指します。
制約
- 2 \le N \le 100
- 0 \le A_{i,j} \le 1(1 \le i,j \le N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
出力
マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目において、上から i 行目、左から j 列目のマスに書かれている整数を B_{i,j} と置く。このとき、以下の形式で出力せよ。
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}
入力例 1
4 0101 1101 1111 0000
出力例 1
1010 1101 0111 0001
上から i 行目、左から j 列目のマスを (i,j) と呼ぶこととします。
外側のマスは (1,1) から時計回りに列挙すると (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1),(2,1) の 12 個です。
これらのマスに書かれている整数を、時計回りに 1 個ずつ動かすと出力欄のようになります。
入力例 2
2 11 11
出力例 2
11 11
入力例 3
5 01010 01001 10110 00110 01010
出力例 3
00101 11000 00111 00110 10100
Score : 200 points
Problem Statement
You are given a grid with N rows and N columns. An integer A_{i, j} is written on the square at the i-th row from the top and j-th column from the left. Here, it is guaranteed that A_{i,j} is either 0 or 1.
Shift the integers written on the outer squares clockwise by one square each, and print the resulting grid.
Here, the outer squares are those in at least one of the 1-st row, N-th row, 1-st column, and N-th column.
Constraints
- 2 \le N \le 100
- 0 \le A_{i,j} \le 1(1 \le i,j \le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
Output
Let B_{i,j} be the integer written on the square at the i-th row from the top and j-th column from the left in the grid resulting from shifting the outer squares clockwise by one square each. Print them in the following format:
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}
Sample Input 1
4 0101 1101 1111 0000
Sample Output 1
1010 1101 0111 0001
We denote by (i,j) the square at the i-th row from the top and j-th column from the left.
The outer squares, in clockwise order starting from (1,1), are the following 12 squares: (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1), and (2,1).
The sample output shows the resulting grid after shifting the integers written on those squares clockwise by one square.
Sample Input 2
2 11 11
Sample Output 2
11 11
Sample Input 3
5 01010 01001 10110 00110 01010
Sample Output 3
00101 11000 00111 00110 10100
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
別世界の AtCoder で開催されている AtCoder Big Contest では、 10^{16} 問の問題が一度に出題されます。
問題の ID は 1 問目から順に A, B, ..., Z, AA, AB, ..., ZZ, AAA, ... と付けられています。
つまり、 ID は以下の順番で付けられています。
- 長さ 1 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 2 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 3 の英大文字からなる文字列を辞書順に並べたもの
- ...
このコンテストに含まれる問題の ID である文字列 S が与えられるので、それが何問目か答えてください。
制約
- S は AtCoder Big Contest に含まれる問題の ID として正しい
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
AB
出力例 1
28
ID が AB である問題は、 AtCoder Big Contest の 28 問目です。
入力例 2
C
出力例 2
3
ID が C である問題は、 AtCoder Big Contest の 3 問目です。
入力例 3
BRUTMHYHIIZP
出力例 3
10000000000000000
ID が BRUTMHYHIIZP である問題は、 AtCoder Big Contest の 10^{16} 問目、すなわち最終問題です。
Score : 300 points
Problem Statement
In a parallel universe, AtCoder holds AtCoder Big Contest, where 10^{16} problems are given at once.
The IDs of the problems are as follows, from the 1-st problem in order: A, B, ..., Z, AA, AB, ..., ZZ, AAA, ...
In other words, the IDs are given in the following order:
- the strings of length 1 consisting of uppercase English letters, in lexicographical order;
- the strings of length 2 consisting of uppercase English letters, in lexicographical order;
- the strings of length 3 consisting of uppercase English letters, in lexicographical order;
- ...
Given a string S that is an ID of a problem given in this contest, find the index of the problem. (See also Samples.)
Constraints
- S is a valid ID of a problem given in AtCoder Big Contest.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
AB
Sample Output 1
28
The problem whose ID is AB is the 28-th problem of AtCoder Big Contest, so 28 should be printed.
Sample Input 2
C
Sample Output 2
3
The problem whose ID is C is the 3-rd problem of AtCoder Big Contest, so 3 should be printed.
Sample Input 3
BRUTMHYHIIZP
Sample Output 3
10000000000000000
The problem whose ID is BRUTMHYHIIZP is the
10^{16}-th (last) problem of AtCoder Big Contest, so 10^{16} should be printed as an integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は N 日間の鉄道旅行を計画しています。
高橋君はそれぞれの日について、運賃の通常料金を払うか、1 日周遊パスを 1 枚使用するか選ぶことができます。
ここで、1\leq i\leq N について、i 日目の旅行にかかる運賃の通常料金は F_i 円です。
一方、1 日周遊パスは D 枚セットで P 円で発売されており、何セットでも購入することが可能ですが、D 枚単位でしか購入することができません。
また、購入したパスは 1 枚ずつ好きな日に使うことができ、旅行が終了した時点で余っていても構いません。
N 日間の旅行でかかる金額、すなわち 1 日周遊パスの購入にかかった代金と、1 日周遊パスを利用しなかった日における運賃の通常料金の合計金額の和としてあり得る最小値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D P F_1 F_2 \ldots F_N
出力
N 日間の旅行でかかる金額としてあり得る最小値を出力せよ。
入力例 1
5 2 10 7 1 6 3 6
出力例 1
20
1 日周遊パスを 1 セットだけ購入し、1 日目と 3 日目に使用すると、合計金額は (10\times 1)+(0+1+0+3+6)=20 となり、このときかかる金額が最小となります。
よって、20 を出力します。
入力例 2
3 1 10 1 2 3
出力例 2
6
3 日間すべてにおいて運賃の通常料金を支払ったときに最小となります。
入力例 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 3
3000000000
1 日周遊パスを 3 セット購入し、8 日間すべてにおいて 1 日周遊パスを利用したときに最小となります。
答えが 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
Takahashi is planning an N-day train trip.
For each day, he can pay the regular fare or use a one-day pass.
Here, for 1\leq i\leq N, the regular fare for the i-th day of the trip is F_i yen.
On the other hand, a batch of D one-day passes is sold for P yen. You can buy as many passes as you want, but only in units of D.
Each purchased pass can be used on any day, and it is fine to have some leftovers at the end of the trip.
Find the minimum possible total cost for the N-day trip, that is, the cost of purchasing one-day passes plus the total regular fare for the days not covered by one-day passes.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D P F_1 F_2 \ldots F_N
Output
Print the minimum possible total cost for the N-day trip.
Sample Input 1
5 2 10 7 1 6 3 6
Sample Output 1
20
If he buys just one batch of one-day passes and uses them for the first and third days, the total cost will be (10\times 1)+(0+1+0+3+6)=20, which is the minimum cost needed.
Thus, print 20.
Sample Input 2
3 1 10 1 2 3
Sample Output 2
6
The minimum cost is achieved by paying the regular fare for all three days.
Sample Input 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
3000000000
The minimum cost is achieved by buying three batches of one-day passes and using them for all eight days.
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
以下の 2 条件をともに満たすような整数の組 (i,j,k) の個数を求めてください。
- 1\leq i \lt j \lt k \leq N
- A_i,A_j,A_k は相異なる
制約
- 3 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 2\times 10^5
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 3 1 4 1
出力例 1
2
条件を満たす整数の組 (i,j,k) は (1,2,3),(1,3,4) の 2 つです。
入力例 2
10 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990
出力例 2
120
入力例 3
15 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
出力例 3
355
Score : 400 points
Problem Statement
You are given a sequence of length N: A=(A_1,A_2,\ldots,A_N).
Find the number of triples (i,j,k) that satisfy both of the following conditions.
- 1\leq i \lt j \lt k \leq N
- A_i, A_j, and A_k are distinct.
Constraints
- 3 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 2\times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 3 1 4 1
Sample Output 1
2
The two triples (i,j,k) satisfying the conditions are (1,2,3) and (1,3,4).
Sample Input 2
10 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990
Sample Output 2
120
Sample Input 3
15 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
Sample Output 3
355
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 頂点 M 辺の無向グラフ G が与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。
N 頂点のグラフは、すべての i = 1, 2, \ldots, K について下記の条件が成り立つとき、良いグラフと呼ばれます。
- G 上で頂点 x_i と頂点 y_i を結ぶパスが存在しない。
与えられるグラフ G は良いグラフです。
Q 個の独立な質問が与えられるので、それらすべてに答えてください。 i = 1, 2, \ldots, Q について、i 番目の質問は
- 入力で与えられたグラフ G に頂点 p_i と頂点 q_i を結ぶ無向辺を 1 本追加して得られるグラフ G^{(i)} は良いグラフですか?
という質問です。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- 1 \leq K \leq 2 \times 10^5
- 1 \leq x_i, y_i \leq N
- x_i \neq y_i
- i \neq j \implies \lbrace x_i, y_i \rbrace \neq \lbrace x_j, y_j \rbrace
- すべての i = 1, 2, \ldots, K について次が成り立つ:頂点 x_i と頂点 y_i を結ぶパスは存在しない。
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq p_i, q_i \leq N
- p_i \neq q_i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K x_1 y_1 x_2 y_2 \vdots x_K y_K Q p_1 q_1 p_2 q_2 \vdots p_Q q_Q
出力
Q 行出力せよ。
i = 1, 2, \ldots, Q について、i 行目には i 番目の質問に対する答えを出力せよ。
具体的には、グラフ G^{(i)} が良いグラフである場合は Yes を、良いグラフでない場合は No を出力せよ。
入力例 1
6 6 1 2 2 3 2 3 3 1 5 4 5 5 3 1 5 2 6 4 3 4 2 5 2 6 5 6 5 4
出力例 1
No No Yes Yes
- 1 番目の質問に関して、グラフ G^{(1)} は良いグラフではありません。なぜなら、頂点 x_1 = 1 と頂点 y_1 = 5 を結ぶパス 1 \rightarrow 2 \rightarrow 5 を持つからです。よって、
Noと出力します。 - 2 番目の質問に関して、グラフ G^{(2)} は良いグラフではありません。なぜなら、頂点 x_2 = 2 と頂点 y_2 = 6 を結ぶパス 2 \rightarrow 6 を持つからです。よって、
Noと出力します。 - 3 番目の質問に関して、グラフ G^{(3)} は良いグラフです。よって、
Yesと出力します。 - 4 番目の質問に関して、グラフ G^{(4)} は良いグラフです。よって、
Yesと出力します。
この入力例のように、与えられるグラフ G が自己ループや多重辺を持つ場合があることに注意してください。
Score : 475 points
Problem Statement
You are given an undirected graph G with N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertices u_i and v_i.
A graph with N vertices is called good if the following condition holds for all i = 1, 2, \ldots, K:
- there is no path connecting vertices x_i and y_i in G.
The given graph G is good.
You are given Q independent questions. Answer all of them. For i = 1, 2, \ldots, Q, the i-th question is as follows.
- Is the graph G^{(i)} obtained by adding an undirected edge connecting vertices p_i and q_i to the given graph G good?
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times10^5
- 1 \leq u_i, v_i \leq N
- 1 \leq K \leq 2 \times 10^5
- 1 \leq x_i, y_i \leq N
- x_i \neq y_i
- i \neq j \implies \lbrace x_i, y_i \rbrace \neq \lbrace x_j, y_j \rbrace
- For all i = 1, 2, \ldots, K, there is no path connecting vertices x_i and y_i.
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq p_i, q_i \leq N
- p_i \neq q_i
- 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 K x_1 y_1 x_2 y_2 \vdots x_K y_K Q p_1 q_1 p_2 q_2 \vdots p_Q q_Q
Output
Print Q lines.
For i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th question: Yes if the graph G^{(i)} is good, and No otherwise.
Sample Input 1
6 6 1 2 2 3 2 3 3 1 5 4 5 5 3 1 5 2 6 4 3 4 2 5 2 6 5 6 5 4
Sample Output 1
No No Yes Yes
- For the first question, the graph G^{(1)} is not good because it has a path 1 \rightarrow 2 \rightarrow 5 connecting vertices x_1 = 1 and y_1 = 5. Therefore, print
No. - For the second question, the graph G^{(2)} is not good because it has a path 2 \rightarrow 6 connecting vertices x_2 = 2 and y_2 = 6. Therefore, print
No. - For the third question, the graph G^{(3)} is good. Therefore, print
Yes. - For the fourth question, the graph G^{(4)} is good. Therefore, print
Yes.
As seen in this sample input, note that the given graph G may have self-loops or multi-edges.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
右向きを x 軸正方向、上向きを y 軸正方向とする座標平面の原点にロボットがいます。ロボットは最初、x 軸正方向を向いています。
i=1,\ldots,N の順に以下の操作を行います:
- ロボットを右回りまたは左回りに 90 度回転させる。その後、ロボットは向いている方向に A_i 進む
回転方向を適切に選ぶことで、N 回の操作後にロボットがいる座標を (X,Y) にすることはできますか?
できるならば、各操作において、右回りと左回りのどちらを選べばよいか求めてください。
制約
- 1 \leq N \leq 80
- 1 \leq A_i \leq 10^7
- -10^9\leq X,Y \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 \ldots A_N
出力
N 回の操作後にロボットがいる座標を (X,Y) にできないとき、No と出力せよ。
N 回の操作後にロボットがいる座標を (X,Y) にできるとき、1行目に Yes と出力し、2行目に次の条件を満たす長さ N の文字列 S を出力せよ。
条件: S は L または R のみからなり、S の i 番目の文字が L ならば i 回目の操作においてロボットを左回りに、R ならばロボットを右回りに回転させることで、N 回の操作後にロボットがいる座標を (X,Y) にできる。
答えが複数ある場合はどれを出力しても正解となる。
入力例 1
3 -2 4 3 2 1
出力例 1
Yes LLR
最初ロボットは (0,0) にいて、x 軸正方向を向いています。次の手順により、N 回の操作後にロボットがいる座標を (X,Y) にできます。
- 1 回目の操作:ロボットを左に 90 度回転させ、y 軸正方向を向かせる。ロボットは向いている方向に A_1=3 進み、ロボットのいる座標は (0,3) となる。
- 2 回目の操作:ロボットを左に 90 度回転させ、x 軸負方向を向かせる。ロボットは向いている方向に A_2=2 進み、ロボットのいる座標は (-2,3) となる。
- 3 回目の操作:ロボットを右に 90 度回転させ、y 軸正方向を向かせる。ロボットは向いている方向に A_3=1 進み、ロボットのいる座標は (-2,4) となる。

入力例 2
1 0 0 1
出力例 2
No
入力例 3
4 0 0 1 1 1 1
出力例 3
Yes LRRR
LLLL や RRRR などでも正解となります。
入力例 4
14 2543269 -1705099 3 14 159 2653 58979 323846 2643383 2795028 841971 69399 37510 58 20 9
出力例 4
Yes LLLLLLLLLRLRRR
Score : 525 points
Problem Statement
A robot is at the origin of a coordinate plane where the positive x-axis points to the right and the positive y-axis points upwards. Initially, the robot is facing the positive x-direction.
You will perform the following operation for i=1,\ldots,N in this order.
- Rotate the robot 90 degrees clockwise or counterclockwise. Then, the robot moves forward A_i units in the direction it is facing.
Can the direction of rotation be chosen so that the robot will be at the coordinates (X,Y) after the N operations?
If it is possible, determine which direction, clockwise or counterclockwise, should be chosen for each operation.
Constraints
- 1 \leq N \leq 80
- 1 \leq A_i \leq 10^7
- -10^9\leq X,Y \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y A_1 \ldots A_N
Output
If the robot cannot be at the coordinates (X,Y) after the N operations, print No.
If the robot can be at the coordinates (X,Y) after the N operations, the first line should contain Yes, and the second line should contain a string S of length N that satisfies the following condition.
Condition: S consists of L and R, and the following choices can put the robot at the coordinates (X,Y) after the N operations: in the i-th operation, rotate the robot counterclockwise if the i-th character of S is L, and rotate it clockwise if it is R.
If there are multiple solutions, you may print any of them.
Sample Input 1
3 -2 4 3 2 1
Sample Output 1
Yes LLR
Initially, the robot is at (0,0) and facing the positive x-direction. The following choices can put the robot at the coordinates (X,Y) after the N operations.
- First operation: Rotate the robot 90 degrees counterclockwise to face the positive y-direction. The robot moves forward A_1=3 units in the direction it is facing, and moves to (0,3).
- Second operation: Rotate the robot 90 degrees counterclockwise to face the negative x-direction. The robot moves forward A_2=2 units in the direction it is facing, and moves to (-2,3).
- Third operation: Rotate the robot 90 degrees clockwise to face the positive y-direction. The robot moves forward A_3=1 unit in the direction it is facing, and moves to (-2,4).

Sample Input 2
1 0 0 1
Sample Output 2
No
Sample Input 3
4 0 0 1 1 1 1
Sample Output 3
Yes LRRR
Outputs such as LLLL and RRRR are also accepted.
Sample Input 4
14 2543269 -1705099 3 14 159 2653 58979 323846 2643383 2795028 841971 69399 37510 58 20 9
Sample Output 4
Yes LLLLLLLLLRLRRR