Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0 と 1 の 2 種類の文字からなる文字列 s が与えられます。
s に含まれる 0 を 1 に、1 を 0 に置き換えた文字列を出力してください。
制約
- s の長さは 1 以上 10 以下
- s は
0と1の 2 種類の文字からなる
入力
入力は以下の形式で標準入力から与えられる。
s
出力
答えを 1 行で出力せよ。
入力例 1
01
出力例 1
10
s の 1 文字目は 1 なので、1 文字目に出力すべき文字は 0 です。
s の 2 文字目は 0 なので、2 文字目に出力すべき文字は 1 です。
入力例 2
1011
出力例 2
0100
入力例 3
100100001
出力例 3
011011110
Score : 100 points
Problem Statement
You are given a string s consisting of two kinds of characters, 0 and 1.
Print the string obtained by replacing 0 with 1 and 1 with 0 in s.
Constraints
- The length of s is between 1 and 10, inclusive.
- s consists of two kinds of characters,
0and1.
Input
The input is given from Standard Input in the following format:
s
Output
Print the answer in a single line.
Sample Input 1
01
Sample Output 1
10
The 1-st character of s is 1, so the 1-st character to print is 0.
The 2-nd character of s is 0, so the 2-nd character to print is 1.
Sample Input 2
1011
Sample Output 2
0100
Sample Input 3
100100001
Sample Output 3
011011110
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
AtCoder社のオフィスには加湿器が 1 つあります。現在は時刻 0 であり、加湿器には水が入っていません。
あなたはこの加湿器にこれから N 回水を追加します。i 回目 (1\leq i \leq N)の水の追加は時刻 T_i に行い、水を V_i リットル追加します。なお、 T_i < T_{i+1} (1 \leq i \leq N-1) を満たすことが保証されます。
ただし、加湿器には穴が空いていて、加湿器に水が入っている間は 1 単位時間につき水が 1 リットル減り続けます。
時刻 T_N に水を追加し終えたとき、加湿器に残っている水の量を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T_1 V_1 T_2 V_2 \vdots T_N V_N
出力
答えを出力せよ。
入力例 1
4 1 3 3 1 4 4 7 1
出力例 1
3
時系列で以下のように水が追加されます。
- 時刻 1 : 水を追加する前は 0 リットル入っており、3 リットル追加して加湿器には 3 リットルの水が入っています。
- 時刻 3 : 水を追加する前は 1 リットル入っており、1 リットル追加して加湿器には 2 リットルの水が入っています。
- 時刻 4 : 水を追加する前は 1 リットル入っており、4 リットル追加して加湿器には 5 リットルの水が入っています。
- 時刻 7 : 水を追加する前は 2 リットル入っており、1 リットル追加して加湿器には 3 リットルの水が入っています。
時刻 7 に水を追加し終えたとき、加湿器に残っている水の量は 3 リットルです。よって答えは 3 です。
入力例 2
3 1 8 10 11 21 5
出力例 2
5
入力例 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
出力例 3
57
Score : 150 points
Problem Statement
There is one humidifier in the AtCoder company office. The current time is 0, and the humidifier has no water inside.
You will add water to this humidifier N times. The i-th addition of water (1 \leq i \leq N) takes place at time T_i, and you add V_i liters of water. It is guaranteed that T_i < T_{i+1} for all 1 \leq i \leq N-1.
However, the humidifier has a leak, and as long as there is water inside, the amount of water decreases by 1 liter per unit time.
Find the amount of water remaining in the humidifier immediately after you finish adding water at time T_N.
Constraints
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T_1 V_1 T_2 V_2 \vdots T_N V_N
Output
Print the answer.
Sample Input 1
4 1 3 3 1 4 4 7 1
Sample Output 1
3
At each point in time, water is added as follows:
- Time 1: Before adding, the humidifier has 0 liters. After adding 3 liters, it has 3 liters.
- Time 3: Before adding, it has 1 liter. After adding 1 liter, it has 2 liters total.
- Time 4: Before adding, it has 1 liter. After adding 4 liters, it has 5 liters total.
- Time 7: Before adding, it has 2 liters. After adding 1 liter, it has 3 liters total.
After finishing the addition at time 7, the humidifier contains 3 liters. Thus, the answer is 3.
Sample Input 2
3 1 8 10 11 21 5
Sample Output 2
5
Sample Input 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
Sample Output 3
57
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
高橋君は改札機の利用履歴を集計しました。 しかし、高橋君はうっかりいくつかの入退場記録を消してしまいました。 高橋君は消してしまった記録の復元を試みようとしています。
i, o のみからなる文字列 S が与えられます。
S の任意の位置に文字を 0 文字以上挿入することで、変更後の文字列が以下の条件を満たすようにしたいです。
- 長さが偶数であり、奇数文字目が
iで偶数文字目がoである。
挿入する必要のある文字数の最小値を求めて下さい。なお、問題の制約下で、有限個の文字を適切に挿入することで、S が条件をみたすようにできることが証明できます。
制約
- S は
i,oからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ioi
出力例 1
1
3 文字目のあとに o を挿入して ioio とすることで、条件を満たすことができます。0 文字以下の挿入で条件を満たすことはできません。
入力例 2
iioo
出力例 2
2
1 文字目のあとに o を、3 文字目のあとに i を挿入することで、条件を満たすことができます。1 文字以下の挿入で条件を満たすことはできません。
入力例 3
io
出力例 3
0
S がすでに条件を満たします。
Score : 250 points
Problem Statement
Takahashi aggregated usage records from ticket gates. However, he accidentally erased some records of entering and exiting stations. He is trying to restore the erased records.
You are given a string S consisting of i and o. We want to insert zero or more characters at arbitrary positions in S so that the resulting string satisfies the following conditions:
- Its length is even, and every odd-numbered (1st, 3rd, ...) character is
iwhile every even-numbered (2nd, 4th, ...) character iso.
Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters, S can be made to satisfy the conditions.
Constraints
- S is a string of length between 1 and 100, consisting of
iando.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ioi
Sample Output 1
1
We can insert o after the 3rd character to form ioio to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.
Sample Input 2
iioo
Sample Output 2
2
We can insert o after the 1st character and i after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.
Sample Input 3
io
Sample Output 3
0
S already satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられます。
N 行 N 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。
このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。
- (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
- (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)
高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。
高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。
制約
- 1 \le N \le 10
- 1 \le A_{i,j} \le 9
- 入力はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
出力
答えを出力せよ。
入力例 1
4 1161 1119 7111 1811
出力例 1
9786
高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。
入力例 2
10 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111
出力例 2
1111111111
32bit整数型に答えが収まるとは限らないことに注意してください。
Score : 200 points
Problem Statement
You are given a positive integer N.
We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.
Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.
- (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
- (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).
Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.
In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.
Constraints
- 1 \le N \le 10
- 1 \le A_{i,j} \le 9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
Output
Print the answer.
Sample Input 1
4 1161 1119 7111 1811
Sample Output 1
9786
If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.
Sample Input 2
10 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111
Sample Output 2
1111111111
Note that the answer may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 N 行、横 N 列のマス目があり、上から i 行目、左から j 列目のマスには整数 N\times (i-1)+j が書かれています。
今から T ターンにわたって相異なる整数が宣言されます。i ターン目には A_i が宣言され、A_i が書かれたマスに印をつけます。初めてビンゴを達成するのは何ターン目か求めてください。ただし、T ターンの中でビンゴを達成しない場合は -1 を出力してください。
ここで、ビンゴを達成するとは以下のいずれかのうち少なくとも一つ満たされることを言います。
- マス目の横の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
- マス目の縦の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
- マス目の対角線の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
制約
- 2\leq N\leq 2\times 10^3
- 1\leq T\leq \min(N^2,2\times 10^5)
- 1\leq A_i\leq N^2
- i\neq j ならば A_i\neq A_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T A_1 A_2 \ldots A_T
出力
T ターンの中でビンゴを達成するならば初めてビンゴを達成するターンを、そうでないならば -1 を出力せよ。
入力例 1
3 5 5 1 8 9 7
出力例 1
4
マス目の状態は以下のように変化します。初めてビンゴを達成するのは 4 ターン目です。

入力例 2
3 5 4 2 9 7 5
出力例 2
-1
5 ターンの中でビンゴを達成できないので -1 を出力してください。
入力例 3
4 12 13 9 6 5 2 7 16 14 8 3 10 11
出力例 3
9
Score : 300 points
Problem Statement
There is an N \times N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N \times (i-1) + j.
Over T turns, integers will be announced. On Turn i, the integer A_i is announced, and the cell containing A_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1.
Here, achieving Bingo means satisfying at least one of the following conditions:
- There exists a row in which all N cells are marked.
- There exists a column in which all N cells are marked.
- There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.
Constraints
- 2 \leq N \leq 2 \times 10^3
- 1 \leq T \leq \min(N^2, 2 \times 10^5)
- 1 \leq A_i \leq N^2
- A_i \neq A_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T A_1 A_2 \ldots A_T
Output
If Bingo is achieved within T turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1.
Sample Input 1
3 5 5 1 8 9 7
Sample Output 1
4
The state of the grid changes as follows. Bingo is achieved for the first time on Turn 4.

Sample Input 2
3 5 4 2 9 7 5
Sample Output 2
-1
Bingo is not achieved within five turns, so print -1.
Sample Input 3
4 12 13 9 6 5 2 7 16 14 8 3 10 11
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
1 から N までの番号が付けられた N 個のおもちゃと、1 から N-1 までの番号が付けられた N-1 個の箱があります。 おもちゃ i\ (1\leq i\leq N) の大きさは A_i、箱 i\ (1\leq i\leq N-1) の大きさは B_i です。
すべてのおもちゃを別々の箱にしまいたいと考えている高橋君は、今から以下の操作を順に行うことにしました。
- 任意の正整数 x を選んで、大きさ x の箱を 1 つ購入する。
- N 個のおもちゃそれぞれを、(元々あった箱と新しく購入した箱を合わせた)N 個の箱のいずれかに入れる。 ただし、各おもちゃはそのおもちゃの大きさ以上の大きさを持つ箱にしか入れることはできず、また 1 つの箱に 2 つ以上のおもちゃを入れることもできない。
高橋君は、操作 1 で十分な大きさの箱を購入することで操作 2 が実行できるようにしたいですが、大きな箱ほど値段が高いため、可能な限り小さな箱を購入したいです。
高橋君が操作 2 を実行できるような x の値が存在するか判定し、存在するならばその最小値を求めてください。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N-1}
出力
高橋君が操作 2 を実行できるような x の値が存在するならばその最小値を、存在しないならば -1 を出力せよ。
入力例 1
4 5 2 3 7 6 2 8
出力例 1
3
x=3 とした場合(すなわち、操作 1 で大きさ 3 の箱を購入した場合)を考えます。
新しく購入した箱を箱 4 と呼ぶことにすると、おもちゃ 1,\dots,4 の大きさはそれぞれ 5,2,3,7、箱 1,\dots,4 の大きさはそれぞれ 6,2,8,3 となります。 よって、おもちゃ 1 を箱 1 に、おもちゃ 2 を箱 2 に、おもちゃ 3 を箱 4 に、おもちゃ 4 を箱 3 にそれぞれ入れることができます。
逆に、x\leq 2 のときは N 個のおもちゃすべてを別々の箱に入れることができません。 よって答えは 3 です。
入力例 2
4 3 7 2 5 8 1 6
出力例 2
-1
操作 1 でどのような大きさの箱を購入したとしても、箱 2 に入れられる大きさのおもちゃが存在しないため、操作 2 を実行することができません。
入力例 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
出力例 3
37
Score : 350 points
Problem Statement
There are N toys numbered from 1 to N, and N-1 boxes numbered from 1 to N-1. Toy i\ (1 \leq i \leq N) has a size of A_i, and box i\ (1 \leq i \leq N-1) has a size of B_i.
Takahashi wants to store all the toys in separate boxes, and he has decided to perform the following steps in order:
- Choose an arbitrary positive integer x and purchase one box of size x.
- Place each of the N toys into one of the N boxes (the N-1 existing boxes plus the newly purchased box). Here, each toy can only be placed in a box whose size is not less than the toy's size, and no box can contain two or more toys.
He wants to execute step 2 by purchasing a sufficiently large box in step 1, but larger boxes are more expensive, so he wants to purchase the smallest possible box.
Determine whether there exists a value of x such that he can execute step 2, and if it exists, find the minimum such x.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_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 \dots A_N
B_1 B_2 \dots B_{N-1}
Output
If there exists a value of x such that Takahashi can execute step 2, print the minimum such x. Otherwise, print -1.
Sample Input 1
4 5 2 3 7 6 2 8
Sample Output 1
3
Consider the case where x=3 (that is, he purchases a box of size 3 in step 1).
If the newly purchased box is called box 4, toys 1,\dots,4 have sizes of 5, 2, 3, and 7, respectively, and boxes 1,\dots,4 have sizes of 6, 2, 8, and 3, respectively. Thus, toy 1 can be placed in box 1, toy 2 in box 2, toy 3 in box 4, and toy 4 in box 3.
On the other hand, if x \leq 2, it is impossible to place all N toys into separate boxes. Therefore, the answer is 3.
Sample Input 2
4 3 7 2 5 8 1 6
Sample Output 2
-1
No matter what size of box is purchased in step 1, no toy can be placed in box 2, so it is impossible to execute step 2.
Sample Input 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
Sample Output 3
37
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
(1, 2, \dots, N) を並び替えて得られる数列 P であって以下の条件を満たすもののうち、辞書順で最小のものを求めてください。
- i = 1, \dots, M に対し、P において A_i は B_i よりも先に現れる。
ただし、そのような P が存在しない場合は -1 と出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
4 3 2 1 3 4 2 4
出力例 1
2 1 3 4
条件を満たす P は (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1) の 5 つです。これらのうち辞書順で最小のものは (2, 1, 3, 4) です。
入力例 2
2 3 1 2 1 2 2 1
出力例 2
-1
条件を満たす P は存在しません。
Score : 400 points
Problem Statement
Among the sequences P that are permutations of (1, 2, \dots, N) and satisfy the condition below, find the lexicographically smallest sequence.
- For each i = 1, \dots, M, A_i appears earlier than B_i in P.
If there is no such P, print -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
4 3 2 1 3 4 2 4
Sample Output 1
2 1 3 4
The following five permutations P satisfy the condition: (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1). The lexicographically smallest among them is (2, 1, 3, 4).
Sample Input 2
2 3 1 2 1 2 2 1
Sample Output 2
-1
No permutations P satisfy the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) と N 以下の正整数 K が与えられます。
i=1,2,\dots,N について次の問題を解いてください。
- A_i を含むように K 個の要素を A から選ぶ時、それらの最大公約数 (GCD) としてあり得る最大値を求めてください。
制約
- 1 \leq K \leq N \leq 1.2 \times 10^6
- 1 \leq A_i \leq 10^6
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
N 行出力せよ。j 行目には i=j の時の答えを出力せよ。
入力例 1
5 2 3 4 6 7 12
出力例 1
3 4 6 1 6
i=1 の時は A_1 と A_3 を選ぶと最大公約数が \gcd(\lbrace 3, 6 \rbrace) = 3 となり、これが最大です。
i=2 の時は A_2 と A_5 を選ぶと最大公約数が \gcd(\lbrace 4, 12 \rbrace) = 4 となり、これが最大です。
i=3 の時は A_3 と A_5 を選ぶと最大公約数が \gcd(\lbrace 6, 12 \rbrace) = 6 となり、これが最大です。
i=4 の時は A_4 と A_2 を選ぶと最大公約数が \gcd(\lbrace 7, 4 \rbrace) = 1 となり、これが最大です。
i=5 の時は A_5 と A_3 を選ぶと最大公約数が \gcd(\lbrace 12, 6 \rbrace) = 6 となり、これが最大です。
入力例 2
3 3 6 10 15
出力例 2
1 1 1
入力例 3
10 3 414003 854320 485570 52740 833292 625990 909680 885153 435420 221663
出力例 3
59 590 590 879 879 590 20 879 590 59
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and a positive integer K (at most N).
For each i = 1, 2, \dots, N, solve the following problem:
- When you choose K elements from A that include A_i, find the maximum possible GCD (greatest common divisor) of those chosen elements.
Constraints
- 1 \leq K \leq N \leq 1.2 \times 10^6
- 1 \leq A_i \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print N lines. The j-th line should contain the answer for i=j.
Sample Input 1
5 2 3 4 6 7 12
Sample Output 1
3 4 6 1 6
For i=1, choosing A_1 and A_3 yields \gcd(\lbrace 3,6 \rbrace) = 3, which is the maximum.
For i=2, choosing A_2 and A_5 yields \gcd(\lbrace 4,12 \rbrace) = 4, which is the maximum.
For i=3, choosing A_3 and A_5 yields \gcd(\lbrace 6,12 \rbrace) = 6, which is the maximum.
For i=4, choosing A_4 and A_2 yields \gcd(\lbrace 7,4 \rbrace) = 1, which is the maximum.
For i=5, choosing A_5 and A_3 yields \gcd(\lbrace 12,6 \rbrace) = 6, which is the maximum.
Sample Input 2
3 3 6 10 15
Sample Output 2
1 1 1
Sample Input 3
10 3 414003 854320 485570 52740 833292 625990 909680 885153 435420 221663
Sample Output 3
59 590 590 879 879 590 20 879 590 59
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
ABC250 は、 ABC1000 の開催を目指す高橋くんにとってちょうど 1/4 となる記念すべき回です。
そこで、高橋くんはピザを 1 枚買ってきて、そのピザのうちなるべく 1/4 に近い分量を食べて祝うことにしました。
高橋くんが買ってきたピザは凸 N 角形 (N \ge 4) の平らな形をしており、このピザを xy 平面上に置いた際、 i 番目の頂点の座標は (X_i,Y_i) でした。
高橋くんは、このピザを以下のように切って食べることにしました。
- まず、高橋くんはピザの頂点のうち隣接しない 2 頂点を選び、それらを通る直線に沿ってナイフを入れ、ピザを 2 つに切り分ける。
- その後、 2 つのピースのうち好きなものをどちらか 1 つ選んで食べる。
高橋くんが買ってきたピザの面積の 1/4 を a 、高橋くんが食べるピースの面積を b とした時、 8 \times |a-b| としてありえる最小値を求めてください。なお、この値は常に整数となることが示せます。
制約
- 入力は全て整数
- 4 \le N \le 10^5
- |X_i|, |Y_i| \le 4 \times 10^8
- 入力される頂点は反時計回りに凸 N 角形をなす。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
5 3 0 2 3 -1 3 -3 1 -1 -1
出力例 1
1
3 番目の頂点と 5 番目の頂点を通る直線に沿ってピザを切り分け、 4 番目の頂点を含む側のピースを食べたとします。
このとき、a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8} 、 b=4 、 8 \times |a-b|=1 であり、これがありえる最小値です。
入力例 2
4 400000000 400000000 -400000000 400000000 -400000000 -400000000 400000000 -400000000
出力例 2
1280000000000000000
入力例 3
6 -816 222 -801 -757 -165 -411 733 131 835 711 -374 979
出力例 3
157889
Score : 500 points
Problem Statement
ABC 250 is a commemorable quarter milestone for Takahashi, who aims to hold ABC 1000, so he is going to celebrate this contest by eating as close to 1/4 of a pizza he bought as possible.
The pizza that Takahashi bought has a planar shape of convex N-gon. When the pizza is placed on an xy-plane, the i-th vertex has coordinates (X_i, Y_i).
Takahashi has decided to cut and eat the pizza as follows.
- First, Takahashi chooses two non-adjacent vertices from the vertices of the pizza and makes a cut with a knife along the line passing through those two points, dividing the pizza into two pieces.
- Then, he chooses one of the pieces at his choice and eats it.
Let a be the quarter (=1/4) of the area of the pizza that Takahashi bought, and b be the area of the piece of pizza that Takahashi eats. Find the minimum possible value of 8 \times |a-b|. We can prove that this value is always an integer.
Constraints
- All values in input are integers.
- 4 \le N \le 10^5
- |X_i|, |Y_i| \le 4 \times 10^8
- The given points are the vertices of a convex N-gon in the counterclockwise order.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
5 3 0 2 3 -1 3 -3 1 -1 -1
Sample Output 1
1
Suppose that he makes a cut along the line passing through the 3-rd and the 5-th vertex and eats the piece containing the 4-th vertex.
Then, a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8}, b=4, and 8 \times |a-b|=1, which is minimum possible.
Sample Input 2
4 400000000 400000000 -400000000 400000000 -400000000 -400000000 400000000 -400000000
Sample Output 2
1280000000000000000
Sample Input 3
6 -816 222 -801 -757 -165 -411 733 131 835 711 -374 979
Sample Output 3
157889