A - Scoreboard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

チーム高橋とチーム青木が N 回の試合を行いました。 i 回め (1\leq i\leq N) の試合ではチーム高橋が X _ i 点、チーム青木が Y _ i 点を獲得しました。

N 回の試合で獲得した得点の合計がより多いチームの勝ちです。

どちらのチームが勝ったか出力してください。 ただし、獲得した得点の合計が等しい場合は引き分けとなります。

制約

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
X _ 1 Y _ 1
X _ 2 Y _ 2
\vdots
X _ N Y _ N

出力

チーム高橋が勝った場合 Takahashi を、チーム青木が勝った場合 Aoki を、引き分けの場合 Draw を出力せよ。


入力例 1

4
10 2
10 1
10 2
3 2

出力例 1

Takahashi

4 回の試合で、チーム高橋は 33 点、チーム青木は 7 点を獲得しました。 チーム高橋が勝ったため、Takahashi を出力してください。


入力例 2

6
5 4
4 5
2 4
1 6
7 1
3 2

出力例 2

Draw

いずれのチームも 22 点を獲得しました。 引き分けなので、Draw を出力してください。


入力例 3

4
0 0
10 10
50 50
0 100

出力例 3

Aoki

一方もしくは両方のチームが、一試合のうちに 1 点も取れない場合もあります。

Score: 100 points

Problem Statement

Team Takahashi and Team Aoki played N matches. In the i-th match (1\leq i\leq N), Team Takahashi scored X _ i points, and Team Aoki scored Y _ i points.

The team with the higher total score from the N matches wins.

Print the winner. If the two teams have the same total score, it is a draw.

Constraints

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
X _ 1 Y _ 1
X _ 2 Y _ 2
\vdots
X _ N Y _ N

Output

If Team Takahashi wins, print Takahashi; if Team Aoki wins, print Aoki; if it is a draw, print Draw.


Sample Input 1

4
10 2
10 1
10 2
3 2

Sample Output 1

Takahashi

In four matches, Team Takahashi scored 33 points, and Team Aoki scored 7 points. Team Takahashi wins, so print Takahashi.


Sample Input 2

6
5 4
4 5
2 4
1 6
7 1
3 2

Sample Output 2

Draw

Both teams scored 22 points. It is a draw, so print Draw.


Sample Input 3

4
0 0
10 10
50 50
0 100

Sample Output 3

Aoki

One or both teams may score no points in a match.

B - Extended ABC

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

拡張 A 文字列、拡張 B 文字列、拡張 C 文字列および拡張 ABC 文字列を以下のように定義します。

  • 文字列 S が拡張 A 文字列であるとは、S のすべての文字が A であることをいいます。
  • 文字列 S が拡張 B 文字列であるとは、S のすべての文字が B であることをいいます。
  • 文字列 S が拡張 C 文字列であるとは、S のすべての文字が C であることをいいます。
  • 文字列 S が拡張 ABC 文字列であるとは、ある拡張 A 文字列 S _ A 、拡張 B 文字列 S _ B 、拡張 C 文字列 S _ C が存在して、S _ A,S _ B,S _ C をこの順に連結した文字列が S と等しいことをいいます。

例えば、ABCAAAABBBCCCCCCC などは拡張 ABC 文字列ですが、ABBAAACBBBCCCCCCCAAA などは拡張 ABC 文字列ではありません。 空文字列は拡張 A 文字列でも拡張 B 文字列でも拡張 C 文字列でもあることに注意してください。

A, B, C からなる文字列 S が与えられます。 S が拡張 ABC 文字列ならば Yes を、そうでなければ No を出力してください。

制約

  • SA, B, C からなる文字列
  • 1\leq|S|\leq 100\ (|S| は文字列 S の長さ)

入力

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

S

出力

S が拡張 ABC 文字列ならば Yes を、そうでなければ No を出力せよ。


入力例 1

AAABBBCCCCCCC

出力例 1

Yes

AAABBBCCCCCCC は長さ 3 の拡張 A 文字列 AAA 、長さ 3 の拡張 B 文字列 BBB 、長さ 7 の拡張 C 文字列 CCCCCCC をこの順に連結した文字列なので、拡張 ABC 文字列です。

よって、Yes を出力してください。


入力例 2

