A - Larger Score

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の整数列 A=(A_1,A_2,\cdots,A_N) があります. 以降この問題では,A の先頭 K 項の和を スコア と呼ぶことにします. また,入力で与えられた A のスコアを s と置くことにします.

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

  • A のある隣接する 2 要素を選び,それらの値を入れ替える.

あなたの目標は,スコアを s+1 以上にすることです. 目標が達成可能であるかどうか判定し,また可能であるなら必要な最小の操作回数を求めてください.

制約

  • 2 \leq N \leq 4 \times 10^5
  • 1 \leq K \leq N-1
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \cdots A_N

出力

目標が達成可能でない場合,-1 を出力せよ. 可能である場合,必要な最小の操作回数を出力せよ.


入力例 1

4 2
2 1 1 2

出力例 1

2

まず,s=2+1=3 です. 以下のように操作することで,スコアを 4 以上にすることができます.

  • (2,1,1,2) \to (A_3A_4 の値を入れ替える )\to (2,1,2,1) \to (A_2A_3 の値を入れ替える )\to (2,2,1,1)

1 回の操作では目標を達成できないため,必要な最小の操作回数は 2 になります.


入力例 2

3 1
3 2 1

出力例 2

-1

入力例 3

20 13
90699850 344821203 373822335 437633059 534203117 523743511 568996900 694866636 683864672 836230375 751240939 942020833 865334948 142779837 22252499 197049878 303376519 366683358 545670804 580980054

出力例 3

13

Score : 400 points

Problem Statement

We have an integer sequence of length N: A=(A_1,A_2,\cdots,A_N). Below in this problem, let the score of A be the sum of the first K terms of A. Additionally, let s be the score of the sequence A given in input.

You can do the following operation any number of times.

  • Choose two adjacent elements of A and swap them.

Your objective is to make the score at least s+1. Determine whether the objective is achievable. If it is, find the minimum number of operations needed to achieve it.

Constraints

  • 2 \leq N \leq 4 \times 10^5
  • 1 \leq K \leq N-1
  • 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 K
A_1 A_2 \cdots A_N

Output

If the objective is not achievable, print -1. If it is achievable, print the minimum number of operations needed to achieve it.


Sample Input 1

4 2
2 1 1 2

Sample Output 1

2

We have s=2+1=3. The following sequence of operations makes the score at least 4.

  • (2,1,1,2) \to (swap A_3 and A_4)\to (2,1,2,1) \to (swap A_2 and A_3)\to (2,2,1,1)

The objective is not achievable in one operation, so the minimum number of operations needed is 2.


Sample Input 2

3 1
3 2 1

Sample Output 2

-1

Sample Input 3

20 13
90699850 344821203 373822335 437633059 534203117 523743511 568996900 694866636 683864672 836230375 751240939 942020833 865334948 142779837 22252499 197049878 303376519 366683358 545670804 580980054

Sample Output 3

13
B - 01 Generation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

すぬけくんは,01 からなる長さ N の整数列を作ろうとしています. 今すぬけ君は空の数列 x を持っており,これから以下の 2 種類の操作を好きな順番で N 回行います.

  • 操作A: x の要素をすべて flip する.つまり,0 ならば 1 に変え,1 ならば 0 に変える. その後,x の先頭に 0 を追加する.
  • 操作B: x の末尾に 0 を追加する.

01 からなる長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます. xA に一致させることが可能かどうか判定してください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

xA に一致させることが可能ならば Yes を,不可能ならば No を出力せよ.


入力例 1

4
0 1 1 0

出力例 1

Yes

以下のように操作すればよいです.

  • 始状態:x=()
  • 操作Aを行う.x=(0) となる.
  • 操作Bを行う.x=(0,0) となる.
  • 操作Aを行う.x=(0,1,1) となる.
  • 操作Bを行う.x=(0,1,1,0) となる.

入力例 2

4
1 0 0 0

出力例 2

No

入力例 3

4
0 0 0 1

出力例 3

No

Score : 500 points

Problem Statement

