A - >< again

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の文字列 S があります。S の各文字は < または > です。

要素数 N+1 の非負整数列 X_0,X_1,\ldots,X_N は、すべての 1 \leq i \leq N について次の条件を満たすとき 良い非負整数列 と呼ばれます。

  • S_i< のとき : X_{i-1}<X_i
  • S_i > のとき : X_{i-1}>X_i

良い非負整数列 A が与えられるので、この数列をできるだけ多くの良い非負整数列に分解してください。 つまり、正の整数 k および k 個の良い非負整数列 B_1,B_2,\ldots, B_k であって、次の条件を満たすもののうち、 k が最大のものを 1 つ求めてください。

  • すべての 0 \leq i \leq N について B_1,\ldots,B_ki 項目の値の合計は A_i と等しい。

制約

  • 1 \leq N \leq 100
  • 0 \leq A_i \leq 10^4
  • S<> からなる長さ N の文字列である。
  • A は良い非負整数列である。特に、要素数は N+1 である。

入力

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

N
S
A_0 A_1 \cdots A_N

出力

以下の形式で、標準出力に出力せよ。

k
B_{1,0} B_{1,1} \cdots B_{1,N}
:
B_{k,0} B_{k,1} \cdots B_{k,N}

ここで、B_{i,j} は良い非負整数列 B_ij 項目の値を表している。


入力例 1

3
<><
3 8 6 10

出力例 1

2
1 5 4 7
2 3 2 3

Score : 400 points

Problem Statement

We have a string S of length N, where each character is < or >.

A non-negative integer sequence of N+1 elements, X_0,X_1,\ldots,X_N, is said to be good when it satisfies the following for every 1 \leq i \leq N:

  • X_{i-1}<X_i, if S_i is <;
  • X_{i-1}>X_i, if S_i is >.

Given a good non-negative integer sequence A, divide it into as many good non-negative integer sequences as possible. That is, find a positive integer k and k good non-negative integer sequences B_1,B_2,\ldots, B_k satisfying the following with the maximum possible value of k:

  • For every 0 \leq i \leq N, the sum of the i-th elements of B_1,\ldots,B_k equals A_i.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq A_i \leq 10^4
  • S is a string of length N consisting of < and >.
  • A is a good non-negative integer sequence. Particularly, A has N+1 elements.

Input

Input is given from Standard Input in the following format:

N
S
A_0 A_1 \cdots A_N

Output

Print your answer to Standard Output in the following format:

k
B_{1,0} B_{1,1} \cdots B_{1,N}
:
B_{k,0} B_{k,1} \cdots B_{k,N}

Here, B_{i,j} denotes the value of the j-th element of the good non-negative integer sequence B_i.


Sample Input 1

3
<><
3 8 6 10

Sample Output 1

2
1 5 4 7
2 3 2 3
B - Taking the middle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

2N 枚のカードがあり、それぞれには 1 から 2N までの番号が付いています。カード i の価値は V_i です。 高橋君と青木君は以下の手順を N 回繰り返し、カードを N 枚ずつに分配します。

  • まず、高橋君がまだ選ばれてないカードの中から 1 枚選び、自分のものとする。 その後、青木君はまだ選ばれてないカードのうち 番号 が中央値であるものを選び、自分のものとする。

高橋君が最終的に持っているカードの価値の総和として考えられる最大の値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 0\leq V_i\leq 10^9
  • V_i は整数

入力

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

N
V_1 V_2 \cdots V_{2N}

出力

答えを出力せよ。


入力例 1

3
1 2 3 4 5 6

出力例 1

15

以下のような手順で、高橋君はカード 4,5,6 を手にすることができます。

  • まず、高橋君はカード 6 を選ぶ。そして、青木君はカード 3 を選ぶ。
  • 次に、高橋君はカード 5 を選ぶ。そして、青木君はカード 2 を選ぶ。
  • 最後に、高橋君はカード 4 を選ぶ。そして、青木君はカード 1 を選ぶ。

入力例 2

4
1 4 5 8 7 6 3 2

出力例 2

20

Score : 700 points

Problem Statement

We have 2N cards, with ID numbers from 1 through 2N. Card i has a value of V_i. Takahashi and Aoki will do the following procedure N times so that each of them gets N cards.

  • First, Takahashi chooses one card that is not yet chosen and gets it. Then, Aoki chooses the card whose ID number is the median of those of the cards not yet chosen and gets it.

Find the maximum possible sum of the values of cards Takahashi has in the end.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 0\leq V_i\leq 10^9
  • V_i is an integer.

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 \cdots V_{2N}

Output

Print the answer.


Sample Input 1

3
1 2 3 4 5 6

Sample Output 1

15