ACABABCBC

出力例 2

No

どのような拡張 A 文字列 S _ A, 拡張 B 文字列 S _ B, 拡張 C 文字列 S _ C についても、S _ A,S _ B,S _ C をこの順に連結した文字列が ACABABCBC と等しくなることはありません。

よって、No を出力してください。


入力例 3

A

出力例 3

Yes

入力例 4

ABBBBBBBBBBBBBCCCCCC

出力例 4

Yes

Score: 200 points

Problem Statement

We define Extended A strings, Extended B strings, Extended C strings, and Extended ABC strings as follows:

  • A string S is an Extended A string if all characters in S are A.
  • A string S is an Extended B string if all characters in S are B.
  • A string S is an Extended C string if all characters in S are C.
  • A string S is an Extended ABC string if there is an Extended A string S_A, an Extended B string S_B, and an Extended C string S_C such that the string obtained by concatenating S_A, S_B, S_C in this order equals S.

For example, ABC, A, and AAABBBCCCCCCC are Extended ABC strings, but ABBAAAC and BBBCCCCCCCAAA are not. Note that the empty string is an Extended A string, an Extended B string, and an Extended C string.

You are given a string S consisting of A, B, and C. If S is an Extended ABC string, print Yes; otherwise, print No.

Constraints

  • S is a string consisting of A, B, and C.
  • 1\leq|S|\leq 100 (|S| is the length of the string S.)

Input

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

S

Output

If S is an Extended ABC string, print Yes; otherwise, print No.


Sample Input 1

AAABBBCCCCCCC

Sample Output 1

Yes

AAABBBCCCCCCC is an Extended ABC string because it is a concatenation of an Extended A string of length 3, AAA, an Extended B string of length 3, BBB, and an Extended C string of length 7, CCCCCCC, in this order.

Thus, print Yes.


Sample Input 2

ACABABCBC

Sample Output 2

No

There is no triple of Extended A string S_A, Extended B string S_B, and Extended C string S_C such that the string obtained by concatenating S_A, S_B, and S_C in this order equals ACABABCBC.

Therefore, print No.


Sample Input 3

A

Sample Output 3

Yes

Sample Input 4

ABBBBBBBBBBBBBCCCCCC

Sample Output 4

Yes
C - Lining Up 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

1,2,\ldots,NN 人が一列に並んでいます。

並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。

A _ i\ (1\leq i\leq N) は次のような情報を表しています。

  • A _ i=-1 のとき、人 i は先頭に並んでいる。
  • A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。

列に並んでいる人の番号を先頭から順番に出力してください。

制約

  • 1\leq N\leq3\times10 ^ 5
  • A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
  • 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N

出力

s _ 1,s _ 2,\ldots,s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。


入力例 1

6
4 1 -1 5 3 2

出力例 1

3 5 4 1 2 6

先頭から、人 3,5,4,1,2,6 がこの順に列に並んでいるとき、与えられた情報と一致しています。

実際、

  • 1 は人 4 のすぐ後ろに並んでいます。
  • 2 は人 1 のすぐ後ろに並んでいます。
  • 3 は先頭に並んでいます。
  • 4 は人 5 のすぐ後ろに並んでいます。
  • 5 は人 3 のすぐ後ろに並んでいます。
  • 6 は人 2 のすぐ後ろに並んでいます。

となり、与えられた情報と一致していることが確認できます。

よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。


入力例 2

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

出力例 2

1 2 3 4 5 6 7 8 9 10

入力例 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

出力例 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14

Score: 300 points

Problem Statement

There are N people standing in a line: person 1, person 2, \ldots, person N.

You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.

A _ i\ (1\leq i\leq N) represents the following information:

  • if A _ i=-1, person i is at the front of the line;
  • if A _ i\neq -1, person i is right behind person A _ i.

Print the people's numbers in the line from front to back.

Constraints

  • 1\leq N\leq3\times10 ^ 5
  • A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
  • There is exactly one way to arrange the N people consistent with the information given.
  • All input values are integers.

Input

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

N
A _ 1 A _ 2 \ldots A _ N

Output

If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.


Sample Input 1

6
4 1 -1 5 3 2

Sample Output 1

3 5 4 1 2 6

If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.

