Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から 2N の番号がついた 2N 人でじゃんけん大会をします。
大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 1 対 1 の N 試合からなります。
i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。
- i ラウンド目までの勝数が多い方が上位
- i ラウンド目までの勝数が同じときは、番号が小さい方が上位
また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。
- 各 k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする
各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。
未来予知ができる高橋君は、人 i が j ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j} は G, C, P のいずれかであり、それぞれグー、チョキ、パーを表します。
M ラウンド目終了時点の順位を求めてください。
じゃんけんのルール
じゃんけんの結果は、2 人の出した手に応じて次のように決まります。- 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
- 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
- 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
- 両者が同じ手を出したとき、引き分け
制約
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} は
G,C,Pのいずれか
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}
出力
2N 行出力せよ。
i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。
入力例 1
2 3 GCP PPP CCC PPC
出力例 1
3 1 2 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3 と 1、2 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。
入力例 2
2 2 GC PG CG PP
出力例 2
1 2 3 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。
Score : 300 points
Problem Statement
2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.
The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.
For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.
- A player with more wins in the first i rounds ranks higher.
- Ties are broken by ID numbers: a player with a smaller ID number ranks higher.
Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.
- For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.
In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.
Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G, C, or P.
Here, G stands for rock, C stands for scissors, and P stands for paper. (These derive from Japanese.)
Find the players' ranks at the end of the M-th round.
Rules of rock-scissors-paper
The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.- If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
- If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
- If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
- If the players play the same hand, the match is drawn.
Constraints
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} is
G,C, orP.
Input
Input is given from Standard Input in the following format:
N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}
Output
Print 2N lines.
The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.
Sample Input 1
2 3 GCP PPP CCC PPC
Sample Output 1
3 1 2 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.
Sample Input 2
2 2 GC PG CG PP
Sample Output 2
1 2 3 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の数列 A = (a_1, a_2, \dots, a_N) があります。
以下で説明される Q 個のクエリに答えてください。
- クエリ i : 整数の組 (x_i, k_i) が与えられます。A の要素を a_1, a_2, \dots と前から順に見ていったときに、数 x_i が k_i 回目に登場するのは A の前から何番目の要素を見たときかを出力してください。
ただし条件を満たす要素が存在しない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
- 1 \leq k_i \leq N (1 \leq i \leq Q)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q a_1 a_2 \dots a_N x_1 k_1 x_2 k_2 \vdots x_Q k_Q
出力
Q 行出力せよ。i 行目ではクエリ i に対する答えを出力せよ。
入力例 1
6 8 1 1 2 3 1 2 1 1 1 2 1 3 1 4 2 1 2 2 2 3 4 1
出力例 1
1 2 5 -1 3 6 -1 -1
A の中で 1 は a_1, a_2, a_5 に登場します。よって、クエリ 1 からクエリ 4 の答えは順に 1, 2, 5, -1 となります。
入力例 2
3 2 0 1000000000 999999999 1000000000 1 123456789 1
出力例 2
2 -1
Score : 300 points
Problem Statement
We have a sequence of N numbers: A = (a_1, a_2, \dots, a_N).
Process the Q queries explained below.
- Query i: You are given a pair of integers (x_i, k_i). Let us look at the elements of A one by one from the beginning: a_1, a_2, \dots Which element will be the k_i-th occurrence of the number x_i?
Print the index of that element, or -1 if there is no such element.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
- 1 \leq k_i \leq N (1 \leq i \leq Q)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q a_1 a_2 \dots a_N x_1 k_1 x_2 k_2 \vdots x_Q k_Q
Output
Print Q lines. The i-th line should contain the answer to Query i.
Sample Input 1
6 8 1 1 2 3 1 2 1 1 1 2 1 3 1 4 2 1 2 2 2 3 4 1
Sample Output 1
1 2 5 -1 3 6 -1 -1
1 occurs in A at a_1, a_2, a_5. Thus, the answers to Query 1 through 4 are 1, 2, 5, -1 in this order.
Sample Input 2
3 2 0 1000000000 999999999 1000000000 1 123456789 1
Sample Output 2
2 -1
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目にあるマスをマス (i, j) で表します。
N 個のマス (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) は壁になっています。
はじめ、高橋君はマス (r_\mathrm{s}, c_\mathrm{s}) にいます。
高橋君に Q 個の指示が与えられます。
i = 1, 2, \ldots, Q について、i 番目の指示は文字 d_i と正整数 l_i の組で表されます。d_i は L 、R 、U 、D のいずれかの文字であり、それぞれ左、右、上、下の方向を表します。
i 番目の指示に対して高橋君は下記の行動を l_i 回繰り返します。
現在いるマスに対して、d_i が表す向きに壁のないマスが隣接しているなら、そのマスに移動する。 そのようなマスが存在しない場合は、何もしない。
i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマスを出力してください。
制約
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- すべての i = 1, 2, \ldots, Nについて、(r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i)
- 1 \leq Q \leq 2 \times 10^5
- d_i は
L、R、U、Dのいずれかの文字 - 1 \leq l_i \leq 10^9
- d_i 以外の入力値は整数
入力
入力は以下の形式で標準入力から与えられる。
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
出力
Q 行出力せよ。 下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマス (R_i, C_i) を i 行目に出力せよ。
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
入力例 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
出力例 1
4 2 3 2 3 1 3 5
与えられるグリッドと高橋君の初期位置は下記の通りです。
ここで、# は壁のマスを、T は高橋君がいるマスを表し、. がその他のマスを表します。
...#. .#... ..... ...T. ..#..
1 つ目の指示に対して高橋君は、左に 2 マス移動し、高橋君の位置は下記の通り、マス (4, 2) になります。
...#. .#... ..... .T... ..#..
2 つ目の指示に対して高橋君は、上に 1 マスに移動した後、次の移動先が壁であるために「何もしない」を 2 回行います。その結果、高橋君の位置は下記の通り、マス (3, 2) になります。
...#. .#... .T... ..... ..#..
3 つ目の指示に対して高橋君は、左に 1 マス移動した後、次の移動先となるマスが存在しないために「何もしない」を 1 回行います。その結果、高橋君の位置は下記の通り、マス (3, 1) になります。
...#. .#... T.... ..... ..#..
4 つ目の指示に対して高橋君は、右に 4 マス移動し、高橋君の位置は下記の通り、マス (3, 5) になります。
...#. .#... ....T ..... ..#..
入力例 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
出力例 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Score : 400 points
Problem Statement
There is 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.
N squares, (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N), have walls.
Takahashi is initially at square (r_\mathrm{s}, c_\mathrm{s}).
Q instructions are given to Takahashi.
For i = 1, 2, \ldots, Q, the i-th instruction is represented by a pair of a character d_i and a positive integer l_i. d_i is one of L, R, U, and D, representing the directions of left, right, up, and down, respectively.
Given the i-th direction, Takahashi repeats the following action l_i times:
If a square without a wall is adjacent to the current square in the direction represented by d_i, move to that square; otherwise, do nothing.
For i = 1, 2, \ldots, Q, print the square where Takahashi will be after he follows the first i instructions.
Constraints
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- (r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i) for all i = 1, 2, \ldots, N.
- 1 \leq Q \leq 2 \times 10^5
- d_i is one of the characters
L,R,U, andD. - 1 \leq l_i \leq 10^9
- All values in the input other than d_i are integers.
Input
The input is given from Standard Input in the following format:
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the square (R_i, C_i) where Takahashi will be after he follows the first i instructions, in the following format:
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
Sample Input 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
Sample Output 1
4 2 3 2 3 1 3 5
The given grid and the initial position of Takahashi are as follows, where # denotes a square with a wall, T a square where Takahashi is, and . the other squares:
...#. .#... ..... ...T. ..#..
Given the 1-st instruction, Takahashi moves 2 squares to the left, ending up in square (4, 2) as follows:
...#. .#... ..... .T... ..#..
Given the 2-nd instruction, Takahashi first moves 1 square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square (3, 2) as follows:
...#. .#... .T... ..... ..#..
Given the 3-rd instruction, Takahashi first moves 1 square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square (3, 1) as follows:
...#. .#... T.... ..... ..#..
Given the 4-th instruction, Takahashi moves 4 squares to the right, ending up in square (3, 5) as follows:
...#. .#... ....T ..... ..#..
Sample Input 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
Sample Output 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
髙橋君は遊園地に遊びに行きました。
この遊園地には N 個のアトラクションがあり、i 個目のアトラクションの「楽しさ」の初期値は A_i です。
髙橋君が i 個目のアトラクションに乗ると、以下の現象が順番に起きます。
- 髙橋君の「満足度」に、i 個目のアトラクションの現在の「楽しさ」が加算される。
- i 個目のアトラクションの「楽しさ」が、1 減少する。
髙橋君の「満足度」の初期値は 0 です。髙橋君はアトラクションに合計 K 回まで乗ることができます。
最終的な髙橋君の「満足度」の最大値はいくつですか?
なお、髙橋君の「満足度」はアトラクションに乗ること以外で変化しません。
制約
- 1 \leq N \leq 10^5
- 1 \leq K \leq 2 \times 10^9
- 1 \leq A_i \leq 2 \times 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
最終的な髙橋君の「満足度」の最大値を出力せよ。
入力例 1
3 5 100 50 102
出力例 1
502
1 個目のアトラクションに 2 回、3 個目のアトラクションに 3 回乗ります。
最終的な「満足度」は、(100+99)+(102+101+100)=502 です。
「満足度」を 503 以上にする方法はないので、502 が答えになります。
入力例 2
2 2021 2 3
出力例 2
9
アトラクションに乗る合計回数は、K 回より少なくても構いません。
Score : 500 points
Problem Statement
Takahashi has come to an amusement park.
The park has N attractions. The fun of the i-th attraction is initially a_i.
When Takahashi rides the i-th attraction, the following sequence of events happens.
- Takahashi's satisfaction increases by the current fun of the i-th attraction.
- Then, the fun of the i-th attraction decreases by 1.
Takahashi's satisfaction is initially 0. He can ride the attractions at most K times in total in any order.
What is the maximum possible value of satisfaction Takahashi can end up with?
Other than riding the attractions, nothing affects Takahashi's satisfaction.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq K \leq 2 \times 10^9
- 1 \leq A_i \leq 2 \times 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the maximum possible value of satisfaction that Takahashi can end up with.
Sample Input 1
3 5 100 50 102
Sample Output 1
502
Takahashi should ride the first attraction twice and the third attraction three times.
He will end up with the satisfaction of (100+99)+(102+101+100)=502.
There is no way to get the satisfaction of 503 or more, so the answer is 502.
Sample Input 2
2 2021 2 3
Sample Output 2
9
Takahashi may choose to ride the attractions fewer than K times in total.
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる有向グラフがあります。各辺には美しさとコストの 2 つの正整数値が定められています。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i から頂点 v_i への有向辺であり、その美しさは b_i 、コストは c_i です。 ここで、u_i \lt v_i が制約として保証されます。
頂点 1 から頂点 N へのパス P を 1 つ選んだときの、下記の値としてあり得る最大値を求めてください。
- P 上のすべての辺の美しさの総和を、P 上のすべての辺のコストの総和で割った値
ここで、与えられるグラフにおいて頂点 1 から頂点 N へのパスが 1 個以上存在することが制約として保証されます。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- 頂点 1 から頂点 N へのパスが存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
出力
答えを出力せよ。出力された値と真の値の相対誤差もしくは絶対誤差が 10^{-9} 以下のとき、正答と判定される。
入力例 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
出力例 1
0.7500000000000000
パス P として、 2, 6, 7 番目の辺をこの順に通り頂点 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 とたどるパスを選んだとき、「 P 上のすべての辺の美しさの総和を P 上のすべての辺のコストの総和で割った値」 は、 (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75 であり、これがあり得る最大値です。
入力例 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
出力例 2
3.0000000000000000
入力例 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
出力例 3
1.8333333333333333
Score : 500 points
Problem Statement
There is a directed graph with N vertices and M edges. Each edge has two positive integer values: beauty and cost.
For i = 1, 2, \ldots, M, the i-th edge is directed from vertex u_i to vertex v_i, with beauty b_i and cost c_i. Here, the constraints guarantee that u_i \lt v_i.
Find the maximum value of the following for a path P from vertex 1 to vertex N.
- The total beauty of all edges on P divided by the total cost of all edges on P.
Here, the constraints guarantee that the given graph has at least one path from vertex 1 to vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- There is a path from vertex 1 to vertex N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
Output
Print the answer. Your output will be judged as correct if the relative or absolute error from the true answer is at most 10^{-9}.
Sample Input 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
Sample Output 1
0.7500000000000000
For the path P that passes through the 2-nd, 6-th, and 7-th edges in this order and visits vertices 1 \rightarrow 3 \rightarrow 4 \rightarrow 5, the total beauty of all edges on P divided by the total cost of all edges on P is (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75, and this is the maximum possible value.
Sample Input 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
Sample Output 2
3.0000000000000000
Sample Input 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
Sample Output 3
1.8333333333333333