E - Adjacent Swaps

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。

高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。

  • 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。

操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \leq N
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
x_1
\vdots
x_Q

出力

a_1,\ldots,a_N を空白区切りで出力せよ。


入力例 1

5 5
1
2
3
4
5

出力例 1

1 2 3 5 4

操作は以下のように行われます。

  • 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
  • 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
  • 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。

入力例 2

7 7
7
7
7
7
7
7
7

出力例 2

1 2 3 4 5 7 6

入力例 3

10 6
1
5
2
9
6
6

出力例 3

1 2 3 4 5 7 6 8 10 9

Score : 300 points

Problem Statement

N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.

Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.

  • Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.

Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
x_1
\vdots
x_Q

Output

Print a_1,\ldots,a_N, with spaces in between.


Sample Input 1

5 5
1
2
3
4
5

Sample Output 1

1 2 3 5 4

The operations are performed as follows.

  • Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
  • Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
  • Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.

Sample Input 2

7 7
7
7
7
7
7
7
7

Sample Output 2

1 2 3 4 5 7 6

Sample Input 3

10 6
1
5
2
9
6
6

Sample Output 3

1 2 3 4 5 7 6 8 10 9
F - Socks 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さ|i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。

残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。

制約

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \dots A_K

出力

奇妙さの総和の最小値を整数として出力せよ。


入力例 1

4 2
1 3

出力例 1

2

以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。

1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4)3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。


入力例 2

5 1
2

出力例 2

0

(1,1),(3,3),(4,4),(5,5)4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。


入力例 3

8 5
1 2 4 7 8

出力例 3

2

Score : 350 points

Problem Statement

Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.

Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.

Constraints

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_K

Output

Print the minimum total weirdness as an integer.


Sample Input 1

4 2
1 3

Sample Output 1

2

Below, let (i,j) denote a pair of a sock of color i and a sock of color j.

There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.


Sample Input 2

5 1
2

Sample Output 2

0

The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).


Sample Input 3

8 5
1 2 4 7 8

Sample Output 3

2
G - Swap to Gather

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

0 および 1 からなる長さ N の文字列 S が与えられます。 ここで、S には 11 つ以上含まれることが保証されます。

あなたは、以下の操作を繰り返し(0 回でも良い)行うことができます。

  • 1\leq i\leq N-1 を満たす整数 i を選び、Si 文字目と i+1 文字目を入れ替える。

すべての 1 がひとかたまりになった状態にするために必要な操作回数の最小値を求めてください。

なお、すべての 1 がひとかたまりになっているとは、ある整数 l,r\ (1\leq l\leq r\leq N) が存在し、 Si 文字目は l\leq i\leq r ならば 1、そうでないならば 0 であることをいいます。

制約

  • 2\leq N\leq 5\times 10^5
  • N は整数
  • S0 および 1 からなる長さ N の文字列
  • S には 11 つ以上含まれる

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

答えを出力せよ。


入力例 1

7
0101001

出力例 1

3

例えば、以下のように操作を 3 回行うと、すべての 1 がひとかたまりになります。

  • i=2 を選び、S2 文字目と 3 文字目を入れ替える。S= 0011001 になる。
  • i=6 を選び、S6 文字目と 7 文字目を入れ替える。S= 0011010 になる。
  • i=5 を選び、S5 文字目と 6 文字目を入れ替える。S= 0011100 になる。

2 回以下の操作ですべての 1 をひとかたまりにすることはできないため、答えは 3 です。


入力例 2

3
100

出力例 2

0

既にすべての 1 がひとかたまりになっているため、操作は必要ありません。


入力例 3

10
0101001001

出力例 3

7

Score : 425 points

Problem Statement

You are given a string S of length N consisting of 0 and 1. It is guaranteed that S contains at least one 1.

You may perform the following operation any number of times (possibly zero):

  • Choose an integer i (1 \leq i \leq N-1) and swap the i-th and (i+1)-th characters of S.

Find the minimum number of operations needed so that all 1s are contiguous.

Here, all 1s are said to be contiguous if and only if there exist integers l and r (1 \leq l \leq r \leq N) such that the i-th character of S is 1 if and only if l \leq i \leq r, and 0 otherwise.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • N is an integer.
  • S is a length N string of 0 and 1.
  • S contains at least one 1.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the answer.


Sample Input 1

7
0101001

Sample Output 1

