Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
(1,2,\ldots,N) の並び替えである数列 A=(A_1,\ldots,A_N) が与えられます。
次の操作を 0 回以上 N-1 回以下行うことで、A を (1,2,\ldots,N) にしてください。
- 操作:1\leq i < j \leq N を満たす整数の組 (i,j) を自由に選ぶ。A の i 番目と j 番目の要素を入れ替える。
なお、制約の条件下で必ず A を (1,2,\ldots,N) にできることが証明できます。
制約
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) は (1,2,\ldots,N) の並び替えである
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
操作回数を K として、K+1 行出力せよ。
1 行目には K を出力せよ。
l+1 行目 (1\leq l \leq K) には l 回目の操作で選ぶ整数 i,j を空白区切りで出力せよ。
問題文中の条件を満たすどのような出力も正解とみなされる。
入力例 1
5 3 4 1 2 5
出力例 1
2 1 3 2 4
操作により数列は次のように変化します。
- 最初 A=(3,4,1,2,5) である。
- 1 回目の操作で 1 番目の要素と 3 番目の要素を入れ替える。A=(1,4,3,2,5) になる。
- 2 回目の操作で 2 番目の要素と 4 番目の要素を入れ替える。A=(1,2,3,4,5) になる。
この他、次のような出力でも正解とみなされます。
4 2 3 3 4 1 2 2 3
入力例 2
4 1 2 3 4
出力例 2
0
入力例 3
3 3 1 2
出力例 3
2 1 2 2 3
Score: 300 points
Problem Statement
You are given a permutation A=(A_1,\ldots,A_N) of (1,2,\ldots,N).
Transform A into (1,2,\ldots,N) by performing the following operation between 0 and N-1 times, inclusive:
- Operation: Choose any pair of integers (i,j) such that 1\leq i < j \leq N. Swap the elements at the i-th and j-th positions of A.
It can be proved that under the given constraints, it is always possible to transform A into (1,2,\ldots,N).
Constraints
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) is a permutation of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Let K be the number of operations. Print K+1 lines.
The first line should contain K.
The (l+1)-th line (1\leq l \leq K) should contain the integers i and j chosen for the l-th operation, separated by a space.
Any output that satisfies the conditions in the problem statement will be considered correct.
Sample Input 1
5 3 4 1 2 5
Sample Output 1
2 1 3 2 4
The operations change the sequence as follows:
- Initially, A=(3,4,1,2,5).
- The first operation swaps the first and third elements, making A=(1,4,3,2,5).
- The second operation swaps the second and fourth elements, making A=(1,2,3,4,5).
Other outputs such as the following are also considered correct:
4 2 3 3 4 1 2 2 3
Sample Input 2
4 1 2 3 4
Sample Output 2
0
Sample Input 3
3 3 1 2
Sample Output 3
2 1 2 2 3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder Land には 1 から N までの番号が付けられた N 個のポップコーン売り場があります。 売られているポップコーンの味には味 1,2,\dots,M の M 種類がありますが、すべての売り場ですべての味のポップコーンを売っているわけではありません。
高橋君は、それぞれのポップコーン売り場でどの味のポップコーンを売っているかの情報を入手しました。
この情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N によって表され、S_i の j 文字目が o であるとき売り場 i が味 j のポップコーンを売っていることを、
x であるとき売っていないことを示します。
どの売り場も最低 1 種類の味のポップコーンを売っており、どの味のポップコーンも最低 1 つの売り場で売られています。
高橋君は全種類のポップコーンを食べたがっていますが、あまり何度も移動はしたくありません。 高橋君がすべての味のポップコーンを購入するためには最低何個の売り場を訪れる必要があるか求めてください。
制約
- N,M は整数
- 1\leq N,M \leq 10
- S_i は
oおよびxからなる長さ M の文字列 - すべての i\ (1\leq i\leq N) について、S_i の中には
oが 1 つ以上存在する - すべての j\ (1\leq j\leq M) について、S_i の j 文字目が
oであるような i が 1 つ以上存在する
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
高橋君がすべての味のポップコーンを購入するために訪れる必要がある売り場の個数の最小値を出力せよ。
入力例 1
3 5 oooxx xooox xxooo
出力例 1
2
1 つめの売り場と 3 つめの売り場を訪れることで、すべての味のポップコーンを購入することができます。 1 つの売り場ですべての味のポップコーンを購入することはできないので、答えは 2 です。
入力例 2
3 2 oo ox xo
出力例 2
1
入力例 3
8 6 xxoxxo xxoxxx xoxxxx xxxoxx xxoooo xxxxox xoxxox oxoxxo
出力例 3
3
Score : 300 points
Problem Statement
In AtCoder Land, there are N popcorn stands numbered 1 to N. They have M different flavors of popcorn, labeled 1, 2, \dots, M, but not every stand sells all flavors of popcorn.
Takahashi has obtained information about which flavors of popcorn are sold at each stand. This information is represented by N strings S_1, S_2, \dots, S_N of length M. If the j-th character of S_i is o, it means that stand i sells flavor j of popcorn. If it is x, it means that stand i does not sell flavor j. Each stand sells at least one flavor of popcorn, and each flavor of popcorn is sold at least at one stand.
Takahashi wants to try all the flavors of popcorn but does not want to move around too much. Determine the minimum number of stands Takahashi needs to visit to buy all the flavors of popcorn.
Constraints
- N and M are integers.
- 1 \leq N, M \leq 10
- Each S_i is a string of length M consisting of
oandx. - For every i (1 \leq i \leq N), there is at least one
oin S_i. - For every j (1 \leq j \leq M), there is at least one i such that the j-th character of S_i is
o.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the minimum number of stands Takahashi needs to visit to buy all the flavors of popcorn.
Sample Input 1
3 5 oooxx xooox xxooo
Sample Output 1
2
By visiting the 1st and 3rd stands, you can buy all the flavors of popcorn. It is impossible to buy all the flavors from a single stand, so the answer is 2.
Sample Input 2
3 2 oo ox xo
Sample Output 2
1
Sample Input 3
8 6 xxoxxo xxoxxx xoxxxx xxxoxx xxoooo xxxxox xoxxox oxoxxo
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) と表記します。
各マスはスタートマス・ゴールマス・空きマス・障害物マスのいずれかであり、その情報は H 個の長さ W の文字列 S_1,S_2,\dots,S_H によって表されます。
具体的には、マス (i,j) は S_i の j 文字目が S であるときスタートマス、G であるときゴールマス、. であるとき空きマス、# であるとき障害物マスです。
ここで、スタートマスとゴールマスはちょうど 1 つずつ存在することが保証されます。
あなたは今スタートマスにいます。 あなたの目標は、今いるマスと辺で隣接するマスに移動することを繰り返してゴールマスへ行くことです。 ただし、障害物マスやグリッドの外に移動することはできず、また縦移動と横移動を 1 回ずつ交互に行わなければなりません。(最初の移動の向きは任意です。)
ゴールマスへ行くことが可能であるか判定し、可能ならば移動回数の最小値を求めてください。
より形式的には、以下の条件をすべて満たすマスの列 (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) が存在するか判定し、存在するならば k-1 の最小値を求めてください。
- すべての 1\leq l\leq k について、1\leq i_l\leq H かつ 1\leq j_l\leq W であり、(i_l,j_l) は障害物マスでない
- (i_1,j_1) はスタートマス
- (i_k,j_k) はゴールマス
- すべての 1\leq l\leq k-1 について、|i_l-i_{l+1}|+|j_l-j_{l+1}|=1
- すべての 1\leq l\leq k-2 について、i_l\neq i_{l+1} ならば i_{l+1}=i_{l+2}
- すべての 1\leq l\leq k-2 について、j_l\neq j_{l+1} ならば j_{l+1}=j_{l+2}
制約
- 1\leq H,W \leq 1000
- H,W は整数
- S_i は
S,G,.,#からなる長さ W の文字列 - スタートマスとゴールマスはちょうど 1 つずつ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
ゴールマスへ行くことが可能ならば移動回数の最小値を、不可能ならば -1 を出力せよ。
入力例 1
3 5 .S#.G ..... .#...
出力例 1
7

