実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ある都にて、東軍と西軍が戦っています。
高橋君は戦っている軍勢の情報を文字列 S として記録しました。
S には文字 E と W のみが含まれており、 S に含まれる文字 E の数と東軍の人数、 W の数と西軍の人数がそれぞれ一致します。
高橋君が記録した S が与えられるので、東軍の人数が西軍の人数より多いなら East 、西軍の人数が東軍の人数より多いなら West と出力してください。
なお、 S の長さは奇数であることが保証されます。そのため、東軍と西軍のどちらか一方がもう一方に比べて真に人数が多い (つまり、両方の軍勢の人数が同じであることはない) ことが証明できます。
制約
- S は長さ 1 以上 99 以下の
EとWからなる文字列 - S の長さは奇数
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
EEWEW
出力例 1
East
S に E が 3 個、 W が 2 個含まれるため、東軍 3 人に対して西軍 2 人です。
東軍の人数が西軍の人数より多いので East と出力してください。
入力例 2
WWWWWWW
出力例 2
West
S に E が 0 個、 W が 7 個含まれるため、東軍 0 人に対して西軍 7 人です。
西軍の人数が東軍の人数より多いので West と出力してください。
Score : 100 points
Problem Statement
The Eastern Army and the Western Army are at war in a certain capital.
Takahashi recorded information about the fighting forces as a string S.
S contains only the characters E and W, and the number of Es in S equals the number of soldiers in the Eastern Army, and the number of Ws equals the number of soldiers in the Western Army.
Given the string S that Takahashi recorded, output East if the Eastern Army has more soldiers than the Western Army, or West if the Western Army has more soldiers than the Eastern Army.
The length of S is guaranteed to be odd. Thus, it can be proved that exactly one of the two armies strictly outnumbers the other (that is, the two armies cannot have the same number of soldiers).
Constraints
- S is a string of length between 1 and 99, inclusive, consisting of
EandW. - The length of S is odd.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
EEWEW
Sample Output 1
East
S contains three Es and two Ws, so the Eastern Army has three soldiers and the Western Army has two soldiers.
Since the Eastern Army has more soldiers than the Western Army, output East.
Sample Input 2
WWWWWWW
Sample Output 2
West
S contains zero Es and seven Ws, so the Eastern Army has zero soldiers and the Western Army has seven soldiers.
Since the Western Army has more soldiers than the Eastern Army, output West.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。
S と T のハミング距離を求めてください。 すなわち、1 以上 N 以下の整数 i であって、S の i 文字目と T の i 文字目が異なるようなものの個数を求めてください。
制約
- 1\leq N \leq 100
- N は整数
- S,T はそれぞれ英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
答えを出力せよ。
入力例 1
6 abcarc agcahc
出力例 1
2
S と T は 2,5 文字目が異なり、それ以外が等しいです。よって答えは 2 です。
入力例 2
7 atcoder contest
出力例 2
7
入力例 3
8 chokudai chokudai
出力例 3
0
入力例 4
10 vexknuampx vzxikuamlx
出力例 4
4
Score : 100 points
Problem Statement
You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters.
Find the Hamming distance between S and T. That is, find the number of integers i such that 1 \leq i \leq N and the i-th character of S is different from the i-th character of T.
Constraints
- 1\leq N \leq 100
- N is an integer.
- Each of S and T is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print the answer.
Sample Input 1
6 abcarc agcahc
Sample Output 1
2
S and T differ in the 2nd and 5th characters, but not in other characters. Thus, the answer is 2.
Sample Input 2
7 atcoder contest
Sample Output 2
7
Sample Input 3
8 chokudai chokudai
Sample Output 3
0
Sample Input 4
10 vexknuampx vzxikuamlx
Sample Output 4
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 L,R と、英小文字のみからなる文字列 S が与えられます。
S の L 文字目から R 文字目までの部分を反転した(すなわち、 L 文字目から R 文字目までの文字の並びを逆にした)文字列を出力してください。
制約
- S は英小文字のみからなる。
- 1 \le |S| \le 10^5 (|S| は S の長さ)
- L,R は整数
- 1 \le L \le R \le |S|
入力
入力は以下の形式で標準入力から与えられる。
L R S
出力
問題文の指示通り出力せよ。
入力例 1
3 7 abcdefgh
出力例 1
abgfedch
abcdefgh の 3 文字目から 7 文字目までの部分を反転すると、 abgfedch となります。
入力例 2
1 7 reviver
出力例 2
reviver
操作を行った結果が元の文字列と同一であることもあります。
入力例 3
4 13 merrychristmas
出力例 3
meramtsirhcyrs
Score : 200 points
Problem Statement
You are given integers L, R, and a string S consisting of lowercase English letters.
Print this string after reversing (the order of) the L-th through R-th characters.
Constraints
- S consists of lowercase English letters.
- 1 \le |S| \le 10^5 (|S| is the length of S.)
- L and R are integers.
- 1 \le L \le R \le |S|
Input
Input is given from Standard Input in the following format:
L R S
Output
Print the specified string.
Sample Input 1
3 7 abcdefgh
Sample Output 1
abgfedch
After reversing the 3-rd through 7-th characters of abcdefgh, we have abgfedch.
Sample Input 2
1 7 reviver
Sample Output 2
reviver
The operation may result in the same string as the original.
Sample Input 3
4 13 merrychristmas
Sample Output 3
meramtsirhcyrs
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 N と長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
クエリが Q 個与えられるので、与えられた順番に処理してください。 クエリは次の 2 種類のいずれかです。
1 k x: A _ k の値を x に変更する。2 k: A _ k の値を出力する。
制約
- 1 \leq N \leq 10 ^ 5
- 1 \leq Q \leq 10 ^ 5
- 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
- どのクエリについても、1\leq k\leq N
- 1 番目の形式のクエリについて、0\leq x\leq 10 ^ 9
- 2 番目の形式のクエリが 1 つ以上存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A _ 1 A _ 2 \ldots A _ N
Q
\operatorname{query} _ 1
\operatorname{query} _ 2
\vdots
\operatorname{query} _ Q
ただし、\operatorname{query} _ i は i 個目のクエリを表しており、次の形式のいずれかで与えられる。
1 k x
2 k
出力
2 番目の形式のクエリの回数を q 回として q 行出力せよ。 j\ (1\leq j\leq q) 行目には、2 番目の形式のクエリのうち j 個目のものに対する答えを出力せよ。
入力例 1
3 1 3 5 7 2 2 2 3 1 3 0 2 3 1 2 8 2 2 2 1
出力例 1
3 5 0 8 1
はじめ、A=(1,3,5) です。
- 1 つめのクエリにおいて、A=(1,3,5) です。A _ 2=3 なので、3 を出力します。
- 2 つめのクエリにおいて、A=(1,3,5) です。A _ 3=5 なので、5 を出力します。
- 3 つめのクエリでは、A _ 3 の値を 0 に変更し、A=(1,3,0) となります。
- 4 つめのクエリにおいて、A=(1,3,0) です。A _ 3=0 なので、0 を出力します。
- 5 つめのクエリでは、A _ 2 の値を 8 に変更し、A=(1,8,0) となります。
- 6 つめのクエリにおいて、A=(1,8,0) です。A _ 2=8 なので、8 を出力します。
- 7 つめのクエリにおいて、A=(1,8,0) です。A _ 1=1 なので、1 を出力します。
入力例 2
5 22 2 16 7 30 10 1 4 0 1 5 0 2 2 2 3 2 4 2 5 1 4 100 1 5 100 2 3 2 4
出力例 2
2 16 0 0 16 100
入力例 3
7 478 369 466 343 541 42 165 20 2 1 1 7 729 1 6 61 1 6 838 1 3 319 1 4 317 2 4 1 1 673 1 3 176 1 5 250 1 1 468 2 6 1 7 478 1 5 595 2 6 1 6 599 1 6 505 2 3 2 5 2 1
出力例 3
478 317 838 838 176 595 468
Score : 200 points
Problem Statement
You are given an integer N and a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
Given Q queries, process them in the given order. Each query is of one of the following two kinds:
1 k x: set the value A _ k to x.2 k: print the value A _ k.
Constraints
- 1 \leq N \leq 10 ^ 5
- 1 \leq Q \leq 10 ^ 5
- 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
- 1\leq k\leq N for all queries.
- 0\leq x\leq 10 ^ 9 for all queries of the first kind.
- There is at least one query of the second kind.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
A _ 1 A _ 2 \ldots A _ N
Q
\operatorname{query} _ 1
\operatorname{query} _ 2
\vdots
\operatorname{query} _ Q
Here, \operatorname{query} _ i denotes the i-th query, given in one of the following formats:
1 k x
2 k
Output
Print q lines, where q is the number of queries of the second kind. The j-th (1\leq j\leq q) line should contain the response to the j-th such query.
Sample Input 1
3 1 3 5 7 2 2 2 3 1 3 0 2 3 1 2 8 2 2 2 1
Sample Output 1
3 5 0 8 1
Initially, A=(1,3,5).
- For the 1-st query, A=(1,3,5), where A _ 2=3, so 3 should be printed.
- For the 2-nd query, A=(1,3,5), where A _ 3=5, so 5 should be printed.
- The 3-rd query sets the value A _ 3 to 0, making A=(1,3,0).
- For the 4-th query, A=(1,3,0), where A _ 3=0, so 0 should be printed.
- The 5-th query sets the value A _ 2 to 8, making A=(1,8,0).
- For the 6-th query, A=(1,8,0), where A _ 2=8, so 8 should be printed.
- For the 7-th query, A=(1,8,0), where A _ 1=1, so 1 should be printed.
Sample Input 2
5 22 2 16 7 30 10 1 4 0 1 5 0 2 2 2 3 2 4 2 5 1 4 100 1 5 100 2 3 2 4
Sample Output 2
2 16 0 0 16 100
Sample Input 3
7 478 369 466 343 541 42 165 20 2 1 1 7 729 1 6 61 1 6 838 1 3 319 1 4 317 2 4 1 1 673 1 3 176 1 5 250 1 1 468 2 6 1 7 478 1 5 595 2 6 1 6 599 1 6 505 2 3 2 5 2 1
Sample Output 3
478 317 838 838 176 595 468
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
あるイベントには N 人が参加し、i 番目の人の交通費は A_i 円でした。
イベントの主催者である高橋くんは、交通費補助額の上限額 x を設定して、人 i には交通費補助額として \min(x,A_i) 円を支給することとしました。ここで x は非負整数である必要があります。
高橋くんの予算が M 円であり、N 人に渡す交通費補助額の総和を M 円以下にしたいとき、交通費補助額の上限額 x は最大でいくらにできますか?
ただし、交通費補助額の上限額を無限に大きくできる場合は代わりにそのことを報告してください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq 2\times 10^{14}
- 1\leq A_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1 A_2 \ldots A_{N}
出力
予算の条件を満たすときの交通費補助額の上限額 x の最大値を整数として出力せよ。
ただし、交通費補助額の上限額を無限に大きくできる場合は代わりに infinite と出力せよ。
入力例 1
4 8 1 3 2 4
出力例 1
2
交通費補助額の上限額を 2 円にすると、N 人に渡す交通費補助額の総和は \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 円となり、予算の 8 円以下となります。
交通費補助額の上限額を 3 円にすると、N 人に渡す交通費補助額の総和は \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 円となり、予算の 8 円を超えてしまいます。
よって、交通費補助額の上限額の最大値は 2 円となります。
入力例 2
3 20 5 3 2
出力例 2
infinite
交通費補助額の上限額を無限に大きくできます。
入力例 3
10 23 2 5 6 5 2 1 7 9 7 2
出力例 3
2
Score : 300 points
Problem Statement
There are N people participating in an event, and the transportation cost for the i-th person is A_i yen.
Takahashi, the organizer of the event, decided to set a maximum limit x for the transportation subsidy. The subsidy for person i will be \min(x, A_i) yen. Here, x must be a non-negative integer.
Given that Takahashi's budget is M yen, and he wants the total transportation subsidy for all N people to be at most M yen, what is the maximum possible value of the subsidy limit x?
If the subsidy limit can be made infinitely large, report that instead.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^{14}
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1 A_2 \ldots A_{N}
Output
Print the maximum value of the subsidy limit x that satisfies the budget condition, as an integer.
If the subsidy limit can be made infinitely large, print infinite instead.
Sample Input 1
4 8 1 3 2 4
Sample Output 1
2
If the subsidy limit is set to 2 yen, the total transportation subsidy for all N people is \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 yen, which is within the budget of 8 yen.
If the subsidy limit is set to 3 yen, the total transportation subsidy for all N people is \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 yen, which exceeds the budget of 8 yen.
Therefore, the maximum possible value of the subsidy limit is 2 yen.
Sample Input 2
3 20 5 3 2
Sample Output 2
infinite
The subsidy limit can be made infinitely large.
Sample Input 3
10 23 2 5 6 5 2 1 7 9 7 2
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は部屋に PC を沢山置こうとしています。そこで最大何台の PC を部屋に置けるか調べるプログラムを書くことにしました。
H 個の長さ W の ., T からなる文字列 S_1,S_2,\ldots,S_H が与えられます。
高橋君は以下の操作を 0 回以上何回でも行うことができます。
- 1\leq i \leq H, 1 \leq j \leq W-1 を満たす整数であって、 S_i の j 番目の文字も j+1 番目の文字も
Tであるようなものを選ぶ。 S_i の j 番目の文字をPで置き換え、S_i の j+1 番目の文字をCで置き換える。
高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を出力してください。
制約
- 1\leq H \leq 100
- 2\leq W \leq 100
- H と W は整数である
- S_i は
.,Tからなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を改行区切りで出力せよ。
解が複数存在する場合、どれを出力しても正答とみなされる。
入力例 1
2 3 TTT T.T
出力例 1
PCT T.T
可能な操作回数の最大値は 1 です。
例えば、 (i,j)=(1,1) として操作を行うと、S_1 が PCT に変化します。
入力例 2
3 5 TTT.. .TTT. TTTTT
出力例 2
PCT.. .PCT. PCTPC
Score : 300 points
Problem Statement
Planning to place many PCs in his room, Takahashi has decided to write a code that finds how many PCs he can place in his room.
You are given H strings S_1,S_2,\ldots,S_H, each of length W, consisting of . and T.
Takahashi may perform the following operation any number of times (possibly zero):
- Choose integers satisfying 1\leq i \leq H and 1 \leq j \leq W-1 such that the j-th and (j+1)-th characters of S_i are both
T. Replace the j-th character of S_i withP, and (j+1)-th withC.
He tries to maximize the number of times he performs the operation. Find possible resulting S_1,S_2,\ldots,S_H.
Constraints
- 1\leq H \leq 100
- 2\leq W \leq 100
- H and W are integers.
- S_i is a string of length W consisting of
.andT.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print a sequence of strings, S_1,S_2,\ldots,S_H, separated by newlines, possibly resulting from maximizing the number of times he performs the operation.
If multiple solutions exist, print any of them.
Sample Input 1
2 3 TTT T.T
Sample Output 1
PCT T.T
He can perform the operation at most once.
For example, an operation with (i,j)=(1,1) makes S_1 PCT.
Sample Input 2
3 5 TTT.. .TTT. TTTTT
Sample Output 2
PCT.. .PCT. PCTPC
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 頂点 M 辺の(単純とは限らない)有向グラフがあり、頂点は頂点 1, 2, \ldots, N と番号付けられています。
i 番目 (1\leq i\leq M) の辺は頂点 U_i から頂点 V_i へ向かう辺で、コストは C_i です。
また、各頂点の出次数は 4 以下です。
次の条件をみたす頂点 v (1\leq v\leq N) をすべて求めてください。
頂点 1 から頂点 v への経路であって、次の条件をともにみたすものが存在する。
- ちょうど L 回辺を通る。このとき、同じ辺を複数回通っても良いが、通るたびに回数にカウントされる。
- 通った辺のコストの総和が S 以上 T 以下である。(同じ辺を複数回通った場合、通るたびに総和に加算されるものとする。)
出次数 とは
頂点 u の出次数は、頂点 u から出て行く辺の数を指します。辺の向かう先が同じ頂点であるような辺であっても重ねて数えられます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 2\times 10^5
- 1\leq L \leq 10
- 1\leq S\leq T \leq 10^9
- 1\leq U_i,V_i\leq N
- 1\leq C_i\leq 10^8
- 各頂点の出次数は高々 4 である。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M L S T U_1 V_1 C_1 U_2 V_2 C_2 \vdots U_M V_M C_M
出力
条件をみたす頂点を 昇順に 空白区切りで出力せよ。
条件をみたす頂点が存在しない場合は空行を出力せよ。
入力例 1
5 8 3 80 100 1 2 20 1 3 70 2 1 30 2 5 10 3 2 10 3 4 30 3 5 20 5 1 70
出力例 1
1 5
与えられるグラフは下図左のようになっています。各辺のコストはその辺の始点のそばに示されています。