Takahashi can get Cards 4, 5, and 6 as follows:

  • First, Takahashi chooses Card 6, which makes Aoki choose Card 3.
  • Next, Takahashi chooses Card 5, which makes Aoki choose Card 2.
  • Lastly, Takahashi chooses Card 4, which makes Aoki choose Card 1.

Sample Input 2

4
1 4 5 8 7 6 3 2

Sample Output 2

20
C - Random Card Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

2N 枚のカードがあり、それぞれには 1 から 2N までの番号が付いています。 このカードを用いて行う、次のゲームを考えます。

まず、ディーラーはそれぞれの山が N 枚のカードからなるように、カードを 2 つの山にランダムに分けます。 このとき、ディーラーは各山におけるカードの順序もランダムに定めます。 その後プレイヤーは、一方の山が空になるまで次の操作を繰り返し行い、最終的な操作回数がこのゲームのスコアとなります。

  • ある正の整数 k を選び、一方の山の上から k 枚目のカードと、もう一方の山の上から k 枚目のカードを比較する。(ただし、k は各山のカード枚数を超えてはいけない。)そして、番号が小さい方のカードをそのカードを含む山から取り除く。

このゲームを チーター がプレイするとします。 つまり、各山の各カードの番号を常に把握できるプレイヤーがプレイするとします。 このプレイヤーがスコアを最小化するよう最適にプレイしたときの、スコアの期待値を \bmod 10^9+7 で求めてください(注記参照)。

注記

  • 求める期待値は有理数となります。期待値を分数 \frac{y}{x}xy は互いに素な正の整数)で表したとき、xP=10^9+7 と互いに素になるので、 xz \equiv y \pmod P なる 0 以上 P-1 以下の唯一の整数 z を出力してください。

制約

  • 1 \leq N \leq 10^6

入力

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

N

出力

答えを出力せよ。


入力例 1

1

出力例 1

1

入力例 2

3

出力例 2

486111118

Score : 900 points

Problem Statement

We have 2N cards, with ID numbers from 1 through 2N. Consider the following game using them.

First, the dealer randomly split the cards into two piles of N cards each. Here, the dealer also randomly chooses the order of cards in each pile. Then, the player repeatedly does the operation below until one pile becomes empty, and the number of operations done will be the score.

  • Choose a positive integer k and compare the k-th cards from the top in the two piles. (k should not exceed the number of each pile.) Then, remove the card with the smaller ID number from the pile that contains it.

Assume that a cheater plays this game. That is, assume that the player can always see the ID numbers of all cards in both piles. Find the expected value of the score when the player plays optimally to minimize it, modulo (10^9 + 7) (see Notes).

Notes

  • The expected value in question will be a rational number. If we express it as a fractional number \frac{y}{x} where x and y are coprime positive integers, x will be coprime with P=10^9+7, so print the only integer z between 0 and P-1 (inclusive) such that xz \equiv y \pmod P.

Constraints

  • 1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

1

Sample Output 1

1

Sample Input 2

3

Sample Output 2

486111118
D - Everyone is a winner

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

N 人の参加者と N 個の問題からなるコンテストがあります。 参加者には 1 から N までの番号が付いています。 各参加者と問題の組について、その参加者がその問題を解くのにかかる時間が分かっており、 その時間は 1 分、2 分または 3 分です。 N 個の問題のうち、参加者 i が解くのに 1 分かかる問題は A_i 問、 2 分かかる問題は B_i 問、3 分かかる問題は C_i 問あります。

各参加者が問題を解く順番を自由に定めることで、すべての 1 \leq i, j \leq N について以下が成立することがありうるか判定してください。

  • 参加者 i が最初の i 問を解くのにかかる時間を S 分、参加者 j が最初の i 問を解くのにかかる時間を T 分とする。このとき S \leq T となる。

つまり、すべての i について参加者 i が最初の i 問を解いた時点で 1 位(同率でもよい)となることがありうるか判定してください。 ただし、ある問題を解いてから次の問題にうつるまでにかかる時間は無視できるものとします。

T 個のテストケースが与えられるので、それぞれを解いてください。

制約

  • 1 \leq T \leq 2\times 10^5
  • 1 \leq N \leq 2\times 10^5
  • 0 \leq A_i,B_i,C_i \leq N
  • A_i+B_i+C_i=N
  • 全テストケースにおける N の総和は 2\times 10^5 以下である。

入力

入力は以下の形式で標準入力から与えられる。入力の 1 行目は次の通りである。

T

そして、以下の形式で T 個のテストケースが続く。

N
A_1 B_1 C_1
:
A_N B_N C_N

出力

各テストケースについて、問題文の条件が成立しうるならば Yes 、そうでなければ No と出力せよ。 1 行につき 1 個のテストケースへの出力を行え。なお、正誤判定器は英大文字と英小文字を区別せず、どちらも受理する。


