A - Gold and Silver

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

すぬけくんは今,1 グラムの金と 0 グラムの銀を持っています. 彼はこれから N 日かけて金と銀の取引を行います. それぞれの日で,"なにもしない" もしくは "交換をする" のいずれかの行動をとります. i 日目 (1 \leq i \leq N) に交換をする場合,以下のことが起こります.

  • 交換前に金を x グラム持っていた場合,それらをすべて銀と交換し,x \times A_i グラムの銀を得る. 逆に,銀を x グラム持っていた場合,それらをすべて金と交換し,x / A_i グラムの金を得る.

すぬけくんの目標は,最終的に持っている金の量を最大化することです. 彼の目標を達成するような方法を一つ求めてください.

制約

  • 2 \leq N \leq 200000
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数である

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを以下の形式で出力せよ.

v_1 v_2 \cdots v_N

ただしここで v_ii 日目の行動を表す整数 (0 \leq v_i \leq 1) であり,v_i=0 ならば何もしないことを,v_i=1 ならば交換をすることを表す. なお,答えが複数通り存在する場合,そのどれを出力しても正解とみなされる.


入力例 1

3
3 5 2

出力例 1

0 1 1

以下のように行動するのが最適です.

  • 1 日目: なにもしない.

  • 2 日目: 1 グラムの金を銀と交換し,5 グラムの銀を得る.

  • 3 日目: 5 グラムの銀を金と交換し,2.5 グラムの金を得る.


入力例 2

4
1 1 1 1

出力例 2

0 0 0 0

(v_1,v_2,v_3,v_4)=(1,1,1,1) なども正解とみなされます.


入力例 3

10
426877385 186049196 624834740 836880476 19698398 709113743 436942115 436942115 436942115 503843678

出力例 3

1 1 0 1 1 1 1 0 0 0

Score : 400 points

Problem Statement

Snuke has 1 gram of gold and 0 grams of silver now. He will do trading of gold and silver for the following N days. On each day, he has two choices: do nothing, or make a trade. If he trades on Day i (1 \leq i \leq N), the following will happen.

  • If he has x grams of gold before the trade, exchange all of it for x \times A_i grams of silver. On the other hand, if he has x grams of silver, exchange all of it for x / A_i grams of gold.

Snuke's objective is to maximize the amount of gold he has in the end. Find one way to achieve his objective.

Constraints

  • 2 \leq N \leq 200000
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer in the following format:

v_1 v_2 \cdots v_N

Here, v_i is an integer representing the action on Day i (0 \leq v_i \leq 1). v_i=0 means he does nothing, and v_i=1 means he makes a trade. If there are multiple possible solutions, printing any of them will be considered correct.


Sample Input 1

3
3 5 2

Sample Output 1

0 1 1

The optimal sequence of actions is as follows.

  • Day 1: Do nothing.

  • Day 2: Exchange 1 gram of gold for 5 grams of silver.

  • Day 3: Exchange 5 grams of silver for 2.5 grams of gold.


Sample Input 2

4
1 1 1 1

Sample Output 2

0 0 0 0

(v_1,v_2,v_3,v_4)=(1,1,1,1), for example, is also considered correct.


Sample Input 3

10
426877385 186049196 624834740 836880476 19698398 709113743 436942115 436942115 436942115 503843678

Sample Output 3

1 1 0 1 1 1 1 0 0 0
B - Balls of Three Colors

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

R 個の赤いボール,G 個の緑のボール,B 個の青いボールがあります. あなたは,以下の操作を好きな回数繰り返すことができます.

  • 色の異なる 2 つのボールを選び,それら両方を残るもう一つの色のボールに変える.

例えば,赤いボールと青いボールを選んだ際は,それら両方を緑のボールに変えます.

あなたの目標は,すべてのボールを同じ色にすることです. 目標が達成可能であるか判定し,また可能であるなら,必要な操作回数の最小値を求めてください.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 100
  • 1 \leq R,G,B \leq 10^8
  • 入力される値はすべて整数である

入力

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

T
case_1
case_2
\vdots
case_T

各ケースは以下の形式で与えられる.

R G B

出力