このとき、以下のようになります。
- 頂点 1 から頂点 1 への経路について、頂点 1 \to 頂点 2 \to 頂点 5 \to 頂点 1 (上図中央)を考えると、これはちょうど 3 本の辺を通り、通る辺のコストの総和は 20+10+70=100 であるため、条件をみたしています。
- 頂点 1 から頂点 2 への経路であって、条件をみたすような経路は存在しません。ちょうど 3 本の辺を通るような経路としては、頂点 1 \to 頂点 2 \to 頂点 1 \to 頂点 2 が存在しますが、この経路において通る辺のコストの総和は 20+30+20=70 であり特に 80 より小さいため、条件をみたしていません。
- 頂点 1 から頂点 3 への経路であって、条件をみたすような経路は存在しません。ちょうど 3 本の辺を通るような経路としては、頂点 1 \to 頂点 2 \to 頂点 1 \to 頂点 3 が存在しますが、この経路において通る辺のコストの総和は 20+30+70=120 であり特に 100 より大きいため、条件をみたしていません。
- 頂点 1 から頂点 4 への経路であって、条件をみたすような経路は存在しません。
- 頂点 1 から頂点 5 への経路について、頂点 1 \to 頂点 3 \to 頂点 2 \to 頂点 5 (上図右)を考えると、これはちょうど 3 本の辺を通り、通る辺のコストの総和は 70+10+10=90 であるため、条件をみたしています。
よって、1, 5 をこの順に出力します。条件をみたす頂点を昇順に出力する必要があることに注意してください。
入力例 2
10 1 1 1 100 2 3 1
出力例 2
条件をみたす頂点が存在しない場合は空行を出力してください。
入力例 3
2 5 3 1 100 1 1 1 2 2 100 1 2 1 1 2 1 1 2 100
出力例 3
1 2
グラフは自己ループや多重辺を含む可能性があります。
なお、このテストケースにおけるグラフの頂点 1,2 からの出次数はそれぞれ 4,1 です。
Score : 400 points
Problem Statement
There is a directed graph (not necessarily simple) with N vertices and M edges, where the vertices are numbered as vertices 1, 2, \ldots, N.
The i-th (1\leq i\leq M) edge goes from vertex U_i to vertex V_i with cost C_i.
Additionally, the out-degree of each vertex is at most 4.
Find all vertices v (1\leq v\leq N) that satisfy the following condition:
There exists a path from vertex 1 to vertex v that satisfies both of the following conditions:
- It traverses exactly L edges. If the same edge is traversed multiple times, each traversal is counted.
- The sum of the costs of the traversed edges is at least S and at most T. (If the same edge is traversed multiple times, the cost is added to the sum each time.)
What is out-degree?
The out-degree of vertex u refers to the number of edges going out from vertex u. Even if multiple edges go to the same vertex, they are counted separately.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 2\times 10^5
- 1\leq L \leq 10
- 1\leq S\leq T \leq 10^9
- 1\leq U_i,V_i\leq N
- 1\leq C_i\leq 10^8
- The out-degree of each vertex is at most 4.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M L S T U_1 V_1 C_1 U_2 V_2 C_2 \vdots U_M V_M C_M
Output
Print the vertices that satisfy the condition in ascending order, separated by spaces.
If there are no vertices that satisfy the condition, print an empty line.
Sample Input 1
5 8 3 80 100 1 2 20 1 3 70 2 1 30 2 5 10 3 2 10 3 4 30 3 5 20 5 1 70
Sample Output 1
1 5
The given graph is as shown in the left figure below. The cost of each edge is shown near the starting vertex of that edge.