左図のように (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5) と移動することで、7 回の移動でゴールマスへ行くことができます。 6 回以下の移動でゴールマスへ行くことはできないので、答えは 7 です。
右図のように横移動を連続で行う経路(あるいは縦移動を連続で行う経路)はとれないことに注意してください。
入力例 2
3 5 ..#.G ..... S#...
出力例 2
-1
ゴールマスへ行くことはできません。
入力例 3
8 63 ............................................................... ..S...#............................#####..#####..#####..####G.. ..#...#................................#..#...#......#..#...... ..#####..####...####..####..#..#...#####..#...#..#####..#####.. ..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#.. ..#...#..#####..####..####..####...#####..#####..#####..#####.. ................#.....#........#............................... ................#.....#........#...............................
出力例 3
148
Score : 400 points
Problem Statement
You are given a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell is one of the following: a start cell, a goal cell, an empty cell, or an obstacle cell. This information is described by H strings S_1,S_2,\dots,S_H, each of length W. Specifically, the cell (i,j) is a start cell if the j-th character of S_i is S, a goal cell if it is G, an empty cell if it is ., and an obstacle cell if it is #. It is guaranteed that there is exactly one start cell and exactly one goal cell.
You are currently on the start cell. Your objective is to reach the goal cell by repeatedly moving to a cell adjacent by an edge to the one you are in. However, you cannot move onto an obstacle cell or move outside the grid, and you must alternate between moving vertically and moving horizontally each time. (The direction of the first move can be chosen arbitrarily.)
Determine if it is possible to reach the goal cell. If it is, find the minimum number of moves required.
More formally, check if there exists a sequence of cells (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) satisfying all of the following conditions. If such a sequence exists, find the minimum value of k-1.
- For every 1 \leq l \leq k, it holds that 1 \leq i_l \leq H and 1 \leq j_l \leq W, and (i_l,j_l) is not an obstacle cell.
- (i_1,j_1) is the start cell.
- (i_k,j_k) is the goal cell.
- For every 1 \leq l \leq k-1, |i_l - i_{l+1}| + |j_l - j_{l+1}| = 1.
- For every 1 \leq l \leq k-2, if i_l \neq i_{l+1}, then i_{l+1} = i_{l+2}.
- For every 1 \leq l \leq k-2, if j_l \neq j_{l+1}, then j_{l+1} = j_{l+2}.
Constraints
- 1 \leq H,W \leq 1000
- H and W are integers.
- Each S_i is a string of length W consisting of
S,G,.,#. - There is exactly one start cell and exactly one goal cell.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
If it is possible to reach the goal cell, print the minimum number of moves. Otherwise, print -1.
Sample Input 1
3 5 .S#.G ..... .#...
Sample Output 1
7