各ケースについて,目標が達成不可能な場合は -1 を,そうでなければ必要な操作回数の最小値を出力せよ.


入力例 1

3
1 2 2
1 2 3
1 2 4

出力例 1

2
-1
4

例えば,case_3 については,以下のように操作を行えばよいです.

  • 緑のボールと青いボールを選び,それら両方を赤いボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える

Score : 400 points

Problem Statement

We have R red balls, G green balls, and B blue balls. You can do the following operation any number of times:

  • choose two balls of different colors and turn them into two balls of the remaining color.

For example, you can choose a red ball and a blue ball and turn them into two green balls.

Your objective is to make all balls have the same color. Determine whether this objective is achievable. If it is, find the minimum number of operations required to achieve it.

For each input file, solve T test cases.

Constraints

  • 1 \leq T \leq 100
  • 1 \leq R,G,B \leq 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
case_1
case_2
\vdots
case_T

Each case is in the following format:

R G B

Output

For each case, print -1 if the objective is unachievable; otherwise, print the minimum number of operations to achieve it.


Sample Input 1

3
1 2 2
1 2 3
1 2 4

Sample Output 1

2
-1
4

For example, in case_3, one optimal sequence of operations is:

  • choose a green ball and blue ball, turning them into two red balls;
  • choose a red ball and blue ball, turning them into two green balls;
  • choose a red ball and blue ball, turning them into two green balls;
  • choose a red ball and blue ball, turning them into two green balls.
C - Max Dot

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

整数 N,M,S,及び長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます.

次の条件をすべて満たす長さ N の非負実数x=(x_1,x_2,\cdots,x_N) を作ることを考えます.

  • 0 \leq x_1 \leq x_2 \leq \cdots \leq x_N \leq M
  • \sum_{1 \leq i \leq N} x_i=S

ここで,x のスコアを \sum_{1 \leq i \leq N} A_i \times x_i と定義します. x のスコアとしてありうる最大の値を求めてください.

制約

  • 1 \leq N \leq 5000
  • 1 \leq M \leq 10^6
  • 1 \leq S \leq \min(N \times M,10^6)
  • 1 \leq A_i \leq 10^6
  • 入力される値はすべて整数である

入力

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

N M S
A_1 A_2 \cdots A_N

出力

答えを出力せよ. 絶対誤差または相対誤差が 10^{-6} 以内であれば,正解と判定される.


入力例 1

3 2 3
1 2 3

出力例 1

8.00000000000000000000

x=(0,1,2) とするのが最適です.


入力例 2

3 3 2
5 1 1

出力例 2

4.66666666666666666667

x=(2/3,2/3,2/3) とするのが最適です.


入力例 3

10 234567 1000000
353239 53676 45485 617014 886590 423581 172670 928532 312338 981241

出力例 3

676780145098.25000000000000000000

Score : 600 points

Problem Statement

Given are integers N, M, S, and a sequence of N integers A=(A_1,A_2,\cdots,A_N).

Consider making a sequence of N non-negative real numbers x=(x_1,x_2,\cdots,x_N) satisfying all of the following conditions:

  • 0 \leq x_1 \leq x_2 \leq \cdots \leq x_N \leq M,
  • \sum_{1 \leq i \leq N} x_i=S.

Now, let us define the score of x as \sum_{1 \leq i \leq N} A_i \times x_i. Find the maximum possible score of x.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq M \leq 10^6
  • 1 \leq S \leq \min(N \times M,10^6)
  • 1 \leq A_i \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M S
A_1 A_2 \cdots A_N

Constraints

Print the answer. Your output will be considered correct if its absolute or relative error is at most 10^{-6}.


Sample Input 1

3 2 3
1 2 3

Sample Output 1

8.00000000000000000000

The optimal choice is x=(0,1,2).


Sample Input 2

3 3 2
5 1 1

Sample Output 2

4.66666666666666666667

The optimal choice is x=(2/3,2/3,2/3).


Sample Input 3

10 234567 1000000
353239 53676 45485 617014 886590 423581 172670 928532 312338 981241

Sample Output 3

676780145098.25000000000000000000
D - Neq Neq

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