Here, the following holds:
- For the path from vertex 1 to vertex 1, consider vertex 1 \to vertex 2 \to vertex 5 \to vertex 1 (center of the figure above). This traverses exactly three edges, and the sum of the costs of the traversed edges is 20+10+70=100, so it satisfies the condition.
- There is no path from vertex 1 to vertex 2 that satisfies the condition. One path that traverses exactly three edges is vertex 1 \to vertex 2 \to vertex 1 \to vertex 2, but the sum of the costs of the edges traversed in this path is 20+30+20=70, which is less than 80, so it does not satisfy the condition.
- There is no path from vertex 1 to vertex 3 that satisfies the condition. One path that traverses exactly three edges is vertex 1 \to vertex 2 \to vertex 1 \to vertex 3, but the sum of the costs of the edges traversed in this path is 20+30+70=120, which is greater than 100, so it does not satisfy the condition.
- There is no path from vertex 1 to vertex 4 that satisfies the condition.
- For the path from vertex 1 to vertex 5, consider vertex 1 \to vertex 3 \to vertex 2 \to vertex 5 (right of the figure above). This traverses exactly three edges, and the sum of the costs of the traversed edges is 70+10+10=90, so it satisfies the condition.
Therefore, print 1, 5 in this order. Note that you need to print those vertices that satisfy the condition in ascending order.
Sample Input 2
10 1 1 1 100 2 3 1
Sample Output 2
If there are no vertices that satisfy the condition, print an empty line.
Sample Input 3
2 5 3 1 100 1 1 1 2 2 100 1 2 1 1 2 1 1 2 100
Sample Output 3
1 2
The graph may contain self-loops and multiple edges.
In this test case, the out-degrees from vertices 1 and 2 are 4 and 1, respectively.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N-1 の整数列 S = (S_1, S_2, \ldots, S_{N-1}) および、「ラッキーナンバー」として M 個の相異なる整数 X_1, X_2, \ldots, X_M が与えられます。
長さ N の整数列 A = (A_1, A_2, \ldots, A_N) であって、次の条件を満たすものを「良い数列」と呼びます。
すべての i = 1, 2, \ldots, N-1 について、A_i + A_{i+1} = S_i が成り立つ。
良い数列 A を 1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数(すなわち、A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace となる 1 以上 N 以下の整数 i の個数)としてあり得る最大値を求めてください。
制約
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10
- -10^9 \leq S_i \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_1 \lt X_2 \lt \cdots \lt X_M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
S_1 S_2 \ldots S_{N-1}
X_1 X_2 \ldots X_M
出力
良い数列 A を 1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数としてありうる最大値を出力せよ。
入力例 1
9 2 2 3 3 4 -4 -7 -4 -1 -1 5
出力例 1
4
良い数列 A として A = (3, -1, 4, -1, 5, -9, 2, -6, 5) を選ぶと、A の要素のうちラッキーナンバーであるものは A_2, A_4, A_5, A_9 の 4 個となり、これが考えられる中で最大です。
入力例 2
20 10 -183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901 -470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398
出力例 2
8
Score : 500 points
Problem Statement
You are given a sequence of N-1 integers S = (S_1, S_2, \ldots, S_{N-1}), and M distinct integers X_1, X_2, \ldots, X_M, which are called lucky numbers.
A sequence of N integers A = (A_1, A_2, \ldots, A_N) satisfying the following condition is called a good sequence.
A_i + A_{i+1} = S_i holds for every i = 1, 2, \ldots, N-1.
Find the maximum possible number of terms that are lucky numbers in a good sequence A, that is, the maximum possible number of integers i between 1 and N such that A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10
- -10^9 \leq S_i \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_1 \lt X_2 \lt \cdots \lt X_M
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
S_1 S_2 \ldots S_{N-1}
X_1 X_2 \ldots X_M
Output
Print the maximum possible number of terms that are lucky numbers in a good sequence A.
Sample Input 1
9 2 2 3 3 4 -4 -7 -4 -1 -1 5
Sample Output 1
4
A good sequence A = (3, -1, 4, -1, 5, -9, 2, -6, 5) contains four terms that are lucky numbers: A_2, A_4, A_5, A_9, which is the maximum possible count.
Sample Input 2
20 10 -183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901 -470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398
Sample Output 2
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のグリッドがあります。 このグリッドの上から i 行目、左から j 列目にあるマスのことを (i,j) と表記します。
このグリッド上には N 枚のコインが落ちており、i 個目のコインはマス (R_i,C_i) を通ることで拾うことができます。
あなたの目標は、マス (1,1) から始めて下か右に 1 マス移動することを繰り返し、できるだけ多くのコインを拾いながらマス (H,W) まで行くことです。
あなたが拾うことのできるコインの枚数の最大値、およびそれを達成するための移動経路を 1 つ求めてください。
制約
- 2\leq H,W \leq 2\times 10^5
- 1\leq N \leq \min(HW-2, 2\times 10^5)
- 1\leq R_i \leq H
- 1\leq C_i \leq W
- (R_i,C_i)\neq (1,1)
- (R_i,C_i)\neq (H,W)
- (R_i,C_i) は互いに相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
2 行出力せよ。
1 行目には、あなたが拾うことのできるコインの枚数の最大値を出力せよ。
2 行目には、それを達成するための移動経路の 1 つを長さ H+W-2 の文字列として出力せよ。
ここで、出力する文字列の i 文字目は、i 回目の移動において下に移動するならば D、右に移動するならば R である。
拾うコインの枚数が最大となるような移動経路が複数存在する場合は、そのどれを出力しても良い。
入力例 1
3 4 4 3 3 2 1 2 3 1 4
出力例 1
3 DRRDR