Indeed, it can be seen that:

  • person 1 is standing right behind person 4,
  • person 2 is standing right behind person 1,
  • person 3 is at the front of the line,
  • person 4 is standing right behind person 5,
  • person 5 is standing right behind person 3, and
  • person 6 is standing right behind person 2.

Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.


Sample Input 2

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

Sample Output 2

1 2 3 4 5 6 7 8 9 10

Sample Input 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

Sample Output 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
D - Cheating Gomoku Narabe

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

各マスには ox. のうちいずれかの文字が書かれています。 各マスに書かれた文字は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で表され、 マス (i, j) に書かれた文字は、文字列 S_ij 文字目と一致します。

このグリッドに対して、下記の操作を 0 回以上好きな回数だけ繰り返します。

  • . が書かれているマスを 1 個選び、そのマスに書かれた文字を o に変更する。

その結果、縦方向または横方向に連続した K 個のマスであってそれらに書かれた文字がすべて o であるようなものが存在する( すなわち、下記の 2 つの条件のうち少なくとも一方を満たす)ようにすることが可能かを判定し、可能な場合はそのために行う操作回数の最小値を出力してください。

  • 1 \leq i \leq H かつ 1 \leq j \leq W-K+1 を満たす整数の組 (i, j) であって、マス (i, j), (i, j+1), \ldots, (i, j+K-1) に書かれた文字が o であるものが存在する。
  • 1 \leq i \leq H-K+1 かつ 1 \leq j \leq W を満たす整数の組 (i, j) であって、マス (i, j), (i+1, j), \ldots, (i+K-1, j) に書かれた文字が o であるものが存在する。

制約

  • H, W, K は整数
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_iox. のみからなる長さ W の文字列

入力

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

H W K
S_1
S_2
\vdots
S_H

出力

問題文中の条件を満たすことが不可能な場合は -1 を、可能な場合はそのために行う操作回数の最小値を出力せよ。


入力例 1

3 4 3
xo.x
..o.
xx.o

出力例 1

2

操作を 2 回行って、例えばマス (2, 1) とマス (2, 2) に書かれた文字をそれぞれ o に変更することで問題文中の条件を満たすことができ、これが最小の操作回数です。


入力例 2

4 2 3
.o
.o
.o
.o

出力例 2

0

操作を一度も行わなくても問題文中の条件を満たします。


入力例 3

3 3 3
x..
..x
.x.

出力例 3

-1

問題文中の条件を満たすことは不可能なので、-1 を出力します。


入力例 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

出力例 4

3

Score: 400 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 the j-th column from the left.

Each cell contains one of the characters o, x, and .. The characters written in each cell are represented by H strings S_1, S_2, \ldots, S_H of length W; the character written in cell (i, j) is the j-th character of the string S_i.

For this grid, you may repeat the following operation any number of times, possibly zero:

  • Choose one cell with the character . and change the character in that cell to o.