N 個のボールが一列に並べられており,左から順に 1 から N までの番号がついています. ボール i には整数 A_i が書かれています.

あなたは,以下の操作を好きなだけ繰り返すことができます.

  • 連続して並んでいる 3 つのボール x,y,z (1 \leq x < y < z \leq N) を選ぶ. ただしこの時,A_x \neq A_y かつ A_y \neq A_z を満たす必要がある. その後,ボール y を食べる. なお,この操作の後,ボール x とボール z は列の中で連続しているとみなす.

最終的に残っているボールの集合としてありうるものの個数を 998244353 で割った余りを求めてください.

制約

  • 2 \leq N \leq 200000
  • 1 \leq A_i \leq N
  • 入力される値はすべて整数である

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

4
1 2 1 2

出力例 1

3

最終的に残っているボールの集合として考えられるのは,\{1,2,3,4\},\{1,2,4\},\{1,3,4\}3 通りです.


入力例 2

5
5 4 3 2 1

出力例 2

8

異なる操作方法でも,最終的に残るボールの集合が同じであれば区別しません.


入力例 3

5
1 2 3 2 1

出力例 3

8

残るボールに書かれた整数を並べた列が同じでも,ボールの集合が異なる場合は区別されます.


入力例 4

9
1 4 2 2 9 6 9 6 6

出力例 4

14

Score : 700 points

Problem Statement

We have N balls arranged in a row, numbered 1 to N from left to right. Ball i has an integer A_i written on it.

You can do the following operation any number of times.

  • Choose three consecutive balls x, y, z (1 \leq x < y < z \leq N). Here, A_x \neq A_y and A_y \neq A_z must hold. Then, eat Ball y. After this operation, Balls x and z are considered adjacent.

Find the number of possible sets of balls remaining in the end, modulo 998244353.

Constraints

  • 2 \leq N \leq 200000
  • 1 \leq A_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4
1 2 1 2

Sample Output 1

3

There are three possible sets of balls remaining in the end: \{1,2,3,4\},\{1,2,4\},\{1,3,4\}.


Sample Input 2

5
5 4 3 2 1

Sample Output 2

8

Different sequences of operations are not distinguished if they result in the same set of balls.


Sample Input 3

5
1 2 3 2 1

Sample Output 3

8

Different sets of remaining balls are distinguished even if they have the same sequence of integers written on them.


Sample Input 4

9
1 4 2 2 9 6 9 6 6

Sample Output 4

14
E - K Different Values

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

長さ N の整数列 A=(A_1,A_2,\cdots,A_N),及び整数 K が与えられます.

以下の条件を両方満たす整数列 x を作ることを考えます.

  • 各整数 i (1 \leq i \leq N) について,x はちょうど A_i 個の i を含む. また逆に,それ以外の整数を含まない.
  • x の中で連続するどの K 個を見ても,その K 個の値はすべて異なる.

条件を満たす x を作ることが可能かどうか判定し,可能な場合は条件を満たす中で辞書順最小の x を求めてください.

制約

  • 2 \leq K \leq N \leq 500
  • 1 \leq A_i
  • \sum_{1 \leq i \leq N} A_i \leq 200000
  • 入力される値はすべて整数である

入力

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

N K
A_1 A_2 \cdots A_N

出力

条件を満たす数列 x が作ることが不可能な場合,-1 と出力せよ. 可能な場合,辞書順最小の x を出力せよ.


入力例 1

3 3
2 2 1

出力例 1

1 2 3 1 2

x=(1,2,3,1,2),(2,1,3,2,1) の二つが条件を満たし,その中で辞書順最小の (1,2,3,1,2) が答えになります.


入力例 2

3 2
2 1 2

出力例 2

1 2 3 1 3

入力例 3

3 3
1 3 3

出力例 3

-1

Score : 800 points

Problem Statement

Given are a sequence of N integers A=(A_1,A_2,\cdots,A_N) and an integer K.

Consider making a sequence of integers x that satisfies both of the following conditions.

  • For each integer i (1 \leq i \leq N), x contains exactly A_i occurrences of i. x does not contain other integers.
  • For every way of choosing K consecutive elements from x, their values are all distinct.