上図のように (1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (2,3)\rightarrow (3,3)\rightarrow (3,4) と移動することで、(2,1),(2,3),(3,3) にある 3 枚のコインを拾うことができます。
入力例 2
2 2 2 2 1 1 2
出力例 2
1 DR
RD という移動経路も正解となります。
入力例 3
10 15 8 2 7 2 9 7 9 10 3 7 11 8 12 9 6 8 1
出力例 3
5 DRRRRRRRRDDDDDRRDRDDRRR
Score : 500 points
Problem Statement
There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.
There are N coins on this grid, and the i-th coin can be picked up by passing through the cell (R_i,C_i).
Your goal is to start from cell (1,1), repeatedly move either down or right by one cell, and reach cell (H,W) while picking up as many coins as possible.
Find the maximum number of coins you can pick up and one of the paths that achieves this maximum.
Constraints
- 2\leq H,W \leq 2\times 10^5
- 1\leq N \leq \min(HW-2, 2\times 10^5)
- 1\leq R_i \leq H
- 1\leq C_i \leq W
- (R_i,C_i)\neq (1,1)
- (R_i,C_i)\neq (H,W)
- (R_i,C_i) are pairwise distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Print two lines.
The first line should contain the maximum number of coins you can pick up.
The second line should contain one of the paths that achieves this maximum as a string of length H+W-2.
The i-th character of this string should be D if the i-th move is downward, and R if it is rightward.
If there are multiple paths that maximize the number of coins picked up, you may print any of them.
Sample Input 1
3 4 4 3 3 2 1 2 3 1 4
Sample Output 1
3 DRRDR

As shown in the figure above, by moving (1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (2,3)\rightarrow (3,3)\rightarrow (3,4), you can pick up three coins at (2,1),(2,3),(3,3).
Sample Input 2
2 2 2 2 1 1 2
Sample Output 2
1 DR
The path RD is also acceptable.
Sample Input 3
10 15 8 2 7 2 9 7 9 10 3 7 11 8 12 9 6 8 1
Sample Output 3
5 DRRRRRRRRDDDDDRRDRDDRRR