Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の人がいます。N 人の人には人 1, 人 2,\dots, 人 N と番号がついています。
人 i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。
人 1 が人 N の何代前か求めてください。
制約
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N P_2 P_3 \dots P_N
出力
答えを整数として出力せよ。
入力例 1
3 1 2
出力例 1
2
人 2 は人 3 の親であるため、人 3 の 1 代前です。
人 1 は人 2 の親であるため、人 3 の 2 代前です。
よって解は 2 です。
入力例 2
10 1 2 3 4 5 6 7 8 9
出力例 2
9
Score : 200 points
Problem Statement
There are N people, called Person 1, Person 2, \ldots, Person N.
The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.
How many generations away from Person N is Person 1?
Constraints
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_2 P_3 \dots P_N
Output
Print the answer as a positive integer.
Sample Input 1
3 1 2
Sample Output 1
2
Person 2 is a parent of Person 3, and thus is one generation away from Person 3.
Person 1 is a parent of Person 2, and thus is two generations away from Person 3.
Therefore, the answer is 2.
Sample Input 2
10 1 2 3 4 5 6 7 8 9
Sample Output 2
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。
高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。
高橋君が抜き取ったカードに書かれていた整数を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- 各 k \, (1 \leq k \leq N) に対し、A_i = k となる i は 4 個以下である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{4N - 1}
出力
答えを出力せよ。
入力例 1
3 1 3 2 3 3 2 2 1 1 1 2
出力例 1
3
高橋君が抜き取ったカードには 3 が書かれています。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
出力例 3
2
Score : 200 points
Problem Statement
We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.
Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.
Find the integer written on the card removed by Takahashi.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{4N - 1}
Output
Print the answer.
Sample Input 1
3 1 3 2 3 3 2 2 1 1 1 2
Sample Output 1
3
Takahashi removed a card with 3 written on it.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。
ここで、S_i は 0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列です。
それぞれのリールには対応するボタンがついており、高橋君は各非負整数 t について、
スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す(または何もしない)ことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、
i 番目のリールは S_i の (t\bmod{10})+1 文字目を表示して止まります。
ただし、t\bmod{10} で t を 10 で割ったあまりを表します。
高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
制約
- 2\leq N\leq 100
- N は整数
- S_i は
0,1, \ldots,9がちょうど 1 回ずつ現れる長さ 10 の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを出力せよ。
入力例 1
3 1937458062 8124690357 2385760149
出力例 1
6
高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。
- スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0\bmod{10})+1=1 文字目である
8を表示して止まります。 - スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2\bmod{10})+1=3 文字目である
8を表示して止まります。 - スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6\bmod{10})+1=7 文字目である
8を表示して止まります。
5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。
入力例 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
出力例 2
40
全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。
Score : 300 points
Problem Statement
There is a slot machine with N reels.
The placement of symbols on the i-th reel is represented by a string S_i of length 10 containing each of 0, 1, \ldots, 9 exactly once.
Each reel has a corresponding button. For each non-negative integer t, Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning.
If the button for the i-th reel is pressed t seconds after the start of the spin, the i-th reel will stop to display the ((t\bmod{10})+1)-th character of S_i.
Here, t\bmod{10} denotes the remainder when t is divided by 10.
Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.
Constraints
- 2\leq N\leq 100
- N is an integer.
- S_i is a string of length 10 containing each of
0,1, \ldots,9exactly once.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.
Sample Input 1
3 1937458062 8124690357 2385760149
Sample Output 1
6
Takahashi can make all reels display 8 in 6 seconds after the start of the spin by stopping them as follows.
- 0 seconds after the start of the spin, press the button for the 2-nd reel, making it stop to display the ((0\bmod{10})+1=1)-st character of S_2,
8. - 2 seconds after the start of the spin, press the button for the 3-rd reel, making it stop to display the ((2\bmod{10})+1=3)-rd character of S_3,
8. - 6 seconds after the start of the spin, press the button for the 1-st reel, making it stop to display the ((6\bmod{10})+1=7)-th character of S_1,
8.
There is no way to make all reels display the same character in five seconds or less, so the answer is 6.
Sample Input 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
Sample Output 2
40
Note that he must stop all reels to make them display the same character.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j} は U, D, L, R のいずれかです。
あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。
あなたは現在 (i,j) にいるとする。
G_{i,j} がUであり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j} がDであり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j} がLであり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j} がRであり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。
操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1 を出力してください。
制約
- 1 \leq H, W \leq 500
- G_{i,j} は
U,D,L,Rのいずれかである。 - H, W は整数である。
入力
入力は以下の形式で標準入力から与えられる。
H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}
出力
操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。
i j
また、無限に動き続ける場合は -1 を出力せよ。
入力例 1
2 3 RDU LRU
出力例 1
1 3
あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。
入力例 2
2 3 RRD ULL
出力例 2
-1
あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1 を出力します。
入力例 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
出力例 3
9 5
Score : 300 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U, D, L, or R.
You are initially at (1,1). You repeat the following operation until you cannot make a move.
Let (i,j) be the square you are currently at.
If G_{i,j} isUand i \neq 1, move to (i-1,j).
If G_{i,j} isDand i \neq H, move to (i+1,j).
If G_{i,j} isLand j \neq 1, move to (i,j-1).
If G_{i,j} isRand j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.
Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.
Constraints
- 1 \leq H, W \leq 500
- G_{i,j} is
U,D,L, orR. - H and W are integers.
Input
Input is given from Standard Input in the following format:
H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}
Output
If you end up at (i, j), print it in the following format:
i j
If you indefinitely repeat moving, print -1.
Sample Input 1
2 3 RDU LRU
Sample Output 1
1 3
You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).
Sample Input 2
2 3 RRD ULL
Sample Output 2
-1
You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1 should be printed in this case.
Sample Input 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
Sample Output 3
9 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君が N 回コイントスを行います。 また、高橋君はカウンタを持っており、最初カウンタの数値は 0 です。
i 回目のコイントスで表裏のどちらが出たかによって、次のことが起こります。
- 表が出たとき:高橋君はカウンタの数値を 1 増やし、X_i 円もらう。
- 裏が出たとき:高橋君はカウンタの数値を 0 に戻す。お金をもらうことは出来ない。
また、M 種類の連続ボーナスがあり、i 種類目の連続ボーナスではカウンタの数値が C_i になるたびに Y_i 円もらうことができます。
高橋君は最大で何円もらうことができるかを求めてください。
制約
- 1\leq M\leq N\leq 5000
- 1\leq X_i\leq 10^9
- 1\leq C_i\leq N
- 1\leq Y_i\leq 10^9
- C_1,C_2,\ldots,C_M はすべて異なる。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 X_2 \ldots X_N C_1 Y_1 C_2 Y_2 \vdots C_M Y_M
出力
高橋君がもらうことのできる金額の最大値を整数で出力せよ。
入力例 1
6 3 2 7 1 8 2 8 2 10 3 1 5 5
出力例 1
48
順に 表, 表, 裏, 表, 表, 表 が出た時、もらえる金額は次のようになります。
- 1 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、2 円もらう。
- 2 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、7 円もらう。さらに、連続ボーナスとして 10 円もらう。
- 3 回目のコイントスで裏が出る。カウンタの数値を 2 から 0 にする。
- 4 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、8 円もらう。
- 5 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、2 円もらう。さらに、連続ボーナスとして 10 円もらう。
- 6 回目のコイントスで表が出る。カウンタの数値を 2 から 3 にして、8 円もらう。さらに、連続ボーナスとして 1 円もらう。
このとき高橋君は合計で 2+(7+10)+0+8+(2+10)+(8+1)=48 円もらうことができ、このときが最大です。
連続ボーナスはカウンタの数値が C_i になるたびに何度でももらえることに注意してください。
ちなみに、6 回のコイントスで全部表が出た時は 2+(7+10)+(1+1)+8+(2+5)+8=44 円しかもらうことが出来ず、この時は最大ではありません。
入力例 2
3 2 1000000000 1000000000 1000000000 1 1000000000 3 1000000000
出力例 2
5000000000
答えが 32 bit 整数型に収まらないこともあることに注意してください。
Score : 400 points
Problem Statement
Takahashi will toss a coin N times. He also has a counter, which initially shows 0.
Depending on the result of the i-th coin toss, the following happens:
- If it heads: Takahashi increases the counter's value by 1 and receives X_i yen (Japanese currency).
- If it tails: he resets the counter's value to 0, without receiving money.
Additionally, there are M kinds of streak bonuses. The i-th kind of streak bonus awards Y_i yen each time the counter shows C_i.
Find the maximum amount of money that Takahashi can receive.
Constraints
- 1\leq M\leq N\leq 5000
- 1\leq X_i\leq 10^9
- 1\leq C_i\leq N
- 1\leq Y_i\leq 10^9
- C_1,C_2,\ldots,C_M are all different.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M X_1 X_2 \ldots X_N C_1 Y_1 C_2 Y_2 \vdots C_M Y_M
Output
Print the maximum amount of money that Takahashi can receive, as an integer.
Sample Input 1
6 3 2 7 1 8 2 8 2 10 3 1 5 5
Sample Output 1
48
If he gets head, head, tail, head, head, head, in this order, the following amounts of money are awarded.
- In the 1-st coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 2 yen.
- In the 2-nd coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 7 yen. Additionally, get 10 yen as a streak bonus.
- In the 3-rd coin toss, the coin tails. Change the counter's value from 2 to 0.
- In the 4-th coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 8 yen.
- In the 5-th coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 2 yen. Additionally, get 10 yen as a streak bonus.
- In the 6-th coin toss, the coin heads. Change the counter's value from 2 to 3 and receive 8 yen. Additionally, get 1 yen as a streak bonus.
In this case, Takahashi receives 2+(7+10)+0+8+(2+10)+(8+1)=48 yen in total, which is the maximum possible.
Note that streak bonuses are awarded any number of times each time the counter shows C_i.
As a side note, if he gets head in all 6 coin tosses, he only receives 2+(7+10)+(1+1)+8+(2+5)+8=44 yen, which is not the maximum.
Sample Input 2
3 2 1000000000 1000000000 1000000000 1 1000000000 3 1000000000
Sample Output 2
5000000000
Note that the answer may not fit into a 32-bit integer type.