Determine whether it is possible to make x that satisfies the conditions. If it is possible, find the lexicographically smallest such x.

Constraints

  • 2 \leq K \leq N \leq 500
  • 1 \leq A_i
  • \sum_{1 \leq i \leq N} A_i \leq 200000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

If it is impossible to make a sequence x that satisfies the conditions, print -1. If it is possible, print the lexicographically smallest such x.


Sample Input 1

3 3
2 2 1

Sample Output 1

1 2 3 1 2

Two sequences x=(1,2,3,1,2),(2,1,3,2,1) satisfy the conditions. The lexicographically smaller one, (1,2,3,1,2), is the answer.


Sample Input 2

3 2
2 1 2

Sample Output 2

1 2 3 1 3

Sample Input 3

3 3
1 3 3

Sample Output 3

-1
F - Game against Robot

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

1 から N までの番号のついた N 枚のカードがあり,カード i には整数 A_i が書かれています. なお,N は偶数です.

すぬけくんとロボットがゲームをします. ゲームは,以下のように進行します.

  • ゲームマスターが (1,2,\cdots,N) の順列 (p_1,p_2,\cdots,p_N) を宣言する. この順列はすぬけくんとロボット両方に知らされる.
  • その後,すぬけくんからはじめて,両者が交互に手番をプレイする. 各手番では以下のことが起こる.
    • すぬけくんの手番: 残っているカードの中から好きなものを一つ選び,食べる.
    • ロボットの手番: 残っているカードのうち,p_i が最大となるカード i を選び,燃やす.
  • カードがなくなったらゲームは終了する.

最終的なゲームのスコアは,すぬけくんが食べたカードに書かれた整数の総和です. すぬけくんは,ゲームのスコアを最大化するように最適な行動をします.

順列 pN! 通り考えられますが,これらすべてについてゲームのスコアを求め,その総和を 998244353 で割った余りを求めてください.

制約

  • 1 \leq N \leq 10^6
  • N は偶数
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数である

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

2
1 2

出力例 1

4

順列 p に依らず,すぬけくんはカード 2 を食べます.


入力例 2

4
1 100 10000 1000000

出力例 2

24200400

例えば p=(3,1,4,2) であるとき,ゲームは以下のように進行します.

  • すぬけくんがカード 3 を食べる.
  • ロボットがカード 1 を燃やす.
  • すぬけくんがカード 4 を食べる.
  • ロボットがカード 2 を燃やす.

このとき,ゲームのスコアは 1010000 になります.


入力例 3

10
866111664 178537096 844917655 218662351 383133839 231371336 353498483 865935868 472381277 579910117

出力例 3

710984634

Score : 1000 points

Problem Statement

There are N cards numbered 1 to N. Card i has an integer A_i written on it. Here, N is even.

Snuke and Robot will play a game, which will go as follows.

  • The game master announces a permutation (p_1,p_2,\cdots,p_N) of (1,2,\cdots,N), to both Snuke and Robot.
  • Then, Snuke and Robot alternately take turns, with Snuke going first. Each turn goes as follows.
    • Snuke's turn: choose a remaining card of his choice and eat it.
    • Robot's turn: choose Card i with the largest p_i and burn it.
  • The game ends when there is no more card.

The final score of the game is the sum of integers written on the cards eaten by Snuke. Snuke will play optimally to maximize the score.

There are N! permutations p of (1,2,\cdots,N). Find the sum, modulo 998244353, of the final scores for all of these permutations.

Constraints

  • 1 \leq N \leq 10^6
  • N is even.
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

2
1 2

Sample Output 1

4

Regardless of the permutation p, Snuke will eat Card 2.


Sample Input 2

4
1 100 10000 1000000

Sample Output 2

24200400

For example, when p=(3,1,4,2), the game will go as follows.

  • Snuke eats Card 3.
  • Robot burns Card 1.
  • Snuke eats Card 4.
  • Robot burns Card 2.

The score of the game here is 1010000.


Sample Input 3

10
866111664 178537096 844917655 218662351 383133839 231371336 353498483 865935868 472381277 579910117

Sample Output 3

710984634