Snuke is about to make an integer sequence of length N consisting of 0 and 1. He starts with an empty sequence x and does the following two kinds of operations N times in total, in any order he likes.

  • Operation A: Flip every element of x, that is, convert each 0 to 1 and vice versa. Then, add 0 to the beginning of x.
  • Operation B: Add 0 to the end of x.

You are given an integer sequence of length N consisting of 0 and 1: A=(A_1,A_2,\cdots,A_N). Determine whether it is possible to make x equal to A.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 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

If it is possible to make x equal to A, print Yes; otherwise, print No.


Sample Input 1

4
0 1 1 0

Sample Output 1

Yes

Snuke can do the following.

  • Start with x=()
  • Do operation A, making x=(0).
  • Do operation B, making x=(0,0).
  • Do operation A, making x=(0,1,1).
  • Do operation B, making x=(0,1,1,0).

Sample Input 2

4
1 0 0 0

Sample Output 2

No

Sample Input 3

4
0 0 0 1

Sample Output 3

No
C - Rotate and Play Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

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

これから,すぬけ君と最小太郎君がゲームをします. ゲームは N ターンからなり,すぬけ君から始めて二人が交互にターンをプレイします. 各ターンでは,以下の操作を行います.

  • すぬけ君のターン:まだ誰にも取られていないカードのうち,好きなものを一つ選び,取る.
  • 最小太郎君のターン:まだ誰にも取られていないカードのうち,番号が最小のものを一つ選び,取る.

すぬけ君のスコアは,すぬけ君が取ったカードに書かれた整数の総和になります. すぬけ君はスコアを最大化するように最適に行動します.

ところで,すぬけ君の大ファンであるあなたは,とある不正を行うことでスコアを最大化しようと考えています. 具体的には,ゲームの開始前に,あなたは以下の行動を一回行います.

  • 整数 k (0 \leq k \leq N-1) を選び,カードに書かれている整数を k 個左に cyclic-shift する. つまり,カード 1,2,\cdots,N に書かれている数を,A_{k+1},A_{k+2},\cdots,A_N,A_1,\cdots,A_k に置き換える.

スコアを最大化するためにあなたが選ぶべき k の値,およびその k を選んだ場合のスコアを求めてください.

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は偶数
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

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

k s

ここで k はあなたが選ぶ整数 (0 \leq k \leq N-1) であり,s はその k を選んだ場合のスコアである. なお,s を最大化するような k が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

4
3 4 1 2

出力例 1

1 7

k=1 を選ぶと,カード 1,2,3,4 に書かれた整数は 4,1,2,3 になります. その後,ゲームは以下のように進行します.

  • すぬけ君がカード 1 を取る.
  • 最小太郎君がカード 2 を取る.
  • すぬけ君がカード 4 を取る.
  • 最小太郎君がカード 3 を取る.

このときのすぬけ君のスコアは 7 になります.

なお,この例では k=2,3 でも正解になります.


入力例 2

2
1 1

出力例 2

0 1

入力例 3

10
716893678 779607519 555600775 393111963 950925400 636571379 912411962 44228139 15366410 2063694

出力例 3

7 3996409938

Score : 600 points

Problem Statement

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

Snuke and Mr. Min will play a game. The game consists of N turns, alternately taken by the two players, with Snuke going first. In each turn, the player does the following operation.

  • In Snuke's turn: He takes any card of his choice that is not taken by anyone yet.
  • In Mr. Min's turn: He takes the card with the minimum index that is not taken by anyone yet.

Snuke's score will be the sum of the integers written on the cards taken by Snuke. Snuke plays optimally to maximize the score.

Incidentally, being a big fan of Snuke, you are planning to do something nasty to maximize the score. Specifically, before the start of the game, you will do the following action once.

  • Choose an integer k (0 \leq k \leq N-1) and cyclically shift the integers written on the cards by k to the left: the cards 1,2,\cdots,N will have A_{k+1},A_{k+2},\cdots,A_N,A_1,\cdots,A_k written on them.