Determine if it is possible to have a sequence of K horizontally or vertically consecutive cells with o written in all cells (in other words, satisfy at least one of the following two conditions). If it is possible, print the minimum number of operations required to achieve this.

  • There is an integer pair (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W-K+1 such that the characters in cells (i, j), (i, j+1), \ldots, (i, j+K-1) are all o.
  • There is an integer pair (i, j) satisfying 1 \leq i \leq H-K+1 and 1 \leq j \leq W such that the characters in cells (i, j), (i+1, j), \ldots, (i+K-1, j) are all o.

Constraints

  • H, W, and K are integers.
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_i is a string of length W consisting of the characters o, x, and ..

Input

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

H W K
S_1
S_2
\vdots
S_H

Output

If it is impossible to satisfy the condition in the problem statement, print -1. Otherwise, print the minimum number of operations required to do so.


Sample Input 1

3 4 3
xo.x
..o.
xx.o

Sample Output 1

2

By operating twice, for example, changing the characters in cells (2, 1) and (2, 2) to o, you can satisfy the condition in the problem statement, and this is the minimum number of operations required.


Sample Input 2

4 2 3
.o
.o
.o
.o

Sample Output 2

0

The condition is satisfied without performing any operations.


Sample Input 3

3 3 3
x..
..x
.x.

Sample Output 3

-1

It is impossible to satisfy the condition, so print -1.


Sample Input 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

Sample Output 4

3
E - Bad Juice

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 425

問題文

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

1 から N の番号がついた N 本のジュースがあります。 このうちちょうど 1 本が腐っていることが判明しました。 そのジュースを微量でも飲むと、翌日お腹を壊してしまいます。

高橋君は翌日までに腐ったジュースを特定しなければなりません。 高橋君はそのために必要な最小の数の友人を呼び、それぞれに N 本のジュースのうちの一部を振る舞うことにしました。 各友人には何本でもジュースを与えることができ、各ジュースは何人の友人にでも与えることができます。

呼ぶ友人の数とジュースの与え方を出力して、翌日に各友人がお腹を壊したかどうかの情報を受け取り、腐ったジュースの番号を出力してください。

制約

  • N は整数
  • 2 \leq N \leq 100

入出力

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

対話を行う前にジャッジは、腐ったジュースの番号 X として 1 以上 N 以下の整数を秘密裏に選択します。 X の値はあなたには与えられません。また、対話の途中で X の値が制約および以前の出力に矛盾しない範囲で変わる場合があります。

まず、ジャッジから N が入力から与えられます。

N

あなたは呼ぶ友人の数 M を出力し改行してください。

M

さらに、あなたは次に述べる M 回の出力からなる手続きを行ってください。 i = 1, 2, \ldots, M について i 回目の出力では、 i 番目の友人に飲ませるジュースの本数 K_i および、それら K_i 本のジュースの番号を昇順に並べた列 A_{i, 1}, A_{i, 2}, \ldots, A_{i, K_i} を下記の形式で空白区切りで出力し、改行してください。

K_i A_{i, 1} A_{i, 2} \ldots A_{i, K_i}

その後ジャッジから、各友人が翌日にお腹を壊したかどうかの情報が、01 のみからなる長さ M の文字列 S として与えられます。

S

i = 1, 2, \ldots, M について、Si 文字目が 1 のとき、かつそのときに限り、i 番目の友人がお腹を壊したことを表します。

それに対し、あなたは腐ったジュースの番号 X' を出力し、改行してください。

X'

その後、直ちにプログラムを終了してください。

あなたが出力した MN 本のジュースから腐ったジュースを特定するために必要な最小の友人の数であり、かつ、あなたが出力した X' が腐ったジュースの番号 X と一致していれば、正解となります。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。 特に、プログラムの実行中に実行時エラーが起こった場合に、ジャッジ結果が ではなく TLE になる可能性があることに注意してください。
  • X' を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • この問題のジャッジはアダプティブです。つまり、制約および以前の出力に矛盾しない範囲で X の値が変わる場合があります。

入出力例

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

入力 出力 説明
3 ジュースの本数 N が与えられます。
2 呼ぶ友人の数 M を出力します。
2 1 2 1 人目の友人にジュース 1 とジュース 2 を与えます。
1 2 2 人目の友人に、ジュース 2 を与えます。
10 翌日に各友人がお腹を壊したかどうかを表す文字列 S が与えられます。
1 腐ったジュースの番号を出力します。

Score: 425 points

Problem Statement

This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).

There are N bottles of juice, numbered 1 to N. It has been discovered that exactly one of these bottles has gone bad. Even a small sip of the spoiled juice will cause stomach upset the next day.

Takahashi must identify the spoiled juice by the next day. To do this, he decides to call the minimum necessary number of friends and serve them some of the N bottles of juice. He can give any number of bottles to each friend, and each bottle of juice can be given to any number of friends.

Print the number of friends to call and how to distribute the juice, then receive information on whether each friend has an upset stomach the next day, and print the spoiled bottle's number.

Constraints

  • N is an integer.
  • 2 \leq N \leq 100

Input/Output

This is an interactive problem (a type of problem where your program interacts with the judge program through Standard Input and Output).

Before the interaction, the judge secretly selects an integer X between 1 and N as the spoiled bottle's number. The value of X is not given to you. Also, the value of X may change during the interaction as long as it is consistent with the constraints and previous outputs.

First, the judge will give you N as input.

N

You should print the number of friends to call, M, followed by a newline.

M

