実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
サンタさんに手紙を出したい高橋くんは、 X 円切手が 1 枚だけ貼られた封筒を用意しました。
サンタさんに手紙を届けるためには、貼られている切手の総額が Y 円以上である必要があります。
高橋くんは、この封筒に 10 円切手を何枚か貼り足すことで、貼られている切手の総額を Y 円以上にしたいです。
高橋くんはこの封筒に、最小で何枚の 10 円切手を貼り足す必要がありますか?
制約
- X,Y は整数
- 1 \le X,Y \le 1000
入力
入力は以下の形式で標準入力から与えられる。
X Y
出力
答えを整数として出力せよ。
入力例 1
80 94
出力例 1
2
- 80 円切手に 0 枚の 10 円切手を貼り足せば総額が 80 円となり、これは手紙を届けるのに必要な 94 円未満です。
- 80 円切手に 1 枚の 10 円切手を貼り足せば総額が 90 円となり、これは手紙を届けるのに必要な 94 円未満です。
- 80 円切手に 2 枚の 10 円切手を貼り足せば総額が 100 円となり、これは手紙を届けるのに必要な 94 円以上です。
入力例 2
1000 63
出力例 2
0
もともと貼られている切手だけで金額が十分である可能性もあります。
入力例 3
270 750
出力例 3
48
Score : 100 points
Problem Statement
Takahashi wants to send a letter to Santa Claus. He has an envelope with an X-yen (Japanese currency) stamp stuck on it.
To be delivered to Santa Claus, the envelope must have stamps in a total value of at least Y yen.
Takahashi will put some more 10-yen stamps so that the envelope will have stamps worth at least Y yen in total.
At least how many more 10-yen stamps does Takahashi need to put on the envelope?
Constraints
- X and Y are integers.
- 1 \le X,Y \le 1000
Input
Input is given from Standard Input in the following format:
X Y
Output
Print the answer as an integer.
Sample Input 1
80 94
Sample Output 1
2
- After adding zero 10-yen stamps to the 80-yen stamp, the total is 80 yen, which is less than the required amount of 94 yen.
- After adding one 10-yen stamp to the 80-yen stamp, the total is 90 yen, which is less than the required amount of 94 yen.
- After adding two 10-yen stamps to the 80-yen stamp, the total is 100 yen, which is not less than the required amount of 94 yen.
Sample Input 2
1000 63
Sample Output 2
0
The envelope may already have a stamp with enough value.
Sample Input 3
270 750
Sample Output 3
48
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
S から a, e, i, o, u をすべて取り除いて得られる文字列を出力してください。
なお、S は a, e, i, o, u 以外の文字を一つ以上含みます。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
- S は
a,e,i,o,u以外の文字を一つ以上含む
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
atcoder
出力例 1
tcdr
S = atcoder のとき、1, 4, 6 文字目を取り除いて tcdr を得ます。
入力例 2
xyz
出力例 2
xyz
入力例 3
aaaabbbbcccc
出力例 3
bbbbcccc
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Remove all occurrences of a, e, i, o, u from S and print the resulting string.
S contains at least one character other than a, e, i, o, u.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
- S contains at least one character other than
a,e,i,o,u.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
atcoder
Sample Output 1
tcdr
For S = atcoder, remove the 1-st, 4-th, and 6-th characters to get tcdr.
Sample Input 2
xyz
Sample Output 2
xyz
Sample Input 3
aaaabbbbcccc
Sample Output 3
bbbbcccc
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
縦 8 マス、横 8 マスの 64 マスからなるマス目があります。 上から i 行目 (1\leq i\leq8) 、左から j 列目 (1\leq j\leq8) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。
マスの状態は長さ 8 の文字列からなる長さ 8 の列 (S _ 1,S _ 2,S _ 3,\ldots,S _ 8) で表されます。
マス (i,j) (1\leq i\leq8,1\leq j\leq8) は、S _ i の j 文字目が . のとき空マスで、# のときコマが置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどちらかの条件を満たすコマを取ることができます。
- i 行目のマスに置かれている
- j 列目のマスに置かれている
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。

あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- S _ i は
.,#からなる長さ 8 の文字列 (1\leq i\leq 8)
入力
入力は以下の形式で標準入力から与えられる。
S _ 1 S _ 2 S _ 3 S _ 4 S _ 5 S _ 6 S _ 7 S _ 8
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
出力例 1
4
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。

よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスはマス (6,6), マス (6,7), マス (7,6), マス (7,7) の 4 マスです。
入力例 2
........ ........ ........ ........ ........ ........ ........ ........
出力例 2
64
コマがひとつも置かれていないこともあります。
入力例 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
出力例 3
4
Score : 200 points
Problem Statement
There is a grid of 64 squares with 8 rows and 8 columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq8) and j-th column from the left (1\leq j\leq8).
Each square is either empty or has a piece placed on it.
The state of the squares is represented by a sequence (S_1,S_2,S_3,\ldots,S_8) of 8 strings of length 8.
Square (i,j) (1\leq i\leq8,1\leq j\leq8) is empty if the j-th character of S_i is ., and has a piece if it is #.
You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.
A piece placed on square (i,j) can capture pieces that satisfy either of the following conditions:
- Placed on a square in row i
- Placed on a square in column j
For example, a piece placed on square (4,4) can capture pieces placed on the squares shown in blue in the following figure:

How many squares can you place your piece on?
Constraints
- Each S_i is a string of length 8 consisting of
.and#(1\leq i\leq 8).
Input
The input is given from Standard Input in the following format:
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
Sample Output 1
4
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece without it being captured on 4 squares: square (6,6), square (6,7), square (7,6), and square (7,7).
Sample Input 2
........ ........ ........ ........ ........ ........ ........ ........
Sample Output 2
64
There may be no pieces on the grid.
Sample Input 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。
1 x: A の末尾に x を追加する。2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。
制約
- 1 \leq Q \leq 100
- 1 種類目のクエリにおいて x は 1 \leq x \leq 10^9 を満たす整数
- 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
クエリは以下の 2 つのいずれかの形式である。
1 x
2 k
出力
2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。
入力例 1
5 1 20 1 30 2 1 1 40 2 3
出力例 1
30 20
- 最初 A は空である。
- 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
- 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
- 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
- 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
- 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。
Score: 200 points
Problem Statement
You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:
1 x: Append x to the end of A.2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.
Constraints
- 1 \leq Q \leq 100
- In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
- In the second type of query, k is a positive integer not greater than the current length of sequence A.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in one of the following two formats:
1 x
2 k
Output
Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.
Sample Input 1
5 1 20 1 30 2 1 1 40 2 3
Sample Output 1
30 20
- Initially, A is empty.
- The first query appends 20 to the end of A, making A=(20).
- The second query appends 30 to the end of A, making A=(20,30).
- The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
- The fourth query appends 40 to the end of A, making A=(20,30,40).
- The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
キーエンス本社に勤務する人数が増えてきたので、本社に存在する部署を 2 つのグループに分け、昼休みの時間帯を分けることにしました。
キーエンス本社には N 個の部署が存在し、i 番目 (1\leq i\leq N) の部署に所属する人数は K_i 人です。
それぞれの部署をグループ A, B のいずれか一方に割り当て、グループごとに同時に昼休みをとり、
かつグループ A, B の昼休みの時間が重ならないようにしたとき、同時に昼休みをとる最大人数としてあり得る最小の値を求めてください。
すなわち、グループ A に割り当てられた部署に所属する人数の合計とグループ B に割り当てられた部署に所属する人数の合計
のうち大きい方の値としてあり得る最小の値を求めてください。
制約
- 2\leq N \leq 20
- 1\leq K_i \leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K_1 K_2 \ldots K_N
出力
同時に昼休みを取る最大人数としてあり得る最小の値を出力せよ。
入力例 1
5 2 3 5 10 12
出力例 1
17
1,2,5 番目の部署をグループ A に、3,4 番目の部署をグループ B に割り当てたとき、 グループ A に割り当てられた部署に所属する人数の合計は 2+3+12=17 、 グループ B に割り当てられた部署に所属する人数の合計は 5+10=15 となり、 このとき同時に昼休みを取る最大人数は 17 となります。
一方で、グループ A,B それぞれに割り当てられた部署に所属する人数の合計がいずれも 16 以下になるように 部署を割り当てることはできないため、17 を出力します。
入力例 2
2 1 1
出力例 2
1
同一人数の部署が複数存在する可能性もあります。
入力例 3
6 22 25 26 45 22 31
出力例 3
89
例えば、1,4,5 番目の部署をグループ A に、2,3,6 番目の部署をグループ B に割り当てたとき同時に昼休みを取る最大人数は 89 となります。
Score : 300 points
Problem Statement
As KEYENCE headquarters have more and more workers, they decided to divide the departments in the headquarters into two groups and stagger their lunch breaks.
KEYENCE headquarters have N departments, and the number of people in the i-th department (1\leq i\leq N) is K_i.
When assigning each department to Group A or Group B, having each group take lunch breaks at the same time, and ensuring that the lunch break times of Group A and Group B do not overlap, find the minimum possible value of the maximum number of people taking a lunch break at the same time.
In other words, find the minimum possible value of the larger of the following: the total number of people in departments assigned to Group A, and the total number of people in departments assigned to Group B.
Constraints
- 2 \leq N \leq 20
- 1 \leq K_i \leq 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K_1 K_2 \ldots K_N
Output
Print the minimum possible value of the maximum number of people taking a lunch break at the same time.
Sample Input 1
5 2 3 5 10 12
Sample Output 1
17
When assigning departments 1, 2, and 5 to Group A, and departments 3 and 4 to Group B, Group A has a total of 2+3+12=17 people, and Group B has a total of 5+10=15 people. Thus, the maximum number of people taking a lunch break at the same time is 17.
It is impossible to assign the departments so that both groups have 16 or fewer people, so print 17.
Sample Input 2
2 1 1
Sample Output 2
1
Multiple departments may have the same number of people.
Sample Input 3
6 22 25 26 45 22 31
Sample Output 3
89
For example, when assigning departments 1, 4, and 5 to Group A, and departments 2, 3, and 6 to Group B, the maximum number of people taking a lunch break at the same time is 89.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
N 種類の薬品 1,2,\dots,N があります。あなたの目標はこれらが全て混ざった状態にすることです。
0, 1 からなる長さ 2^N-1 の文字列 S が与えられます。この文字列は次の情報を表します。
- まず、 1 種類以上の薬品が混ざった状態 i ( 1 \le i \le 2^N-1 ) を次のように定義する。
- i を 2 進法で表記した際に下から k ( 1 \le k \le N ) 桁目が 1 である、またその時に限り、薬品 k が含まれている。
- 例えば、 13 を 2 進法で表記すると 1101_{(2)} となるため、状態 13 は薬品 1,3,4 が混ざった状態を表現します。
- S の i 文字目が
0であるとき、状態 i は 安全 である。 - S の i 文字目が
1であるとき、状態 i は 危険 である。
あなたは次の操作を使って薬品を混ぜ合わせます。
- まず、空の瓶を用意する。
- 次に、以下を繰り返す。
- まだ瓶に注いでいない薬品を 1 種類選択し、瓶に注ぐ。
- この時、瓶の中で混ざった薬品が危険な状態であってはならない。
この操作によって全ての薬品が混ざった状態を作れるか判定してください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- T は 1 以上 40000 以下の整数
- N は 1 以上 18 以下の整数
- S は
0,1からなる長さ 2^N-1 の文字列 - ひとつの入力に含まれる |S| = 2^N-1 の総和は 5 \times 10^5 を超えない
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。
各テストケースについて、全ての薬品が混ざった状態を作れるなら Yes 、作れないなら No と出力せよ。
入力例 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
出力例 1
Yes No No Yes Yes
この入力には 5 個のテストケースが含まれます。
1 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 のみが危険で、他の状態は安全である。
- 例えば、以下の手順で全ての薬品が混ざった状態を作ることができます。
- はじめに、瓶に薬品 2 を注ぐ。瓶の中で薬品 2 のみが混ざっており、これは状態 2 なので安全である。
- 次に、瓶に薬品 3 を注ぐ。瓶の中で薬品 2,3 が混ざっており、これは状態 6 なので安全である。
- 最後に、瓶に薬品 1 を注ぐ。瓶の中で薬品 1,2,3 が混ざっており、これは状態 7 なので安全である。
2 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 、薬品 1,3 が混ざった状態 5 、薬品 2,3 が混ざった状態 6 が危険で、他の状態は安全である。
- このケースについて、全ての薬品が混ざった状態を作ることはできません。
3 番目のケースは次の通りです。
- 1 種類の薬品が存在する。
- 薬品 1 のみが混ざった状態 1 が危険であるため、全ての薬品が混ざった状態を作ることはできません。
Score : 350 points
Problem Statement
There are N types of chemicals 1,2,\dots,N. Your goal is to create a state where all of them are mixed.
You are given a string S of length 2^N-1 consisting of 0 and 1, which represents the following information:
- First, define state i (1 \le i \le 2^N-1) where one or more types of chemicals are mixed as follows:
- When the k-th digit (1 \le k \le N) from the bottom in the binary representation of i is 1, and only then, chemical k is included.
- For example, 13 in binary is 1101_{(2)}, so state 13 represents a state where chemicals 1,3,4 are mixed.
- When the i-th character of S is
0, state i is safe. - When the i-th character of S is
1, state i is dangerous.
You mix chemicals using the following operation:
- First, prepare an empty bottle.
- Next, repeat the following:
- Choose one type of chemical that has not yet been poured into the bottle and pour it into the bottle.
- At this time, the chemicals mixed in the bottle must not be in a dangerous state.
Determine whether this operation can create a state where all chemicals are mixed.
You are given T test cases; solve each of them.
Constraints
- T is an integer between 1 and 40000, inclusive.
- N is an integer between 1 and 18, inclusive.
- S is a string of length 2^N-1 consisting of
0and1. - The sum of |S| = 2^N-1 in each input does not exceed 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N S
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if it is possible to create a state where all chemicals are mixed, print Yes; otherwise, print No.
Sample Input 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
Sample Output 1
Yes No No Yes Yes
This input contains five test cases.
The 1st case is as follows:
- There are three types of chemicals.
- Only state 3 where chemicals 1,2 are mixed is dangerous, and the other states are safe.
- For example, you can create a state where all chemicals are mixed with the following procedure:
- First, pour chemical 2 into the bottle. Only chemical 2 is mixed in the bottle, which is state 2, so it is safe.
- Next, pour chemical 3 into the bottle. Chemicals 2,3 are mixed in the bottle, which is state 6, so it is safe.
- Finally, pour chemical 1 into the bottle. Chemicals 1,2,3 are mixed in the bottle, which is state 7, so it is safe.
The 2nd case is as follows:
- There are three types of chemicals.
- State 3 where chemicals 1,2 are mixed, state 5 where chemicals 1,3 are mixed, and state 6 where chemicals 2,3 are mixed are dangerous, and the other states are safe.
- For this case, it is impossible to create a state where all chemicals are mixed.
The 3rd case is as follows:
- There is one type of chemical.
- Since state 1 where only chemical 1 is mixed is dangerous, it is impossible to create a state where all chemicals are mixed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正の整数 k について、サイズ k の ピラミッド数列 とは、長さ (2k-1) の数列であって各項の値が順に 1,2,\ldots,k-1,k,k-1,\ldots,2,1 であるようなものをさします。
長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A に対して、次の操作のうち一方を選んで行うことを繰り返して (0 回でも良い) 得ることのできるピラミッド数列のサイズの最大値を求めてください。
- 数列の項を 1 つ選び、その項の値を 1 減少させる。
- 先頭または末尾の項を削除する。
なお、問題の制約のもとで、操作を繰り返すことで必ず 1 種類以上のピラミッド数列を得ることができることが証明できます。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
数列 A に問題文の操作を繰り返して得ることのできるピラミッド数列のサイズの最大値を出力せよ。
入力例 1
5 2 2 3 1 1
出力例 1
2
A=(2,2,3,1,1) から始めて、 次のようにして数列 A からサイズ 2 のピラミッド数列を作る事ができます。
- 第 3 項を選び、1 減少させる。数列は A=(2,2,2,1,1) となる。
- 先頭を削除する。数列は A=(2,2,1,1) となる。
- 末尾を削除する。数列は A=(2,2,1) となる。
- 第 1 項を選び、1 減少させる。数列は A=(1,2,1) となる。
(1,2,1) はサイズ 2 のピラミッド数列です。
一方、どのように操作を行ってもサイズ 3 以上のピラミッド数列を作ることはできないため 2 を出力します。
入力例 2
5 1 2 3 4 5
出力例 2
3
入力例 3
1 1000000000
出力例 3
1
Score: 400 points
Problem Statement
For a positive integer k, the Pyramid Sequence of size k is a sequence of length (2k-1) where the terms of the sequence have the values 1,2,\ldots,k-1,k,k-1,\ldots,2,1 in this order.
You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N.
Find the maximum size of a Pyramid Sequence that can be obtained by repeatedly choosing and performing one of the following operations on A (possibly zero times).
- Choose one term of the sequence and decrease its value by 1.
- Remove the first or last term.
It can be proved that the constraints of the problem guarantee that at least one Pyramid Sequence can be obtained by repeating the operations.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- 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
Print the maximum size of the Pyramid Sequence that can be obtained by repeatedly performing the operations described in the problem statement on the sequence A.
Sample Input 1
5 2 2 3 1 1
Sample Output 1
2
Starting with A=(2,2,3,1,1), you can create a Pyramid Sequence of size 2 as follows:
- Choose the third term and decrease it by 1. The sequence becomes A=(2,2,2,1,1).
- Remove the first term. The sequence becomes A=(2,2,1,1).
- Remove the last term. The sequence becomes A=(2,2,1).
- Choose the first term and decrease it by 1. The sequence becomes A=(1,2,1).
(1,2,1) is a Pyramid Sequence of size 2.
On the other hand, there is no way to perform the operations to create a Pyramid Sequence of size 3 or larger, so you should print 2.
Sample Input 2
5 1 2 3 4 5
Sample Output 2
3
Sample Input 3
1 1000000000
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
N+1 個の数列 A_0, A_1, \ldots, A_{N} があります。A_i は以下のように定義されます。
- A_0 は空の数列
- A_i\ (1\leq i\leq N) は数列 A_{x_i}\ (0\leq x_i\lt i) の末尾に整数 y_i を追加することで得られる数列
以下の条件を満たす (1,2,\ldots,N) の順列 P=(P_1, P_2,\ldots,P_N) を求めてください。
- i = 1,2,\ldots,N-1 に対して、以下のいずれかが成り立つ。
- A_{P_i} は辞書順で A_{P_{i+1}} より小さい。
- A_{P_i}= A_{P_{i+1}} かつ P_i\lt P_{i+1}
言い換えると、A_1,A_2,\ldots,A_N を辞書順で小さいものから順に並べたとき(等しい列が複数ある場合には添え字が小さいものを先にするように並べる)、その並びに現れる添え字の列が P です。
数列の辞書順とは?
数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。
- |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})。
- ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i が T_i より(数として)小さい。
制約
- 1\leq N\leq 3\times 10^5
- 0\leq x_i\lt i
- 1\leq y_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
P_1, P_2, \ldots, P_N を空白区切りで一行で出力せよ。
入力例 1
4 0 2 0 1 2 2 0 1
出力例 1
2 4 3 1
A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1) なので P=(2,4,3,1) です。
入力例 2
5 0 1 0 1 0 1 0 1 0 1
出力例 2
1 2 3 4 5
A_1 = A_2 = A_3 = A_4 = A_5 = (1) です。
入力例 3
10 0 305186313 1 915059758 0 105282054 1 696409999 3 185928366 3 573289179 6 254538849 3 105282054 5 696409999 8 168629803
出力例 3
3 8 10 5 9 6 7 1 4 2
Score : 450 points
Problem Statement
There are N+1 sequences A_0, A_1, \ldots, A_{N}. A_i is defined as follows:
- A_0 is an empty sequence.
- A_i\ (1\leq i\leq N) is a sequence obtained by appending an integer y_i to the end of the sequence A_{x_i}\ (0\leq x_i\lt i).
Find the permutation P=(P_1, P_2,\ldots,P_N) of (1,2,\ldots,N) that satisfies the following condition:
- For i = 1,2,\ldots,N-1, one of the following holds:
- A_{P_i} is lexicographically smaller than A_{P_{i+1}}.
- A_{P_i}= A_{P_{i+1}} and P_i\lt P_{i+1}
In other words, when A_1,A_2,\ldots,A_N are arranged in lexicographical order (when there are multiple equal sequences, arrange those with smaller indices first), P is the sequence of indices that appears in that arrangement.
What is the lexicographical order of sequences?
A sequence S = (S_1,S_2,\ldots,S_{|S|}) is lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if one of the following two conditions holds. Here, |S| and |T| represent the lengths of S and T, respectively.
- |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
- There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i is (numerically) smaller than T_i.
Constraints
- 1\leq N\leq 3\times 10^5
- 0\leq x_i\lt i
- 1\leq y_i\leq 10^9
- 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
Output P_1, P_2, \ldots, P_N in one line, separated by spaces.
Sample Input 1
4 0 2 0 1 2 2 0 1
Sample Output 1
2 4 3 1
A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1), so P=(2,4,3,1).
Sample Input 2
5 0 1 0 1 0 1 0 1 0 1
Sample Output 2
1 2 3 4 5
A_1 = A_2 = A_3 = A_4 = A_5 = (1).
Sample Input 3
10 0 305186313 1 915059758 0 105282054 1 696409999 3 185928366 3 573289179 6 254538849 3 105282054 5 696409999 8 168629803
Sample Output 3
3 8 10 5 9 6 7 1 4 2
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
半径で切って N 等分された円形のケーキがあります。
各ピースには時計回りに 1, 2, \ldots, N の番号が付けられています。また、1 \leq i \leq N なる整数 i についてピース i をピース N + i とも呼ぶこととします。
はじめ、すべてのピースの色は色 0 です。
あなたは、以下の操作を好きな回数行うことができます。
- 1 \leq a, b, c \leq N なる整数 a, b, c を選ぶ。0 \leq i < b なる各整数 i に対してピース a + i の色を色 c に変更する。この操作にはコストが b + X_c かかる。
1 \leq i \leq N なるすべての整数 i に対してピース i の色を色 C_i にするために必要なコストの合計の最小値を求めてください。
制約
- 1 \leq N \leq 400
- 1 \leq C_i \leq N
- 1 \leq X_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
出力
答えを出力せよ。
入力例 1
6 1 4 2 1 2 5 1 2 3 4 5 6
出力例 1
20
ピース i の色が色 A_i であるとします。はじめ、(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0) です。
(a, b, c) = (2, 1, 4) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 0, 0, 0, 0) となります。
(a, b, c) = (3, 3, 2) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 2, 2, 2, 0) となります。
(a, b, c) = (1, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 2, 2, 0) となります。
(a, b, c) = (4, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 0) となります。
(a, b, c) = (6, 1, 5) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 5) となります。
このとき、コストの合計は 5 + 5 + 2 + 2 + 6 = 20 となります。
入力例 2
5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
5000000005
入力例 3
8 2 3 3 1 2 1 3 1 3 4 1 2 5 3 1 2
出力例 3
23
Score : 550 points
Problem Statement
There is a circular cake that has been cut into N equal slices by its radii.
Each piece is labeled with an integer from 1 to N in clockwise order, and for each integer i with 1 \leq i \leq N, the piece i is also referred to as piece N + i.
Initially, every piece’s color is color 0.
You can perform the following operation any number of times:
- Choose integers a, b, and c such that 1 \leq a, b, c \leq N. For each integer i with 0 \leq i < b, change the color of piece a + i to color c. The cost of this operation is b + X_c.
You want each piece i (for 1 \leq i \leq N) to have color C_i. Find the minimum total cost of operations needed to achieve this.
Constraints
- 1 \leq N \leq 400
- 1 \leq C_i \leq N
- 1 \leq X_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
Output
Print the answer.
Sample Input 1
6 1 4 2 1 2 5 1 2 3 4 5 6
Sample Output 1
20
Let A_i denote the color of piece i. Initially, (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0).
Performing an operation with (a, b, c) = (2, 1, 4) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 0, 0, 0, 0).
Performing an operation with (a, b, c) = (3, 3, 2) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 2, 2, 2, 0).
Performing an operation with (a, b, c) = (1, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 2, 2, 0).
Performing an operation with (a, b, c) = (4, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 0).
Performing an operation with (a, b, c) = (6, 1, 5) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 5).
In this case, the total cost is 5 + 5 + 2 + 2 + 6 = 20.
Sample Input 2
5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
5000000005
Sample Input 3
8 2 3 3 1 2 1 3 1 3 4 1 2 5 3 1 2
Sample Output 3
23