As shown in the left figure, you can move as (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5), reaching the goal cell in 7 moves. It is impossible in 6 moves or fewer, so the answer is 7.
Note that you cannot take a path that moves horizontally (or vertically) consecutively without alternating as shown in the right figure.
Sample Input 2
3 5 ..#.G ..... S#...
Sample Output 2
-1
It is not possible to reach the goal cell.
Sample Input 3
8 63 ............................................................... ..S...#............................#####..#####..#####..####G.. ..#...#................................#..#...#......#..#...... ..#####..####...####..####..#..#...#####..#...#..#####..#####.. ..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#.. ..#...#..#####..####..####..####...#####..#####..#####..#####.. ................#.....#........#............................... ................#.....#........#...............................
Sample Output 3
148
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
高橋君は整数 x を持っています。最初 x=0 です。
高橋君は以下の操作を好きな回数行えます。
- 整数 i\ (1\leq i \leq 9) を選ぶ。 C_i 円払い、x を 10x + i で置き換える。
高橋君の予算は N 円です。操作で支払うお金の総和が予算を超過しないように操作を行うとき、最終的に得られる x の最大値を求めてください。
制約
- 1 \leq N \leq 10^6
- 1 \leq C_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N C_1 C_2 \ldots C_9
出力
答えを出力せよ。
入力例 1
5 5 4 3 3 2 5 3 5 3
出力例 1
95
例えば i = 9 とする操作、i=5 とする操作を順に行うことで、x は以下のように変化します。
0 \rightarrow 9 \rightarrow 95
操作により支払うお金の合計は C_9 + C_5 = 3 + 2 = 5 円であり、これは予算を超過しません。 予算を超過しないような操作の方法によって 96 以上の整数を作ることが不可能であることが証明できるので、答えは 95 です。
入力例 2
20 1 1 1 1 1 1 1 1 1
出力例 2
99999999999999999999
答えが 64 bit整数型に収まらないこともあることに注意してください。
Score : 500 points
Problem Statement
Takahashi has an integer x. Initially, x=0.
Takahashi may do the following operation any number of times.
- Choose an integer i\ (1\leq i \leq 9). Pay C_i yen (the currency in Japan) to replace x with 10x + i.
Takahashi has a budget of N yen. Find the maximum possible value of the final x resulting from operations without exceeding the budget.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq C_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N C_1 C_2 \ldots C_9
Output
Print the answer.
Sample Input 1
5 5 4 3 3 2 5 3 5 3
Sample Output 1
95
For example, the operations where i = 9 and i=5 in this order change x as:
0 \rightarrow 9 \rightarrow 95.
The amount of money required for these operations is C_9 + C_5 = 3 + 2 = 5 yen, which does not exceed the budget. Since we can prove that we cannot make an integer greater than or equal to 96 without exceeding the budget, the answer is 95.
Sample Input 2
20 1 1 1 1 1 1 1 1 1
Sample Output 2
99999999999999999999
Note that the answer may not fit into a 64-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
この問題は C 問題の強化版です。分割する個数が 3 個になります。
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A を 2 か所で区切って 3 個の空でない連続する部分列に分割するとき、数列に含まれる数の種類数の和としてありえる最大値を求めてください。
より厳密には、1 \leq i < j \leq N-1 を満たす整数の組 (i,j) について (A_1,A_2,\ldots,A_i) , (A_{i+1},A_{i+2},\ldots,A_j) , (A_{j+1},A_{j+2},\ldots,A_{N}) のそれぞれに含まれる整数の種類数の和としてありえる最大値を求めてください。
制約
- 3 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 3 1 4 1 5
出力例 1
5
(i,j)=(2,4) とし、(3,1) と (4,1) と (5) の 3 つの連続する部分列に分割すると、それぞれに含まれる整数の種類数は 2,2,1 でこれらの和は 5 です。また、5 より大きい値は取り得ないので、答えは 5 です。他にも、(i,j)=(1,3),(2,3),(3,4) などでも種類数の和は 5 になります。
入力例 2
10 2 5 6 4 4 1 1 3 1 4
出力例 2
9
Score : 550 points
Problem Statement
This problem is a harder version of Problem C. Here, the sequence is split into three subarrays.
You are given an integer sequence of length N: A = (A_1, A_2, \ldots, A_N).
When splitting A at two positions into three non-empty (contiguous) subarrays, find the maximum possible sum of the counts of distinct integers in those subarrays.
More formally, find the maximum sum of the following three values for a pair of integers (i,j) such that 1 \leq i < j \leq N-1: the count of distinct integers in (A_1, A_2, \ldots, A_i), the count of distinct integers in (A_{i+1},A_{i+2},\ldots,A_j), and the count of distinct integers in (A_{j+1},A_{j+2},\ldots,A_{N}).
Constraints
- 3 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 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 answer.
Sample Input 1
5 3 1 4 1 5
Sample Output 1
5
If we let (i,j) = (2,4) to split the sequence into three subarrays (3,1), (4,1), (5), the counts of distinct integers in those subarrays are 2, 2, 1, respectively, for a total of 5. This sum cannot be greater than 5, so the answer is 5. Other partitions, such as (i,j) = (1,3), (2,3), (3,4), also achieve this sum.
Sample Input 2
10 2 5 6 4 4 1 1 3 1 4
Sample Output 2
9