Next, you should perform the following procedure to print M outputs. For i = 1, 2, \ldots, M, the i-th output should contain the number K_i of bottles of juice you will serve to the i-th friend, and the K_i bottles' numbers in ascending order, A_{i, 1}, A_{i, 2}, \ldots, A_{i, K_i}, separated by spaces, followed by a newline.

K_i A_{i, 1} A_{i, 2} \ldots A_{i, K_i}

Then, the judge will inform you whether each friend has a stomach upset the next day by giving you a string S of length M consisting of 0 and 1.

S

For i = 1, 2, \ldots, M, the i-th friend has a stomach upset if and only if the i-th character of S is 1.

You should respond by printing the number of the spoiled juice bottle X', followed by a newline.

X'

Then, terminate the program immediately.

If the M you printed is the minimum necessary number of friends to identify the spoiled juice out of the N bottles, and the X' you printed matches the spoiled bottle's number X, then your program is considered correct.

Notes

  • Each output should end with a newline and flushing Standard Output. Otherwise, you may receive a TLE verdict.
  • The verdict is indeterminate if your program prints an invalid output during the interaction or terminates prematurely. In particular, note that if a runtime error occurs during the execution of the program, the verdict may be or TLE instead of .
  • Terminate the program immediately after printing X'. Otherwise, the verdict is indeterminate.
  • The judge for this problem is adaptive, meaning that the value of X may change as long as it is consistent with the constraints and previous outputs.

Sample Input/Output

Below is an interaction with N = 3.

Input Output Description
3 The number of bottles, N, is given.
2 Print the number of friends to call, M.
2 1 2 Serve juice 1 and juice 2 to the first friend.
1 2 Serve juice 2 to the second friend.
10 The string S is given, indicating whether each friend has a stomach upset the next day.
1 Print the spoiled bottle's number.
F - Usual Color Ball Problems

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

正整数 N, M, K と、長さ N の正整数列 (C_1, C_2, \ldots, C_N) が与えられるので、 r = 0, 1, 2, \ldots, N-1 の場合それぞれについて、下記の問題の答えを出力してください。

色がついた N 個のボールからなる列があり、i = 1, 2, \ldots, N について、列の先頭から i 番目にあるボールの色は C_i です。 また、1 から M の番号がつけられた M 個の空の箱があります。

下記の手順を行った後に箱に入っているボールの総数を求めてください。

  • まず、下記の操作を r 回行う。

    • 列の先頭のボール 1 個を列の最後尾に移動する。
  • その後、列にボールが 1 個以上残っている限り、下記の操作を繰り返す。

    • 列の先頭のボールと同じ色のボールが既に 1 個以上 K 個未満入っている箱が存在する場合、その箱に列の先頭のボールを入れる。
    • そのような箱が存在しない場合、
      • 空の箱が存在するなら、そのうち番号が最小のものに、列の先頭のボールを入れる。
      • 空の箱が存在しない場合、列の先頭のボールをどの箱にも入れず、食べる。

制約

  • 入力される値はすべて整数
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq N
  • 1 \leq C_i \leq N

入力

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

N M K
C_1 C_2 \ldots C_N

出力

r = 0, 1, 2, \ldots, N-1 のそれぞれの場合の問題の答え X_r を下記の通りに N 行にわたって出力せよ。

X_0
X_1
\vdots
X_{N-1}

入力例 1

7 2 2
1 2 3 5 2 5 4

出力例 1

3
3
3
4
4
3
2

例として、r = 1 の場合の手順を説明します。 まず「列の先頭のボール 1 個を列の最後尾に移動する」ことを 1 回行い、ボールの色の列は (2, 3, 5, 2, 5, 4, 1) となります。 その後、ボールを箱に入れていく操作を下記の通りに行います。

  • 1 回目の操作:先頭のボールの色は 2 です。色 2 のボールが 1 個以上 2 個未満入った箱は存在しないため、先頭のボールを空の箱のうち番号が最小の箱 1 に入れます。
  • 2 回目の操作:先頭のボールの色は 3 です。色 3 のボールが 1 個以上 2 個未満入った箱は存在しないため、先頭のボールを空の箱のうち番号が最小の箱 2 に入れます。
  • 3 回目の操作:先頭のボールの色は 5 です。色 5 のボールが 1 個以上 2 個未満入った箱も空の箱も存在しないため、先頭のボールを食べます。
  • 4 回目の操作:先頭のボールの色は 2 です。色 2 のボールが 1 個以上 2 個未満入った箱として箱 1 が存在するため、先頭のボールを箱 1 に入れます。
  • 5 回目の操作:先頭のボールの色は 5 です。色 5 のボールが 1 個以上 2 個未満入った箱も空の箱も存在しないため、先頭のボールを食べます。
  • 6 回目の操作:先頭のボールの色は 4 です。色 4 のボールが 1 個以上 2 個未満入った箱も空の箱も存在しないため、先頭のボールを食べます。
  • 7 回目の操作:先頭のボールの色は 1 です。色 1 のボールが 1 個以上 2 個未満入った箱も空の箱も存在しないため、先頭のボールを食べます。

