A - Colorful Slimes 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は異世界に住んでいます。この世界のスライムの色は 10000 色存在し,色 1, 2, ..., 10000 と呼ぶことにします。

高橋君は N 匹のスライムを飼っており,スライムは左右に一列に並んでいます。左から i 匹目のスライムの色は a_i です。 もし同じ色どうしのスライムが隣り合っていると,そのスライムたちは合体してしまいます。高橋君は小さいスライムのほうが好きなので,魔法でスライムの色を何匹か変えることにしました。

高橋君は魔法を唱えることで,どれか 1 匹のスライムの色を,10000 色のうち好きな色に変えることが出来ます。 どのスライムたちも合体しないようにするには,最小で何回魔法を唱える必要があるでしょうか?

制約

  • 2 \leq N \leq 100
  • 1 \leq a_i \leq N
  • 入力される値は全て整数である

入力

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

N
a_1 a_2 ... a_N

出力

高橋君が唱える必要のある魔法の最小回数を出力して下さい。


入力例 1

5
1 1 2 2 2

出力例 1

2

例えば左から 2 匹目のスライムの色を4,左から 4 匹目のスライムの色を 5 に変えると, スライムの色は 1, 4, 2, 5, 2 となり,これで条件を満たします。


入力例 2

3
1 2 1

出力例 2

0

1 匹目と 3 匹目のスライムは同じ色ですが隣り合っていないため,魔法を唱える必要はありません。


入力例 3

5
1 1 1 1 1

出力例 3

2

たとえば 2, 4 匹目のスライムの色を 2 に変えると,スライムの色は 1, 2, 1, 2, 1 となり,これで条件を満たします。


入力例 4

14
1 2 2 3 3 3 4 4 4 4 1 2 3 4

出力例 4

4

Score : 200 points

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?

Constraints

  • 2 \leq N \leq 100
  • 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 ... a_N

Output

Print the minimum number of spells required.


Sample Input 1

5
1 1 2 2 2

Sample Output 1

2

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.


Sample Input 2

3
1 2 1

Sample Output 2

0

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.


Sample Input 3

5
1 1 1 1 1

Sample Output 3

2

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.


Sample Input 4

14
1 2 2 3 3 3 4 4 4 4 1 2 3 4

Sample Output 4

4
B - rng_10s

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

コンビニエンスストアのりんごマートでは,りんごジュースを販売しています。

りんごマートはある日の朝に開店し,その時にはジュースの在庫が A 本ありました。 すぬけ君は毎日昼にりんごマートでジュースを B 本買います。 りんごマートでは毎日夜にジュースの在庫を確認し,C 本以下だった場合,次の日の朝までに D 本在庫を追加します。

すぬけ君がジュースを永遠に買い続けられるかを判定して下さい。 つまり,ジュースを買おうとした時,必ず在庫が B 本以上あるかどうかを判定して下さい。 すぬけ君以外がジュースを買うことはありません。

また,今回の問題では入力ケースは T 個のクエリからなります。

制約

  • 1 \leq T \leq 300
  • 1 \leq A, B, C, D \leq 10^{18}
  • 入力される値は全て整数である

入力

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

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

i 個目のクエリにおいては,A = A_i, B = B_i, C = C_i, D = D_i である。

出力

T 行出力せよ。i 行目には,i 個目のクエリですぬけ君が永遠にりんごジュースを買い続けられる場合 Yes,そうでない場合 No と出力せよ。


入力例 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

出力例 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

1 個目のクエリでは在庫の個数は以下のように変動します。

9 2 11 4 13 6 6 x

2 個目のクエリでは在庫の個数は以下のように変動します。

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

と続いていき,このまま永遠に購入し続けられます。


入力例 2

24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

出力例 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning. Snuke buys B cans of juice here every day in the daytime. Then, the manager checks the number of cans of juice remaining in stock every night. If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them. Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

  • 1 \leq T \leq 300
  • 1 \leq A, B, C, D \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.


Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9 D 2 N 11 D 4 N 13 D 6 N 6 D x

In the second query, the number of cans of juice in stock changes as follows:

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

and so on, thus Snuke can buy juice indefinitely.


Sample Input 2

24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No
C - String Coloring

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ 2N の,英小文字のみからなる文字列 S が与えられます。

S の各文字を赤色か青色かに塗り分ける方法は 2^{2N} 通りありますが,このうち以下の条件を満たす塗り分け方は何通りですか?

  • 赤色に塗られた文字を左から右に読んだ文字列と,青色に塗られた文字を右から左に読んだ文字列が一致する

制約

  • 1 \leq N \leq 18
  • S の長さは 2N である
  • S は英小文字のみからなる

入力

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

N
S

出力

条件を満たす塗り分け方の個数を出力せよ。


入力例 1

4
cabaacba

出力例 1

4

以下の 4 通りの塗り分け方が存在します。

  • cabaacba
  • cabaacba
  • cabaacba
  • cabaacba

