実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の文字列 S_1,S_2,\ldots,S_N がこの順番で与えられます。
S_N,S_{N-1},\ldots,S_1 の順番で出力してください。
制約
- 1\leq N \leq 10
- N は整数
- S_i は英小文字、英大文字、数字からなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 行出力せよ。 i\ (1\leq i \leq N) 行目には、S_{N+1-i} を出力せよ。
入力例 1
3 Takahashi Aoki Snuke
出力例 1
Snuke Aoki Takahashi
N=3、S_1= Takahashi、S_2= Aoki、S_3= Snuke です。
よって、Snuke、Aoki、Takahashi の順で出力します。
入力例 2
4 2023 Year New Happy
出力例 2
Happy New Year 2023
与えられる文字列が数字を含むこともあります。
Score : 100 points
Problem Statement
You are given N strings S_1,S_2,\ldots,S_N in this order.
Print S_N,S_{N-1},\ldots,S_1 in this order.
Constraints
- 1\leq N \leq 10
- N is an integer.
- S_i is a string of length between 1 and 10, inclusive, consisting of lowercase English letters, uppercase English letters, and digits.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines. The i-th (1\leq i \leq N) line should contain S_{N+1-i}.
Sample Input 1
3 Takahashi Aoki Snuke
Sample Output 1
Snuke Aoki Takahashi
We have N=3, S_1= Takahashi, S_2= Aoki, and S_3= Snuke.
Thus, you should print Snuke, Aoki, and Takahashi in this order.
Sample Input 2
4 2023 Year New Happy
Sample Output 2
Happy New Year 2023
The given strings may contain digits.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 10 以下の整数 X が与えられます。
HelloWorld という文字列から X 文字目だけを削除した文字列を出力してください。
制約
- X は 1 以上 10 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを出力せよ。
入力例 1
5
出力例 1
HellWorld
HelloWorld の 5 文字目を削除すると HellWorld になります。したがって、HellWorld を出力してください。
入力例 2
9
出力例 2
HelloWord
入力例 3
1
出力例 3
elloWorld
Score : 100 points
Problem Statement
You are given an integer X between 1 and 10, inclusive.
Output the string obtained by deleting only the X-th character from the string HelloWorld.
Constraints
- X is an integer between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
X
Output
Output the answer.
Sample Input 1
5
Sample Output 1
HellWorld
Deleting the 5-th character of HelloWorld gives HellWorld. Thus, output HellWorld.
Sample Input 2
9
Sample Output 2
HelloWord
Sample Input 3
1
Sample Output 3
elloWorld
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
ある測定では、時刻 0,1,\dots,T におけるセンサーの測定値を以下の規則で記録します。
- 時刻 0 では、測定値を保存する。
- 時刻 1,2,\dots,T では、「現時刻の測定値」と「直前に保存された測定値」との差の絶対値が X 以上であるとき、またその時に限り値を保存する。
時刻 i=0,1,\dots,T におけるセンサーの測定値は A_i でした。
測定値が保存された時刻と保存された値とを、時刻の昇順に出力してください。
制約
- 1 \le T \le 100
- 1 \le X \le 100
- 0 \le A_i \le 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T X A_0 A_1 \dots A_T
出力
測定値が k 回保存され、そのうち時刻の昇順に並べた時に i 回目の時刻が t_i 、測定値が a_i であったとき、以下の形式で出力せよ。
t_1 a_1 t_2 a_2 \vdots t_k a_k
入力例 1
6 10 30 35 40 21 30 12 31
出力例 1
0 30 2 40 3 21 6 31
測定は以下の流れで進行します。
- 時刻 0 の測定値は 30 であった。これを保存する。
- 時刻 1 の測定値は 35 であった。最後に保存された測定値は 30 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 2 の測定値は 40 であった。最後に保存された測定値は 30 であり、この値との差の絶対値が 10 以上であるため、保存する。
- 時刻 3 の測定値は 21 であった。最後に保存された測定値は 40 であり、この値との差の絶対値が 10 以上であるため、保存する。
- 時刻 4 の測定値は 30 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 5 の測定値は 12 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 6 の測定値は 31 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 以上であるため、保存する。
Score : 200 points
Problem Statement
In a certain measurement, the sensor readings at times 0,1,\dots,T are recorded according to the following rules.
- At time 0, the reading is saved.
- At times 1,2,\dots,T, the reading is saved if and only if the absolute difference between the current reading and the most recently saved reading is at least X.
The sensor reading at time i=0,1,\dots,T was A_i.
Output the times at which readings were saved and the saved values, in ascending order of time.
Constraints
- 1 \le T \le 100
- 1 \le X \le 100
- 0 \le A_i \le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T X A_0 A_1 \dots A_T
Output
If k readings were saved, and the i-th saved reading in ascending order of time was at time t_i with value a_i, output in the following format:
t_1 a_1 t_2 a_2 \vdots t_k a_k
Sample Input 1
6 10 30 35 40 21 30 12 31
Sample Output 1
0 30 2 40 3 21 6 31
The measurement proceeds as follows.
- The reading at time 0 is 30. Save it.
- The reading at time 1 is 35. The most recently saved reading is 30, and the absolute difference is less than 10, so it is not saved.
- The reading at time 2 is 40. The most recently saved reading is 30, and the absolute difference is at least 10, so it is saved.
- The reading at time 3 is 21. The most recently saved reading is 40, and the absolute difference is at least 10, so it is saved.
- The reading at time 4 is 30. The most recently saved reading is 21, and the absolute difference is less than 10, so it is not saved.
- The reading at time 5 is 12. The most recently saved reading is 21, and the absolute difference is less than 10, so it is not saved.
- The reading at time 6 is 31. The most recently saved reading is 21, and the absolute difference is at least 10, so it is saved.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 個の宝石があります。i 番目の宝石の色(整数で表されます)は C_i で価値は V_i です。
この N 個の宝石の中から K 個を選びます。ただし、選んだ宝石の色が M 種類以上なければなりません。
このとき、選んだ宝石の価値の総和としてありうる最大値を求めてください。(与えられる入力では、このような選択が必ず可能です。)
制約
- 1 \leq M \leq K \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq N
- 1 \leq V_i \leq 10^9
- M 種類以上の色の宝石が存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K M C_1 V_1 C_2 V_2 \vdots C_N V_N
出力
選んだ宝石の価値の総和としてありうる最大値を整数として出力せよ。
入力例 1
5 3 2 1 30 1 40 1 50 2 10 3 20
出力例 1
110
この例では 5 個の宝石から 3 個を選びます。選んだ宝石の色が 2 種類以上なければなりません。
宝石 2, 3, 5 を選ぶと、それらの色は 1, 1, 3 で 2 種類あります。それらの価値の総和は 40 + 50 + 20 = 110 で、これがありうる最大値です。
入力例 2
5 3 3 1 30 1 40 1 50 2 10 3 20
出力例 2
80
宝石や選ぶ個数は入力例 1 と同じですが、選んだ宝石の色が 3 種類以上なければなりません。
宝石 3, 4, 5 を選ぶと、それらの色は 1, 2, 3 で 3 種類あります。それらの価値の総和は 50 + 10 + 20 = 80 で、これがありうる最大値です。
入力例 3
5 5 1 4 1000000000 5 1000000000 4 1000000000 5 1000000000 4 1000000000
出力例 3
5000000000
オーバーフローに注意してください。
Score : 300 points
Problem Statement
There are N gems. The color (represented as an integer) of the i-th gem is C_i and its value is V_i.
Choose K gems from these N gems. Here, the chosen gems must have at least M distinct colors.
Find the maximum possible total value of the chosen gems. (Such a choice is always possible in the given input.)
Constraints
- 1 \leq M \leq K \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq N
- 1 \leq V_i \leq 10^9
- There exist gems of at least M distinct colors.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K M C_1 V_1 C_2 V_2 \vdots C_N V_N
Output
Output the maximum possible total value of the chosen gems as an integer.
Sample Input 1
5 3 2 1 30 1 40 1 50 2 10 3 20
Sample Output 1
110
In this sample, choose three gems from five gems. The chosen gems must have at least two distinct colors.
Choosing gems 2, 3, 5 gives colors 1, 1, 3, which are two distinct colors. Their total value is 40 + 50 + 20 = 110, and this is the maximum possible value.
Sample Input 2
5 3 3 1 30 1 40 1 50 2 10 3 20
Sample Output 2
80
The gems and the number to choose are the same as in Sample Input 1, but the chosen gems must have at least three distinct colors.
Choosing gems 3, 4, 5 gives colors 1, 2, 3, which are three distinct colors. Their total value is 50 + 10 + 20 = 80, and this is the maximum possible value.
Sample Input 3
5 5 1 4 1000000000 5 1000000000 4 1000000000 5 1000000000 4 1000000000
Sample Output 3
5000000000
Beware of overflow.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoder レストランは開店してから N 日間営業しました。
開店してから i 日目 (1\leq i\leq N) には次の行動が行われました。
- i 日目の朝に、A_i 個の卵を仕入れる。
- i 日目の昼に、B_i 個の卵を使用する。このとき、卵は 在庫にある卵の中で仕入れた順に使用される 。
- i 日目の夜に、仕入れてから D 日間以上経過した卵をすべて処分する。
なお、1 日目の朝の前の時点で卵の在庫は無く、それぞれの日の昼に卵が足りなくなることはありませんでした。
N 日目の夜の行動の後で、レストランに何個の卵が残っているか求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T\leq 2\times 10^5
- 1 \leq D \leq N \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10
- N 日間のそれぞれの昼において、卵が足りなくなることはない。
- それぞれの入力において、各テストケースにおける N の総和は 2\times 10^5 以下である。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 個目のテストケースを表す。
各テストケースは以下の形式で与えられる。
N D A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースに対する答えを出力せよ。
入力例 1
3 3 1 7 2 3 1 3 2 3 2 7 2 3 1 3 2 2 1 2 1 1 2
出力例 1
3 5 0
1 個目のテストケースにおいて次の行動が行われます。
- 最初、AtCoder レストランには卵がありません。
- 1 日目の朝に卵を 7 個仕入れます。レストランには 1 日目に仕入れた卵が 7 個あります。
- 1 日目の昼に卵を 1 個使用します。レストランには 1 日目に仕入れた卵が 6 個残っています。
- 1 日目の夜に処分する卵はありません。レストランには 1 日目に仕入れた卵が 6 個残っています。
- 2 日目の朝に卵を 2 個仕入れます。レストランには 1 日目に仕入れた卵が 6 個、2 日目に仕入れた卵が 2 個あります。
- 2 日目の昼に卵を 3 個使用します。レストランには 1 日目に仕入れた卵が 3 個、2 日目に仕入れた卵が 2 個残っています。
- 2 日目の夜に、1 日目に仕入れた卵を処分します。レストランには 2 日目に仕入れた卵が 2 個残っています。
- 3 日目の朝に卵を 3 個仕入れます。レストランには 2 日目に仕入れた卵が 2 個、3 日目に仕入れた卵が 3 個あります。
- 3 日目の昼に卵を 2 個使用します。レストランには 3 日目に仕入れた卵が 3 個残っています。
- 3 日目の夜に処分する卵はありません。(2 日目に仕入れた卵は全て使用されているためです。)レストランには 3 日目に仕入れた卵が 3 個残っています。
よって、3 日目の夜の行動の後で、卵は 3 個残っているため、1 行目には 3 を出力します。
2 個目のテストケースにおいては、3 日目の夜に 1 日目に仕入れた卵を処分した後の個数を出力することに注意してください。
Score : 300 points
Problem Statement
AtCoder Restaurant was open for N days after opening.
On day i (1\leq i\leq N) after opening, the following actions were performed.
- In the morning of day i, A_i eggs are purchased.
- At noon on day i, B_i eggs are used. Here, eggs in stock are used in the order they were purchased.
- In the evening of day i, all eggs that have been stocked for D or more days are discarded.
There were no eggs in stock before the morning of day 1, and eggs never ran out at noon on any day.
Find how many eggs remain in the restaurant after the evening action on day N.
T test cases are given; solve each.
Constraints
- 1 \leq T\leq 2\times 10^5
- 1 \leq D \leq N \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10
- Eggs never run out at noon on any of the N days.
- For each input, the sum of N over all test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case.
Each test case is given in the following format:
N D A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Output T lines.
The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
Sample Input 1
3 3 1 7 2 3 1 3 2 3 2 7 2 3 1 3 2 2 1 2 1 1 2
Sample Output 1
3 5 0
In the first test case, the following actions are performed.
- Initially, AtCoder Restaurant has no eggs.
- In the morning of day 1, 7 eggs are purchased. The restaurant has 7 eggs stocked on day 1.
- At noon on day 1, 1 egg is used. The restaurant has 6 eggs stocked on day 1 remaining.
- In the evening of day 1, no eggs are discarded. The restaurant has 6 eggs stocked on day 1 remaining.
- In the morning of day 2, 2 eggs are purchased. The restaurant has 6 eggs stocked on day 1 and 2 eggs stocked on day 2.
- At noon on day 2, 3 eggs are used. The restaurant has 3 eggs stocked on day 1 and 2 eggs stocked on day 2 remaining.
- In the evening of day 2, the eggs stocked on day 1 are discarded. The restaurant has 2 eggs stocked on day 2 remaining.
- In the morning of day 3, 3 eggs are purchased. The restaurant has 2 eggs stocked on day 2 and 3 eggs stocked on day 3.
- At noon on day 3, 2 eggs are used. The restaurant has 3 eggs stocked on day 3 remaining.
- In the evening of day 3, no eggs are discarded. (This is because all eggs stocked on day 2 have already been used.) The restaurant has 3 eggs stocked on day 3 remaining.
Thus, 3 eggs remain after the evening action on day 3, so output 3 on the first line.
For the second test case, remember to output the number of eggs after discarding the eggs stocked on day 1 in the evening of day 3.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。
- 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- 頂点 1 と頂点 N_1+N_2 は非連結
次の操作をちょうど 1 回行います。
- 1 \leq u \leq N_1 を満たす整数 u と N_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する
操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。
操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。
連結とは?
無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。制約
- 1 \leq N_1,N_2 \leq 1.5 \times 10^5
- 0 \leq M \leq 3 \times 10^5
- 1 \leq a_i \leq b_i \leq N_1+N_2
- i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
- 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- 頂点 1 と頂点 N_1+N_2 は非連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N_1 N_2 M a_1 b_1 \vdots a_M b_M
出力
答えを出力せよ。
入力例 1
3 4 6 1 2 2 3 4 5 4 6 1 3 6 7
出力例 1
5
u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。