3

For example, the following three operations make all 1s contiguous:

  • Choose i=2 and swap the 2nd and 3rd characters. Then, S= 0011001.
  • Choose i=6 and swap the 6th and 7th characters. Then, S= 0011010.
  • Choose i=5 and swap the 5th and 6th characters. Then, S= 0011100.

It is impossible to do this in two or fewer swaps, so the answer is 3.


Sample Input 2

3
100

Sample Output 2

0

All 1s are already contiguous, so no swaps are needed.


Sample Input 3

10
0101001001

Sample Output 3

7
H - How to Win the Election

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1,2,\ldots,N の番号のついた N 人の候補者で選挙が行われています。K 票の投票があり、現在一部の開票作業が行なわれました。

これまでの開票作業では、i 番目の候補者に A_i 票が入りました。

全ての票が開票された後、i 番目 (1 \leq i \leq N) の候補者はその候補者より多く票を獲得した候補者が M 人未満であるとき、かつその時に限り当選します。複数の候補者が当選することもあります。

各候補者が当選を確定させるためには残りの票のうち最小で何票追加で票が必要か求めてください。

より厳密には、i = 1,2,\ldots,N に対して次の問題を解いてください。

次の条件を満たす K - \displaystyle{\sum_{i=1}^{N}} A_i 以下の非負整数 X が存在するか判定し、存在するならその最小値を求めてください。

  • これ以降の開票作業で i 番目の候補者へ X 票入るなら、i 番目の候補者は必ず当選する。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{12}
  • 0 \leq A_i \leq 10^{12}
  • \displaystyle{\sum_{i=1}^{N} A_i} \leq K
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M K
A_1 A_2 \ldots A_N

出力

候補者 i が当選を確定させるために必要な票数を C_i としたとき、C_1,C_2,\ldots,C_N を空白区切りで出力せよ。

ただし、候補者 i がすでに当選を確定させている場合は C_i=0、候補者 i が当選を確定させることができない場合は C_i=-1 とします。


入力例 1

5 2 16
3 1 4 1 5

出力例 1

2 -1 1 -1 0

すでに 14 票の開票が終了しており、残りの票数は 2 票です。
答えるべき C(2, -1, 1, -1, 0) です。たとえば:

  • 候補者 1 は追加で 2 票得ることで当選を確定することができます。追加で 1 票獲得することでは当選を確定することができないので、C_1 = 2 です。
  • 候補者 2 はどのようにしても(例えば、残り 2 票をすべて獲得しても)当選することが不可能なので、 C_2 = -1 です。

入力例 2

12 1 570
81 62 17 5 5 86 15 7 79 26 6 28

出力例 2

79 89 111 117 117 74 112 116 80 107 117 106

Score : 500 points

Problem Statement

An election is being held with N candidates numbered 1, 2, \ldots, N. There are K votes, some of which have been counted so far.

Up until now, candidate i has received A_i votes.

After all ballots are counted, candidate i (1 \leq i \leq N) will be elected if and only if the number of candidates who have received more votes than them is less than M. There may be multiple candidates elected.

For each candidate, find the minimum number of additional votes they need from the remaining ballots to guarantee their victory regardless of how the other candidates receive votes.

Formally, solve the following problem for each i = 1,2,\ldots,N.

Determine if there is a non-negative integer X not exceeding K - \displaystyle{\sum_{i=1}^{N}} A_i satisfying the following condition. If it exists, find the minimum possible such integer.

  • If candidate i receives X additional votes, then candidate i will always be elected.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{12}
  • 0 \leq A_i \leq 10^{12}
  • \displaystyle{\sum_{i=1}^{N} A_i} \leq K
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N

Output

Let C_i be the minimum number of additional votes candidate i needs from the remaining ballots to guarantee their victory regardless of how other candidates receive votes. Print C_1, C_2, \ldots, C_N separated by spaces.

If candidate i has already secured their victory, then let C_i = 0. If candidate i cannot secure their victory under any circumstances, then let C_i = -1.


Sample Input 1

5 2 16
3 1 4 1 5

Sample Output 1

2 -1 1 -1 0

14 votes have been counted so far, and 2 votes are left.
The C to output is (2, -1, 1, -1, 0). For example:

  • Candidate 1 can secure their victory by obtaining 2 more votes, while not by obtaining 1 more vote. Thus, C_1 = 2.
  • Candidate 2 can never (even if they obtain 2 more votes) secure their victory, so C_2 = -1.