入力例 2

11
mippiisssisssiipsspiim

出力例 2

504

入力例 3

4
abcdefgh

出力例 3

0

入力例 4

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

出力例 4

9075135300

答えは32bit整数型で表せないこともあります。

Score : 600 points

Problem Statement

You are given a string S of length 2N consisting of lowercase English letters.

There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition?

  • The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.

Constraints

  • 1 \leq N \leq 18
  • The length of S is 2N.
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to paint the string that satisfy the condition.


Sample Input 1

4
cabaacba

Sample Output 1

4

There are four ways to paint the string, as follows:

  • cabaacba
  • cabaacba
  • cabaacba
  • cabaacba

Sample Input 2

11
mippiisssisssiipsspiim

Sample Output 2

504

Sample Input 3

4
abcdefgh

Sample Output 3

0

Sample Input 4

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 4

9075135300

The answer may not be representable as a 32-bit integer.

D - Histogram Coloring

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

高さ 10^9 マス,幅 N マスのグリッドを考え,左から i(1 \leq i \leq N) 番目,下から j(1 \leq j \leq 10^9) 番目のマス目を (i, j) と表すことにします。

すぬけ君は各 i = 1, 2, ..., N について,左から i 列目のマスたちを,下から h_i 個を残すように切り取りました。 そして赤,青の絵の具を使い,マス目を絵の具で塗ります。 以下の条件を満たすような塗り分け方は何通りか求めて下さい。ただし答えは非常に大きくなるので,10^9+7 で割った余りを出力して下さい。

  • 全ての(切り取った後に残された)マスたちは,赤,青のどちらかの色に塗られている。
  • 全ての 1 \leq i \leq N-1, 1 \leq j \leq min(h_i, h_{i+1})-1 について,(i, j), (i, j+1), (i+1, j), (i+1, j+1) の4マスのなかにちょうど 2 個ずつ赤色と青色のマスが存在する。

制約

  • 1 \leq N \leq 100
  • 1 \leq h_i \leq 10^9

入力

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

N
h_1 h_2 ... h_N

出力

塗り分け方の個数を 10^9+7 で割った余りを出力せよ。


入力例 1

9
2 3 5 4 1 2 4 2 1

出力例 1

12800

以下に塗り分け方の一例を示します。

  #
  ##  #
 ###  #
#### ###
#########

入力例 2

2
2 2

出力例 2

6

以下の 6 通りの塗り分け方が存在します。

## ## ## ## ## ##
## ## ## ## ## ##


入力例 3

5
2 1 2 1 2

出力例 3

256

どのような塗り分け方も条件を満たします。


入力例 4

9
27 18 28 18 28 45 90 45 23

出力例 4

844733013

塗り分け方の個数を 10^9 + 7 で割った余りを出力することに注意して下さい。

Score : 1100 points

Problem Statement

Let us consider a grid of squares with 10^9 rows and N columns. Let (i, j) be the square at the i-th column (1 \leq i \leq N) from the left and j-th row (1 \leq j \leq 10^9) from the bottom.

Snuke has cut out some part of the grid so that, for each i = 1, 2, ..., N, the bottom-most h_i squares are remaining in the i-th column from the left. Now, he will paint the remaining squares in red and blue. Find the number of the ways to paint the squares so that the following condition is satisfied:

  • Every remaining square is painted either red or blue.
  • For all 1 \leq i \leq N-1 and 1 \leq j \leq min(h_i, h_{i+1})-1, there are exactly two squares painted red and two squares painted blue among the following four squares: (i, j), (i, j+1), (i+1, j) and (i+1, j+1).

Since the number of ways can be extremely large, print the count modulo 10^9+7.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq h_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 ... h_N

Output

Print the number of the ways to paint the squares, modulo 10^9+7.


Sample Input 1

9
2 3 5 4 1 2 4 2 1

Sample Output 1

12800

One of the ways to paint the squares is shown below:

  #
  ##  #
 ###  #
#### ###
#########

Sample Input 2

2
2 2

Sample Output 2

6

There are six ways to paint the squares, as follows:

## ## ## ## ## ##
## ## ## ## ## ##

Sample Input 3

5
2 1 2 1 2

Sample Output 3

256

Every way to paint the squares satisfies the condition.


Sample Input 4

9
27 18 28 18 28 45 90 45 23

Sample Output 4

844733013

Remember to print the number of ways modulo 10^9 + 7.

E - Synchronized Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

N 個の aN 個の b からなる,長さ 2N の文字列 S が与えられます。

あなたは S からいくつかの文字を選びます。ただし各 i = 1,2,...,N について,Si 番目に出現する ai 番目に出現する b から片方だけ選ぶことは出来ません。 そして選んだ文字たちを( S での順番通りに)結合します。

こうして得られる文字列のうち,辞書順で最大のものを求めて下さい。