入力例 2
7 5 20 10 11 4 5 10 12 1 2 1 5 5 6 2 4 3 5 9 10 2 5 1 4 11 12 9 12 8 9 5 7 3 7 3 6 3 4 8 12 9 11
出力例 2
4
Score : 400 points
Problem Statement
We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:
- Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
- Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
- Vertex 1 and vertex (N_1+N_2) are disconnected.
Consider performing the following operation exactly once:
- choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.
We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).
Find the maximum possible d resulting from adding an appropriate edge to add.
Definition of "connected"
Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.Constraints
- 1 \leq N_1,N_2 \leq 1.5 \times 10^5
- 0 \leq M \leq 3 \times 10^5
- 1 \leq a_i \leq b_i \leq N_1+N_2
- (a_i,b_i) \neq (a_j,b_j) if i \neq j.
- Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
- Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
- Vertex 1 and vertex (N_1+N_2) are disconnected.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N_1 N_2 M a_1 b_1 \vdots a_M b_M
Output
Print the answer.
Sample Input 1
3 4 6 1 2 2 3 4 5 4 6 1 3 6 7
Sample Output 1
5
If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.

Sample Input 2
7 5 20 10 11 4 5 10 12 1 2 1 5 5 6 2 4 3 5 9 10 2 5 1 4 11 12 9 12 8 9 5 7 3 7 3 6 3 4 8 12 9 11
Sample Output 2
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
各要素の値が 0 または 1 である H 行 W 列の行列 A が与えられます。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i,j) について、A の i 行目 j 列目の要素を A_{i,j} で表します。
行列 A に対し、以下の操作を 0 回以上の好きな回数行うことができます。
- 1 \leq i \leq H を満たす整数 i を選び、1 \leq j \leq W を満たす全ての整数 j に対して A_{i,j} の値を 1-A_{i,j} で置き換える。
また、A_{i,j} は行列において上下左右に同じ値が存在しない、すなわち 4 つの整数組 (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) のいずれかであって、 1 \leq x \leq H, 1 \leq y \leq W かつ A_{i,j} = A_{x,y} を満たすものが存在しないとき、またそのときに限り孤立した要素であると定義されます。
操作を繰り返し行列 A の任意の要素が孤立した要素でない状態にすることが可能か判定し、可能な場合は行う操作回数の最小値を求めてください。
制約
- 2 \leq H,W \leq 1000
- A_{i,j} = 0 または A_{i,j} = 1
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
出力
操作を繰り返すことにより孤立した要素が存在しないようにできる場合は操作回数の最小値を、できない場合は -1 を出力せよ。
入力例 1
3 3 1 1 0 1 0 1 1 0 0
出力例 1
1
i = 1 を選択し操作を行うと、A = ((0,0,1),(1,0,1),(1,0,0)) となり、孤立した要素は存在しなくなります。
入力例 2
4 4 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1
出力例 2
2
入力例 3
2 3 0 1 0 0 1 1
出力例 3
-1
Score : 500 points
Problem Statement
You are given a matrix A with H rows and W columns. The value of each of its elements is 0 or 1. For an integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, we denote by A_{i,j} the element at the i-th row and j-th column.
You can perform the following operation on the matrix A any number of times (possibly zero):
- Choose an integer i such that 1 \leq i \leq H. For every integer j such that 1 \leq j \leq W, replace the value of A_{i,j} with 1-A_{i,j}.
A_{i,j} is said to be isolated if and only if there is no adjacent element with the same value; in other words, if and only if none of the four integer pairs (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) satisfies 1 \leq x \leq H, 1 \leq y \leq W, and A_{i,j} = A_{x,y}.
Determine if you can make the matrix A in such a state that no element is isolated by repeating the operation. If it is possible, find the minimum number of operations required to do so.
Constraints
- 2 \leq H,W \leq 1000
- A_{i,j} = 0 or A_{i,j} = 1
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
Output
If you can make it in such a state that no element is isolated by repeating the operation, print the minimum number of operations required to do so; otherwise, print -1.
Sample Input 1
3 3 1 1 0 1 0 1 1 0 0
Sample Output 1
1
An operation with i = 1 makes A = ((0,0,1),(1,0,1),(1,0,0)), where there is no longer an isolated element.
Sample Input 2
4 4 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1
Sample Output 2
2
Sample Input 3
2 3 0 1 0 0 1 1
Sample Output 3
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1 から N までの番号がつけられた N 個の足場が一列に並んでいます。足場 i(1 \leq i \leq N) の高さは H_i です。
高橋君は足場に乗って移動する遊びをすることにしました。 最初、高橋君は整数 i(1 \leq i \leq N) を自由に選び、足場 i に乗ります。
高橋君はある時点で足場 i に乗っている時、以下の条件を満たす整数 j(1 \leq j \leq N) を選び足場 j に移動することができます。
- H_j \leq H_i - D かつ 1 \leq |i-j| \leq R
高橋君は移動を行えなくなるまで移動を繰り返すとき、移動できる回数の最大値を求めてください。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq D,R \leq N
- H は (1,2,\ldots,N) の順列
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D R H_1 H_2 \ldots H_N
出力
答えを出力せよ。
入力例 1
5 2 1 5 3 1 4 2
出力例 1
2
高橋君は初めに足場 1 に乗り、以下のように足場を移動することができます。
- 1 回目の移動: H_2 \leq H_1 -D かつ |2-1| \leq R であるため足場 2 に移動することができる。足場 1 から足場 2 に移動する。
- 2 回目の移動: H_3 \leq H_2 -D かつ |3-2| \leq R であるため足場 3 に移動することができる。足場 2 から足場 3 に移動する。
- 足場 3 の高さは 1 であるため、これ以上移動できない。
以上のように 2 回移動することができます。また、どのように移動する足場を選んでも 3 回以上移動することはできません。よって、2 を出力します。
入力例 2
13 3 2 13 7 10 1 9 5 4 11 12 2 8 6 3
出力例 2
3
Score : 500 points
Problem Statement
There are N scaffolds numbered from 1 to N arranged in a line. The height of scaffold i(1 \leq i \leq N) is H_i.
Takahashi decides to play a game of moving on the scaffolds. Initially, he freely chooses an integer i(1 \leq i \leq N) and gets on scaffold i.
When he is on scaffold i at some point, he can choose an integer j(1 \leq j \leq N) satisfying the following condition and move to scaffold j:
- H_j \leq H_i - D and 1 \leq |i-j| \leq R.
Find the maximum number of moves he can make when he repeats moving until he can no longer move.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq D,R \leq N
- H is a permutation of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D R H_1 H_2 \ldots H_N
Output
Output the answer.
Sample Input 1
5 2 1 5 3 1 4 2
Sample Output 1
2
Takahashi initially gets on scaffold 1 and can move between the scaffolds as follows:
- First move: Since H_2 \leq H_1 -D and |2-1| \leq R, he can move to scaffold 2. Move from scaffold 1 to scaffold 2.
- Second move: Since H_3 \leq H_2 -D and |3-2| \leq R, he can move to scaffold 3. Move from scaffold 2 to scaffold 3.
- Since the height of scaffold 3 is 1, he can no longer move.
As shown above, he can move 2 times. Also, no matter how he chooses the scaffolds to move to, he cannot move 3 or more times. Therefore, output 2.
Sample Input 2
13 3 2 13 7 10 1 9 5 4 11 12 2 8 6 3
Sample Output 2
3