Find the value k that you should choose to maximize the score, and the resulting score when choosing that k.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 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 in the following format:

k s

Here, k is the integer you choose (0 \leq k \leq N-1), and s is the score when choosing that k. If there are multiple values of k that maximize s, printing any of them will be accepted.


Sample Input 1

4
3 4 1 2

Sample Output 1

1 7

If you choose k=1, the cards 1,2,3,4 will have 4,1,2,3 written on them. Then, the game will go as follows.

  • Snuke takes Card 1.
  • Mr. Min takes Card 2.
  • Snuke takes Card 4.
  • Mr. Min takes Card 3.

In this case, Snuke's score is 7.

In this input, k=2,3 will also be accepted.


Sample Input 2

2
1 1

Sample Output 2

0 1

Sample Input 3

10
716893678 779607519 555600775 393111963 950925400 636571379 912411962 44228139 15366410 2063694

Sample Output 3

7 3996409938
D - Differ by K bits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

整数 N,K が与えられます. (0,1,\cdots,2^N-1) の順列 P=(P_0,P_1,\cdots,P_{2^N-1}) であって,以下の条件を満たすものが存在するか判定し, また存在するなら一つ構成してください.P の添字が 0 から始まることに注意してください.

  • すべての i (0 \leq i \leq 2^N-1) について,P_iP_{i+1 \mod 2^N}2 進表記でちょうど K 桁だけ異なる. なお,比較の際はどちらも leading 0's を補って N 桁に揃えた上で比較する.

制約

  • 1 \leq K \leq N \leq 18
  • 入力される値はすべて整数

入力

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

N K

出力

条件を満たす P が存在しない場合,No と出力せよ. 存在する場合,以下の形式で答えを出力せよ.

Yes
P_0 P_1 \cdots P_{2^N-1}

条件を満たす解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

3 1

出力例 1

Yes
0 1 3 2 6 7 5 4

P2 進表記で書くと,P=(000,001,011,010,110,111,101,100) です.

例えば P_1=001,P_2=011 なので,これらはちょうど 1 桁だけ異なっており,i=1 について条件が成立していることが確認できます. 同様に,すべての i についても条件を満たしています.


入力例 2

2 2

出力例 2

No

Score : 700 points

Problem Statement

You are given integers N and K. Determine whether there exists a permutation P=(P_0,P_1,\cdots,P_{2^N-1}) of (0,1,\cdots,2^N-1) satisfying the condition below, and construct one such sequence if it exists. Note that P is 0-indexed.

  • For every i (0 \leq i \leq 2^N-1), P_i and P_{i+1 \mod 2^N} differ by exactly K bits in binary representation. The comparison is made after zero-padding both integers to N bits.

Constraints

  • 1 \leq K \leq N \leq 18
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If there is no P satisfying the condition, print No. If there is such a P, print it in the following format:

Yes
P_0 P_1 \cdots P_{2^N-1}

If there are multiple solutions satisfying the condition, printing any of them will be accepted.


Sample Input 1

3 1

Sample Output 1

Yes
0 1 3 2 6 7 5 4

Here, we have P=(000,001,011,010,110,111,101,100) in binary representation.

We can see that P_1=001 and P_2=011, for example, differ by exactly 1 bit, satisfying the condition for i=1. The same goes for every i.


Sample Input 2

2 2

Sample Output 2

No
E - Decreasing Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

整数 N,K が与えられます. 以下の条件をすべて満たす整数列 A=(A_1,A_2,\cdots,A_N) を,よい数列と呼ぶことにします.

  • 0 \leq A_i \leq i (1 \leq i \leq N)
  • 各整数 v=1,2,\cdots,N について,A_i=v となる i は高々 1 つ.

すべてのよい数列 A について以下の問題の答えを足し合わせた値を 10^9+7 で割った余りを求めてください.

  • A の長さ K の(連続するとは限らない)部分列であって,正整数のみからなり,かつ単調減少であるようなものは何通りあるか求めよ. 別の言い方をすれば,添字の列 1 \leq i_1 < i_2 < \cdots < i_K \leq N であって, A_{i_1}>A_{i_2}>\cdots>A_{i_K} \geq 1 を満たすものの個数を求めよ.