入力例 1

2
3
0 2 1
0 1 2
1 1 1
3
0 2 1
0 0 3
1 1 1

出力例 1

Yes
No

最初のテストケースでは、例えば以下のような場合に条件が成立します。

  • 参加者 11 問目を 3 分、2 問目を 2 分、3 問目を 2 分かけて解く。
  • 参加者 21 問目を 3 分、2 問目を 2 分、3 問目を 3 分かけて解く。
  • 参加者 31 問目を 3 分、2 問目を 2 分、3 問目を 1 分かけて解く。

Score : 1000 points

Problem Statement

We have a contest with N participants and N problems. The participants are numbered 1 through N. For each pair of a participant and a problem, we know the time it takes for the participant to solve the problem, and it is 1, 2, or 3 minutes. Among the N problems, there are A_i problems that Participant i takes 1 minute to solve, B_i problems that s/he takes 2 minutes to solve, and C_i problems that s/he takes 3 minutes to solve.

Determine whether it is possible for the following to hold for every 1 \leq i, j \leq N when the participants can freely decide the order they solve the problems.

  • Let S be the total time Participant i takes to solve the first i problems, and T be the total time Participant j takes to solve the first i problems. Then, we have S \leq T.

In other words, determine whether it is possible that Participant i places first (possibly with ties) when they have solved the first i problems, ignoring the time it takes for them to switch between problems.

Given T test cases, solve each of them.

Constraints

  • 1 \leq T \leq 2\times 10^5
  • 1 \leq N \leq 2\times 10^5
  • 0 \leq A_i,B_i,C_i \leq N
  • A_i+B_i+C_i=N
  • The sum of N over all test cases is at most 2\times 10^5.

Input

Input is given from Standard Input in the following format. The first line is as follows:

T

Then, T test cases follow, each in the format below:

N
A_1 B_1 C_1
:
A_N B_N C_N

Output

For each test case, print Yes if the condition in Problem Statement can be satisfied, and print No otherwise. Use one line for each test case. (The checker is case-insensitive: we will accept both uppercase and lowercase letters.)


Sample Input 1

2
3
0 2 1
0 1 2
1 1 1
3
0 2 1
0 0 3
1 1 1

Sample Output 1

Yes
No

In the first test case, one scenario that satisfies the condition is as follows:

  • Participant 1 solves the first problem in 3 minutes, the second in 2 minutes, and the third in 2 minutes;
  • Participant 2 solves the first problem in 3 minutes, the second in 2 minutes, and the third in 3 minutes;
  • Participant 3 solves the first problem in 3 minutes, the second in 2 minutes, and the third in 1 minutes.
E - More Peaks More Fun

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

2N 枚のカードと N 個の箱があります。 カードには 1 から 2N までの番号が付いており、箱には 1 から N までの番号が付いています。 カードは各箱に 2 枚ずつ入っています。箱 i にはカード A_i とカード B_i が入っています。

この N 個の箱を一列に並べる方法であって、以下の条件を満たす並べ方の個数を 10^9+7 で割った余りを求めてください。

  • 箱を左から順に開けていき、入っている 2 枚のカードを末尾に好きな順で並べていくことで、長さ 2N のカードの列が得られる。左から j 番目のカードの番号を P_j とする。このとき、カードをうまく並べることで、数列 P_1,P_2,\ldots, P_{2N} におけるピークの個数が N-1 となる。

ただし、数列 P_1,P_2,\ldots, P_{2N} におけるピークとは、 2 \leq j < 2N なる整数 j であって P_{j-1} < P_j かつ P_j > P_{j+1} となるものを指します。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq 2N
  • A_1,\ldots,A_N,B_1,\ldots,B_N は相異なる。

入力

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

N
A_1 B_1
:
A_N B_N

出力

答えを出力せよ。


入力例 1

3
1 3
2 4
5 6

出力例 1

4

例えば、箱 1,2,3 をこの順で並べたとき、 以下のようにカードを並べることで、数列 P のピークの個数が 2 となります。

  • まず箱 1 に入っているカードをカード 1,3 の順で並べる。
  • 次に箱 2 に入っているカードをカード 2,4 の順で末尾に並べる。
  • 最後に箱 3 に入っているカードをカード 6,5 の順で末尾に並べる。

このとき、数列 P(1,3,2,4,6,5) となり j=2,5 がピークとなります。


入力例 2

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

出力例 2

492

入力例 3

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

出力例 3

1411200

Score : 1400 points

Problem Statement

We have 2N cards and N boxes. The cards are numbered 1 through 2N, and the boxes are numbered 1 through N. Each box contains two cards; Box i contains Card A_i and Card B_i.