最終的に箱に入っているボールの総数は 3 個であるので、r = 1 の問題の答えは 3 です。


入力例 2

20 5 4
20 2 20 2 7 3 11 20 3 8 7 9 1 11 8 20 2 18 11 18

出力例 2

14
14
14
14
13
13
13
11
8
9
9
11
13
14
14
14
14
14
14
13

Score: 550 points

Problem Statement

You are given positive integers N, M, K, and a sequence of positive integers of length N, (C_1, C_2, \ldots, C_N). For each r = 0, 1, 2, \ldots, N-1, print the answer to the following problem.

There is a sequence of N colored balls. For i = 1, 2, \ldots, N, the color of the i-th ball from the beginning of the sequence is C_i. Additionally, there are M empty boxes numbered 1 to M.

Determine the total number of balls in the boxes after performing the following steps.

  • First, perform the following operation r times.

    • Move the frontmost ball in the sequence to the end of the sequence.
  • Then, repeat the following operation as long as at least one ball remains in the sequence.

    • If there is a box that already contains at least one but fewer than K balls of the same color as the frontmost ball in the sequence, put the frontmost ball into that box.
    • If there is no such box,
      • If there is an empty box, put the frontmost ball into the one with the smallest box number.
      • If there are no empty boxes, eat the frontmost ball without putting it into any box.

Constraints

  • All input values are integers.
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq N
  • 1 \leq C_i \leq N

Input

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

N M K
C_1 C_2 \ldots C_N

Output

Print the answer X_r to the problem for each r = 0, 1, 2, \ldots, N-1 over N lines as follows:

X_0
X_1
\vdots
X_{N-1}

Sample Input 1

7 2 2
1 2 3 5 2 5 4

Sample Output 1

3
3
3
4
4
3
2

For example, let us explain the procedure for r = 1. First, perform the operation "Move the frontmost ball in the sequence to the end of the sequence" once, and the sequence of ball colors becomes (2, 3, 5, 2, 5, 4, 1). Then, proceed with the operation of putting balls into boxes as follows:

  • First operation: The color of the frontmost ball is 2. There is no box with at least one but fewer than two balls of color 2, so put the frontmost ball into the empty box with the smallest box number, box 1.
  • Second operation: The color of the frontmost ball is 3. There is no box with at least one but fewer than two balls of color 3, so put the frontmost ball into the empty box with the smallest box number, box 2.
  • Third operation: The color of the frontmost ball is 5. There is no box with at least one but fewer than two balls of color 5 and no empty boxes, so eat the frontmost ball.
  • Fourth operation: The color of the frontmost ball is 2. There is a box, box 1, with at least one but fewer than two balls of color 2, so put the frontmost ball into box 1.
  • Fifth operation: The color of the frontmost ball is 5. There is no box with at least one but fewer than two balls of color 5 and no empty boxes, so eat the frontmost ball.
  • Sixth operation: The color of the frontmost ball is 4. There is no box with at least one but fewer than two balls of color 4 and no empty boxes, so eat the frontmost ball.
  • Seventh operation: The color of the frontmost ball is 1. There is no box with at least one but fewer than two balls of color 1 and no empty boxes, so eat the frontmost ball.

The final total number of balls in the boxes is 3, so the answer to the problem for r = 1 is 3.


Sample Input 2

20 5 4
20 2 20 2 7 3 11 20 3 8 7 9 1 11 8 20 2 18 11 18

Sample Output 2

14
14
14
14
13
13
13
11
8
9
9
11
13
14
14
14
14
14
14
13
G - Tree Inversion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