制約

  • 3 \leq N \leq 5000
  • 2 \leq K
  • 2K-1 \leq N
  • 入力される値はすべて整数

入力

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

N K

出力

答えを出力せよ.


入力例 1

3 2

出力例 1

1

例えば A=(0,2,1) はよい数列であり,条件を満たす部分列の個数は 1 です. それ以外のよい数列の例としては A=(0,1,0),(1,2,3),(0,0,0) などが考えられますが,どれも条件を満たす部分列が存在しません. 結局,A=(0,2,1) 以外のよい数列は条件を満たす部分列を持たず,よって答えは 1 になります.


入力例 2

6 2

出力例 2

660

入力例 3

10 3

出力例 3

242595

入力例 4

100 10

出力例 4

495811864

Score : 1000 points

Problem Statement

You are given integers N and K. Let us call an integer sequence A=(A_1,A_2,\cdots,A_N) good when it satisfies all of the conditions below.

  • 0 \leq A_i \leq i (1 \leq i \leq N)
  • For every integer v=1,2,\cdots,N, there is at most one index i such that A_i=v.

Find the sum, modulo (10^9+7), of the answers to the following problem for all good sequences A.

  • Find the number of length-K (not necessarily contiguous) subsequences of A consisting of positive integers that are decreasing. In other words, find the number of sequences of indices 1 \leq i_1 < i_2 < \cdots < i_K \leq N such that A_{i_1}>A_{i_2}>\cdots>A_{i_K} \geq 1.

Constraints

  • 3 \leq N \leq 5000
  • 2 \leq K
  • 2K-1 \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.


Sample Input 1

3 2

Sample Output 1

1

For example, A=(0,2,1) is a good sequence, with one subsequence satisfying the condition. There are other good sequences such as A=(0,1,0),(1,2,3),(0,0,0), but none of them has subsequences satisfying the condition. In the end, no good sequence other than A=(0,2,1) has subsequences satisfying the condition, so the answer is 1.


Sample Input 2

6 2

Sample Output 2

660

Sample Input 3

10 3

Sample Output 3

242595

Sample Input 4

100 10

Sample Output 4

495811864
F - KD Tree

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

平面上に N 個の点があり,そのうち i 番目 (1 \leq i \leq N) の点は (i,P_i) です. なお,(P_1,P_2,\cdots,P_N)(1,2,\cdots,N) の順列になっています.

あなたは,空でない点の集合 s に対し,整列という操作を行えます. 整列とは,以下のような再帰的な操作です.

  • s に含まれる点がちょうど 1 個のとき,その点だけからなる列を作る.
  • s に含まれる点が 2 個以上のとき,以下の 2 種類のうちどちらかの操作を行い,s に含まれる点からなる列を作る.
    • 整数 x を自由に選び,X座標が x 未満の点の集合(この集合を a と呼ぶ)と,それ以外の点の集合(この集合を b と呼ぶ)に分ける. ここで,ab が空であってはならない. a を整列してできた列の後ろに,b を整列してできた列を連結し,新しい列を作る.
    • 整数 y を自由に選び,Y座標が y 未満の点の集合(この集合を a と呼ぶ)と,それ以外の点の集合(この集合を b と呼ぶ)に分ける. ここで,ab が空であってはならない. a を整列してできた列の後ろに,b を整列してできた列を連結し,新しい列を作る.

点の集合 \{(1,P_1),(2,P_2),\cdots,(N,P_N)\} に対して整列を行うとき,その結果としてありうる列の個数を 10^9+7 で割った余り求めてください.

制約

  • 1 \leq N \leq 30
  • (P_1,P_2,\cdots,P_N)(1,2,\cdots,N) の順列
  • 入力される値はすべて整数である

入力

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

N
P_1 P_2 \cdots P_N

出力

答えを出力せよ.