制約

  • 1 \leq N \leq 3000
  • SN 個の ab からなる,長さ 2N の文字列である。

入力

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

N
S

出力

条件を満たす T のうち,辞書順で最大のものを出力して下さい。


入力例 1

3
aababb

出力例 1

abab

S1, 3, 4, 6 番目の文字からなる部分列 T は,条件を満たします。


入力例 2

3
bbabaa

出力例 2

bbabaa

全ての文字を選ぶことも可能です。


入力例 3

6
bbbaabbabaaa

出力例 3

bbbabaaa

入力例 4

9
abbbaababaababbaba

出力例 4

bbaababababa

Score : 1600 points

Problem Statement

You are given a string S of length 2N, containing N occurrences of a and N occurrences of b.

You will choose some of the characters in S. Here, for each i = 1,2,...,N, it is not allowed to choose exactly one of the following two: the i-th occurrence of a and the i-th occurrence of b. (That is, you can only choose both or neither.) Then, you will concatenate the chosen characters (without changing the order).

Find the lexicographically largest string that can be obtained in this way.

Constraints

  • 1 \leq N \leq 3000
  • S is a string of length 2N containing N occurrences of a and N occurrences of b.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically largest string that satisfies the condition.


Sample Input 1

3
aababb

Sample Output 1

abab

A subsequence of T obtained from taking the first, third, fourth and sixth characters in S, satisfies the condition.


Sample Input 2

3
bbabaa

Sample Output 2

bbabaa

You can choose all the characters.


Sample Input 3

6
bbbaabbabaaa

Sample Output 3

bbbabaaa

Sample Input 4

9
abbbaababaababbaba

Sample Output 4

bbaababababa
F - Manju Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 2000

問題文

N 個の箱が左右に一列に並んでいます。左から i 個目の箱にはまんじゅうが a_i 個入っています。 sugim君とsigma君はこの箱たちを使いゲームをします。 2人は交互に以下の操作を行います。先手はsugim君で,計 N 回操作を行ったらゲームを終了します。

  • 相手が最後にコマを入れた箱に隣り合う箱で,まだコマが入っていないものを選び,コマを入れる。条件を満たす箱が複数ある場合,好きな箱を 1 つ選べる。
  • 上記の条件を満たす箱が存在しない場合,及びsugim君の最初の操作では,まだコマが入っていない箱をどれか好きに 1 つ選び,コマを入れる。

最終的に 2 人は,自分がコマを入れた箱のまんじゅうたちを得ることが出来ます。 2 人は十分に頭がよく,またまんじゅうが大好きなので,自分が得られるまんじゅうの個数を最大化するために最適に行動を行います。

2 人が得られるまんじゅうの個数はいくつになるかを求めて下さい。

制約

  • 2 \leq N \leq 300,000
  • 1 \leq a_i \leq 1000
  • 入力される値は全て整数である

入力

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

N
a_1 a_2 ... a_N

出力

sugim君が得られるまんじゅうの個数と,sigma君が得られるまんじゅうの個数を,この順で空白区切りで出力せよ。


入力例 1

5
20 100 10 1 10

出力例 1

120 21

二人が最適に行動した場合,以下のようにゲームは進んでいきます。

  • sugim君は最初に左から 2 番目の箱にコマを入れる必要があります。
  • 次にsigma君は左端の箱にコマを入れる必要があります。
  • 次にsugim君は左から 3 番目,もしくは 5 番目の箱にコマを入れます。
  • 次にsigma君は左から 4 番目の箱にコマを入れます。
  • 最後にsugim君は残った箱にコマを入れます。

入力例 2

6
4 5 1 1 4 5

出力例 2

11 9

入力例 3

5
1 10 100 10 1

出力例 3

102 20

Score : 2000 points

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains a_i manju (buns stuffed with bean paste). Sugim and Sigma play a game using these boxes. They alternately perform the following operation. Sugim goes first, and the game ends when a total of N operations are performed.

  • Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's last operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.
  • If there is no box that satisfies the condition above, or this is Sugim's first operation, choose any one box that still does not contain a piece, then put a piece in that box.

At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.

Find the number of manju that each player will have at the end of the game.

Constraints

  • 2 \leq N \leq 300 000
  • 1 \leq a_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the numbers of Sugim's manju and Sigma's manju at the end of the game, in this order, with a space in between.


Sample Input 1

5
20 100 10 1 10

Sample Output 1

120 21

If the two performs the optimal moves, the game proceeds as follows:

  • First, Sugim has to put his piece in the second box from the left.
  • Then, Sigma has to put his piece in the leftmost box.
  • Then, Sugim puts his piece in the third or fifth box.
  • Then, Sigma puts his piece in the fourth box.
  • Finally, Sugim puts his piece in the remaining box.

Sample Input 2

6
4 5 1 1 4 5

Sample Output 2

11 9

Sample Input 3

5
1 10 100 10 1

Sample Output 3

102 20