Sample Input 2

12 1 570
81 62 17 5 5 86 15 7 79 26 6 28

Sample Output 2

79 89 111 117 117 74 112 116 80 107 117 106
I - Guess The Number 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。

(フェイズ 1

  • ジャッジが 1 以上 10^9 以下の整数 N を決める。この整数は隠されている。
  • あなたは 1 以上 110 以下の整数 M を出力する。
  • さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq A_i \leq M を満たす、長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力する。

(フェイズ 2

  • ジャッジから、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられる。ここで、 B_i = f^N(i) である。 f(i)1 以上 M 以下の整数 i に対し f(i)=A_i で定められ、 f^N(i)if(i) で置き換える操作を N 回行った際に得られる整数である。
  • あなたは、B の情報から、ジャッジが決めた整数 N を特定し、N を出力する。

上記の手順を行った後、直ちにプログラムを終了することで正解となります。

制約

  • N1 以上 10^9 以下の整数

入出力

この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

(フェイズ 1

  • まず、1 以上 110 以下の整数 M を出力してください。出力後、必ず改行してください。
M
  • その後、空白区切りで 1 以上 M 以下の整数からなる長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力してください。出力後、必ず改行してください。
A_1 A_2 \ldots A_M

(フェイズ 2

  • まず、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が入力から与えられます。
B_1 B_2 \ldots B_M
  • 整数 N を求め、出力してください。出力後、必ず改行してください。
N

不正な出力がなされた場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 答えを出力したら(または -1 を受け取ったら)直ちにプログラムを正常終了してください。そうしなかった場合、ジャッジ結果は不定です。
  • 特に、余計な改行も不正なフォーマットの出力とみなされるので注意してください。

入出力例

以下は、N = 2 の場合の入出力例です。

入力 出力 説明
ジャッジは N=2 と決めました。N は入力としては与えられず、隠されています。
4 M を出力します。
2 3 4 4 A=(2,3,4,4) を出力します。
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4,f^2(4)=4 なので、ジャッジは B=(3,4,4,4) をあなたのプログラムに与えます。
2 B から N=2 であると特定しました。 N を出力し、プログラムを正常終了させてください。

Score : 500 points

Problem Statement

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.

(Phase 1)

  • The judge decides an integer N between 1 and 10^9 (inclusive), which is hidden.
  • You print an integer M between 1 and 110 (inclusive).
  • You also print an integer sequence A=(A_1,A_2,\ldots,A_M) of length M such that 1 \leq A_i \leq M for all i = 1, 2, \ldots, M.

(Phase 2)

  • The judge gives you an integer sequence B=(B_1,B_2,\ldots,B_M) of length M. Here, B_i = f^N(i). f(i) is defined by f(i)=A_i for all integers i between 1 and M (inclusive), and f^N(i) is the integer resulting from replacing i with f(i) N times.
  • Based on the given B, you identify the integer N that the judge has decided, and print N.

After the procedure above, terminate the program immediately to be judged correct.

Constraints

  • N is an integer between 1 and 10^9 (inclusive).

Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase 1)

  • First, print an integer M between 1 and 110 (inclusive). It must be followed by a newline.
M
  • Then, print a sequence A=(A_1,A_2,\ldots,A_M) of length M consisting of integers between 1 and M (inclusive), with spaces in between. It must be followed by a newline.
A_1 A_2 \ldots A_M

(Phase 2)

  • First, an integer sequence B=(B_1,B_2,\ldots,B_M) of length M is given from the input.
B_1 B_2 \ldots B_M
  • Find the integer N and print it. It must be followed by a newline.
N

If you print something illegal, the judge prints -1. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.

Notes

  • After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.
  • After you print the answer (or you receive -1), immediately terminate the program normally. Otherwise, the verdict will be indeterminate.
  • Note that an excessive newline is also considered an invalid input.

Sample Interaction

The following is a sample interaction with N = 2.

Input Output Description
The judge has decided that N=2. N is hidden: it is not given as an input.
4 You print M.
2 3 4 4 You print A=(2,3,4,4).
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4, and f^2(4)=4, so the judge gives B=(3,4,4,4) to your program.
2 Based on B, you have identified that N=2. Print N and terminate the program normally.