入力例 1

3
3 1 2

出力例 1

3

以下の 3 種類の列を得ることができます.

  • ((1,3),(2,1),(3,2))
  • ((2,1),(3,2),(1,3))
  • ((2,1),(1,3),(3,2))

たとえば,((2,1),(1,3),(3,2)) という列は,以下の手順で得られます.

  • 集合 \{(1,3),(2,1),(3,2)\} に対して整列を行う.Y座標が 2 未満の点の集合 (=\{(2,1)\}) とそれ以外の点の集合 (=\{(1,3),(3,2)\}) に分ける.
    • 集合 \{(2,1)\} に対して整列を行う.列 ((2,1)) を得る.
    • 集合 \{(1,3),(3,2)\} に対して整列を行う.X座標が 3 未満の点の集合とそれ以外の点の集合に分ける.
      • \{(1,3)\} に対して整列を行う.列 ((1,3)) を得る.
      • \{(3,2)\} に対して整列を行う.列 ((3,2)) を得る.
      • 得られた 2 つの列を連結し,列 ((1,3),(3,2)) を得る.
    • 得られた 2 つの列を連結し,列 ((2,1),(1,3),(3,2)) を得る.

入力例 2

5
1 2 3 4 5

出力例 2

1

入力例 3

10
3 6 4 8 7 2 10 5 9 1

出力例 3

1332

入力例 4

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

出力例 4

641915679

Score : 1000 points

Problem Statement

There are N points in a plane, the i-th (1 \leq i \leq N) of which is (i,P_i). Here, (P_1,P_2,\cdots,P_N) is a permutation of (1,2,\cdots,N).

You can arrange a non-empty set s of points, which is a recursive operation defined as follows.

  • If s contains exactly one point, arranging it creates a sequence composed of just that point.
  • If s contains two or more points, arranging it creates a sequence composed of those points by doing one of the following two operations.
    • Choose any integer x and divide s into two: the set a of points whose X-coordinates are less than x and the set b of the other points. Here, neither a nor b should be empty. Create a sequence by concatenating the sequence obtained by arranging a and the sequence obtained by arranging b in this order.
    • Choose any integer y and divide s into two: the set a of points whose Y-coordinates are less than y and the set b of the other points. Here, neither a nor b should be empty. Create a sequence by concatenating the sequence obtained by arranging a and the sequence obtained by arranging b in this order.

Find the number, modulo (10^9+7), of sequences that can be obtained by arranging the set of points \{(1,P_1),(2,P_2),\cdots,(N,P_N)\}.

Constraints

  • 1 \leq N \leq 30
  • (P_1,P_2,\cdots,P_N) is a permutation of (1,2,\cdots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \cdots P_N

Output

Print the answer.


Sample Input 1

3
3 1 2

Sample Output 1

3

The following three sequences can be obtained.

  • ((1,3),(2,1),(3,2))
  • ((2,1),(3,2),(1,3))
  • ((2,1),(1,3),(3,2))

For example, the sequence ((2,1),(1,3),(3,2)) can be obtained as follows.

  • Arrange the set \{(1,3),(2,1),(3,2)\} by dividing it to the set of points whose Y-coordinates are less than 2 (=\{(2,1)\}) and the set of the other points (=\{(1,3),(3,2)\}).
    • Arrange the set \{(2,1)\} to obtain the sequence ((2,1)).
    • Arrange the set \{(1,3),(3,2)\} by dividing it to the set of points whose X-coordinates are less than 3 and the set of the other points.
      • Arrange the set \{(1,3)\} to obtain the sequence ((1,3)).
      • Arrange the set \{(3,2)\} to obtain the sequence ((3,2)).
      • Concatenate the resulting two sequences to obtain the sequence ((1,3),(3,2)).
    • Concatenate the resulting two sequences to obtain the sequence ((2,1),(1,3),(3,2)).

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1

Sample Input 3

10
3 6 4 8 7 2 10 5 9 1

Sample Output 3

1332

Sample Input 4

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

Sample Output 4

641915679