Find, modulo (10^9+7), the number of ways to arrange the N boxes in a row that satisfies the following condition.

  • Consider getting a sequence of 2N cards as follows: for each box, from left to right, open it and append the two cards in it to the end of the sequence in an order we like. In this way, it is possible to get a sequence P_1,P_2,\ldots, P_{2N} with N-1 peaks.

Here, a peak in a sequence P_1,P_2,\ldots, P_{2N} is an integer j satisfying 2 \leq j < 2N such that P_{j-1} < P_j and P_j > P_{j+1}.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq 2N
  • A_1,\ldots,A_N,B_1,\ldots,B_N are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the answer.


Sample Input 1

3
1 3
2 4
5 6

Sample Output 1

4

For example, if we arrange Boxes 1, 2, 3 in this order, we can arrange the cards as follows to get a sequence P with 2 peaks:

  • first, arrange the cards in Box 1 in the order 1, 3;
  • next, append the cards in Box 2 at the end in the order 2, 4;
  • lastly, append the cards in Box 3 at the end in the order 6, 5.

Here, we have P=(1,3,2,4,6,5) with peaks j=2,5.


Sample Input 2

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

Sample Output 2

492

Sample Input 3

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

Sample Output 3

1411200
F - ESPers

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 2400

問題文

2N+1 人の参加者で、多数決というゲームを行います。各参加者は 2 つの選択肢の一方に投票し、最終的により多くの票を集めた選択肢に投票した参加者が勝者となります。投票は具体的には以下のように行われます。

  1. 全員が投票を終えた場合、そこで投票は終了する。そうでない場合、2. に進む。
  2. 投票をしていない参加者のうち 1 人がランダムに選ばれ、その人が投票を行う。そして、1. に戻る。

参加者のうち K 人は超能力者であり、自身の投票時に、各選択肢が何票投票されているのかを知ることができます。 そのため、各参加者は以下のようにして投票先を決定します。

  • 参加者が超能力者の場合、その時点でより多くの票を集めている選択肢に投票する。ただし、その時点で各選択肢の票数が等しい場合は、ランダムに一方の選択肢を選び投票する。
  • 参加者が超能力者でない場合、ランダムに一方の選択肢を選び投票する。

X はこのゲームの参加者であり、超能力者でもあります。X の勝率を \bmod 10^9+7 で求めてください(注記参照)。

注記

  • 求める確率は有理数となります。確率を分数 \frac{y}{x}xy は互いに素な正の整数)で表したとき、xP=10^9+7 と互いに素になるので、 xz \equiv y \pmod P なる 0 以上 P-1 以下の唯一の整数 z を出力してください。

制約

  • 1 \leq N \leq 2\times 10^6
  • 1 \leq K \leq 2N+1

入力

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

N K

出力

答えを出力せよ。


入力例 1

1 1

出力例 1

916666674

X の勝率は \frac{11}{12} です。


入力例 2

1 2

出力例 2

958333341

X の勝率は \frac{23}{24} です。


入力例 3

8 5

出力例 3

582281799

入力例 4

100 100

出力例 4

196654831

入力例 5

2000000 2000000

出力例 5

768385859

Score : 2400 points

Problem Statement

2N+1 players will play a game called Majority Vote. Each player casts a vote to one of the two options given, and the players choosing the option that ends up with the greater number of votes will win. The detailed progression of the vote is as follows:

  1. If everyone has already voted, the vote ends. Otherwise, proceed to step 2.
  2. One player is chosen randomly among the players who have not voted, and s/he casts a vote. Then, go back to step 1.

K of the players are ESPers, who can know the number of votes cast to each option when they cast votes. Thus, the players choose the option to cast as follows:

  • A player who is an ESPer chooses the option with the greater number of votes at the moment. If the two options have the same number of votes, s/he chooses one option randomly and votes for it.
  • A player who is not an ESPer randomly chooses one option and votes for it.

X is a player of the game who is also an ESPer. Find the probability that X wins, modulo (10^9+7) (see Notes).

Notes

  • The expected value in question will be a rational number. If we express it as a fractional number \frac{y}{x} where x and y are coprime positive integers, x will be coprime with P=10^9+7, so print the only integer z between 0 and P-1 (inclusive) such that xz \equiv y \pmod P.

Constraints

  • 1 \leq N \leq 2\times 10^6
  • 1 \leq K \leq 2N+1

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.


Sample Input 1

1 1

Sample Output 1

916666674

X wins with probability \frac{11}{12}.


Sample Input 2

1 2

Sample Output 2

958333341

X wins with probability \frac{23}{24}.


Sample Input 3

8 5

Sample Output 3

582281799

Sample Input 4

100 100

Sample Output 4

196654831

Sample Input 5

2000000 2000000

Sample Output 5

768385859