頂点 1, 頂点 2,\ldots, 頂点 NN 頂点からなる木 T が与えられます。 i 番目 (1\leq i\lt N) の辺は頂点 u _ i と頂点 v _ i を結んでいます。

T の頂点 u について f(u) を次のように定めます。

  • f(u)\coloneqq T の頂点 v と頂点 w の組であって、次の 2 つの条件を満たすものの個数
    • 頂点 u と頂点 v を結ぶパス上に頂点 w が含まれる。
    • v\lt w

ただし、「頂点 u と頂点 v を結ぶパス上に頂点 w が含まれる」は、u=wv=w のときにも成立するとします。

f(1),f(2),\ldots,f(N) の値を求めてください。

制約

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq u _ i\leq N\ (1\leq i\leq N)
  • 1\leq v _ i\leq N\ (1\leq i\leq N)
  • 与えられるグラフは木
  • 入力はすべて整数

入力

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

N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}

出力

f(1),f(2),\ldots,f(N) の値をこの順に空白を区切りとして出力せよ。


入力例 1

7
1 2
2 3
2 4
4 5
4 6
6 7

出力例 1

0 1 3 4 8 9 15

与えられる木は以下のようになります。

例えば、f(4)=4 です。 実際、u=4 に対して (v,w)=(1,2),(1,4),(2,4),(3,4)4 通りが条件を満たします。


入力例 2

15
14 9
9 1
1 6
6 12
12 2
2 15
15 4
4 11
11 13
13 3
3 8
8 10
10 7
7 5

出力例 2

36 29 32 29 48 37 45 37 44 42 33 36 35 57 35

与えられる木は以下のようになります。

f(14) の値は、数列 (14,9,1,6,12,2,15,4,11,13,3,8,10,7,5) の転倒数 57 になります。


入力例 3

24
7 18
4 2
5 8
5 15
6 5
13 8
4 6
7 11
23 16
6 18
24 16
14 21
20 15
16 18
3 16
11 10
9 11
15 14
12 19
5 1
9 17
5 22
11 19

出力例 3

20 20 41 20 21 20 28 28 43 44 36 63 40 46 34 40 59 28 53 53 66 42 62 63

Score: 600 points

Problem Statement

You are given a tree T with N vertices: vertex 1, vertex 2, \ldots, vertex N. The i-th edge (1\leq i\lt N) connects vertices u_i and v_i.

For a vertex u in T, define f(u) as follows:

  • f(u)\coloneqq The number of pairs of vertices v and w in T that satisfy the following two conditions:
    • Vertex w is contained in the path connecting vertices u and v.
    • v\lt w.

Here, vertex w is contained in the path connecting vertices u and v also when u=w or v=w.

Find the values of f(1),f(2),\ldots,f(N).

Constraints

  • 2\leq N\leq2\times10^5
  • 1\leq u_i\leq N\ (1\leq i\leq N)
  • 1\leq v_i\leq N\ (1\leq i\leq N)
  • The given graph is a tree.
  • All input values are integers.

Input

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print the values of f(1),f(2),\ldots,f(N) in this order, separated by spaces.


Sample Input 1

7
1 2
2 3
2 4
4 5
4 6
6 7

Sample Output 1

0 1 3 4 8 9 15

The given tree is as follows:

For example, f(4)=4. Indeed, for u=4, four pairs (v,w)=(1,2),(1,4),(2,4),(3,4) satisfy the conditions.


Sample Input 2

15
14 9
9 1
1 6
6 12
12 2
2 15
15 4
4 11
11 13
13 3
3 8
8 10
10 7
7 5

Sample Output 2

36 29 32 29 48 37 45 37 44 42 33 36 35 57 35

The given tree is as follows:

The value of f(14) is 57, which is the inversion number of the sequence (14,9,1,6,12,2,15,4,11,13,3,8,10,7,5).


Sample Input 3

24
7 18
4 2
5 8
5 15
6 5
13 8
4 6
7 11
23 16
6 18
24 16
14 21
20 15
16 18
3 16
11 10
9 11
15 14
12 19
5 1
9 17
5 22
11 19

Sample Output 3

20 20 41 20 21 20 28 28 43 44 36 63 40 46 34 40 59 28 53 53 66 42 62 63