実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。
S と T のハミング距離を求めてください。 すなわち、1 以上 N 以下の整数 i であって、S の i 文字目と T の i 文字目が異なるようなものの個数を求めてください。
制約
- 1\leq N \leq 100
- N は整数
- S,T はそれぞれ英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
答えを出力せよ。
入力例 1
6 abcarc agcahc
出力例 1
2
S と T は 2,5 文字目が異なり、それ以外が等しいです。よって答えは 2 です。
入力例 2
7 atcoder contest
出力例 2
7
入力例 3
8 chokudai chokudai
出力例 3
0
入力例 4
10 vexknuampx vzxikuamlx
出力例 4
4
Score : 100 points
Problem Statement
You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters.
Find the Hamming distance between S and T. That is, find the number of integers i such that 1 \leq i \leq N and the i-th character of S is different from the i-th character of T.
Constraints
- 1\leq N \leq 100
- N is an integer.
- Each of S and T is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print the answer.
Sample Input 1
6 abcarc agcahc
Sample Output 1
2
S and T differ in the 2nd and 5th characters, but not in other characters. Thus, the answer is 2.
Sample Input 2
7 atcoder contest
Sample Output 2
7
Sample Input 3
8 chokudai chokudai
Sample Output 3
0
Sample Input 4
10 vexknuampx vzxikuamlx
Sample Output 4
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の整数の 2 つ組 (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N) が与えられます。 各 i = 1, 2, \ldots, N について、A_i + B_i を出力してください。
制約
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には A_i+B_i を出力せよ。
入力例 1
4 3 5 2 -6 -5 0 314159265 123456789
出力例 1
8 -4 -5 437616054
- 1 行目には、A_1 + B_1 = 3 + 5 = 8 を、
- 2 行目には、A_2 + B_2 = 2 + (-6) = -4 を、
- 3 行目には、A_3 + B_3 = (-5) + 0 = -5 を、
- 4 行目には、A_4 + B_4 = 314159265 + 123456789 = 437616054 を出力します。
Score : 100 points
Problem Statement
You are given N pairs of integers: (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N). For each i = 1, 2, \ldots, N, print A_i + B_i.
Constraints
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain A_i+B_i.
Sample Input 1
4 3 5 2 -6 -5 0 314159265 123456789
Sample Output 1
8 -4 -5 437616054
- The first line should contain A_1 + B_1 = 3 + 5 = 8.
- The second line should contain A_2 + B_2 = 2 + (-6) = -4.
- The third line should contain A_3 + B_3 = (-5) + 0 = -5.
- The fourth line should contain A_4 + B_4 = 314159265 + 123456789 = 437616054.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
この問題は 1 つの入力ファイルに複数のテストケースが含まれる問題です。
はじめに整数 T が与えられます。T 個のテストケースについて次の問題を解いてください。
- N 個の正整数 A_1, A_2, ..., A_N があります。このうち奇数は何個ありますか?
制約
- 1 \leq T \leq 100
- 1 \leq N \leq 100
- 1 \leq A_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{test}_i は i 番目のテストケースを意味する。
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
各テストケースは以下の形式で与えられる。
N A_1 A_2 \dots A_N
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 3 1 2 3 2 20 23 10 6 10 4 1 5 9 8 6 5 1 1 1000000000
出力例 1
2 1 5 0
この入力は 4 個のテストケースが含まれています。
入力の 2 行目と 3 行目が 1 番目のテストケースに対応する入力で、N = 3, A_1 = 1, A_2 = 2, A_3 = 3 になります。
A_1, A_2, A_3 のうち奇数は全部で 2 個なので 1 行目に 2 を出力します。
Score : 200 points
Problem Statement
In this problem, an input file contains multiple test cases.
You are first given an integer T. Solve the following problem for T test cases.
- We have N positive integers A_1, A_2, ..., A_N. How many of them are odd?
Constraints
- 1 \leq T \leq 100
- 1 \leq N \leq 100
- 1 \leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \text{test}_i represents the i-th test case:
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
Each test case is in the following format:
N A_1 A_2 \dots A_N
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
4 3 1 2 3 2 20 23 10 6 10 4 1 5 9 8 6 5 1 1 1000000000
Sample Output 1
2 1 5 0
This input contains four test cases.
The second and third lines correspond to the first test case, where N = 3, A_1 = 1, A_2 = 2, A_3 = 3.
We have two odd numbers in A_1, A_2, and A_3, so the first line should contain 2.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は文字 C_{i,j} で表されます。C_{i,j} が . ならば (i, j) には何も置かれておらず、 # ならば箱が 1 個置かれています。
1 \leq j \leq W を満たす整数 j に対して、整数 X_j を次のように定義します。
- j 列目に置かれている箱の個数。言い換えると、C_{i,j} が
#であるような整数 i の個数。
X_1, X_2, \dots, X_W をすべて求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W は整数
- C_{i, j} は
.または#
入力
入力は以下の形式で標準入力から与えられる。
H W
C_{1,1}C_{1,2}\dots C_{1,W}
C_{2,1}C_{2,2}\dots C_{2,W}
\vdots
C_{H,1}C_{H,2}\dots C_{H,W}
出力
X_1, X_2, \dots, X_W を以下の形式に従って出力せよ。
X_1 X_2 \dots X_W
入力例 1
3 4 #..# .#.# .#.#
出力例 1
1 2 0 3
1 列目の箱が置かれているマスは (1, 1) の 1 ヵ所です。よって X_1 = 1 です。
2 列目の箱が置かれているマスは (2, 2), (3, 2) の 2 ヵ所です。よって X_2 = 2 です。
3 列目の箱が置かれているマスは存在しません。よって X_3 = 0 です。
4 列目の箱が置かれているマスは (1, 4), (2, 4), (3, 4) の 3 ヵ所です。よって X_4 = 3 です。
よって (X_1, X_2, X_3, X_4) = (1, 2, 0, 3) が答えとなります。
入力例 2
3 7 ....... ....... .......
出力例 2
0 0 0 0 0 0 0
箱が置かれているマスが存在しない場合もあります。
入力例 3
8 3 .#. ### .#. .#. .## ..# ##. .##
出力例 3
2 7 4
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
Score : 200 points
Problem Statement
There is a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
The squares are described by characters C_{i,j}. If C_{i,j} is ., (i, j) is empty; if it is #, (i, j) contains a box.
For integers j satisfying 1 \leq j \leq W, let the integer X_j defined as follows.
- X_j is the number of boxes in the j-th column. In other words, X_j is the number of integers i such that C_{i,j} is
#.
Find all of X_1, X_2, \dots, X_W.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H and W are integers.
- C_{i, j} is
.or#.
Input
The input is given from Standard Input in the following format:
H W
C_{1,1}C_{1,2}\dots C_{1,W}
C_{2,1}C_{2,2}\dots C_{2,W}
\vdots
C_{H,1}C_{H,2}\dots C_{H,W}
Output
Print X_1, X_2, \dots, X_W in the following format:
X_1 X_2 \dots X_W
Sample Input 1
3 4 #..# .#.# .#.#
Sample Output 1
1 2 0 3
In the 1-st column, one square, (1, 1), contains a box. Thus, X_1 = 1.
In the 2-nd column, two squares, (2, 2) and (3, 2), contain a box. Thus, X_2 = 2.
In the 3-rd column, no squares contain a box. Thus, X_3 = 0.
In the 4-th column, three squares, (1, 4), (2, 4), and (3, 4), contain a box. Thus, X_4 = 3.
Therefore, the answer is (X_1, X_2, X_3, X_4) = (1, 2, 0, 3).
Sample Input 2
3 7 ....... ....... .......
Sample Output 2
0 0 0 0 0 0 0
There may be no square that contains a box.
Sample Input 3
8 3 .#. ### .#. .#. .## ..# ##. .##
Sample Output 3
2 7 4
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
\displaystyle \sum_{1\leq i< j\leq N} A_iA_j の値を求めてください。
制約
- 2\leq N \leq 3\times 10^5
- 1\leq A_i \leq 10^4
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 4 2 3
出力例 1
26
\displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26 です。
入力例 2
2 9 45
出力例 2
405
入力例 3
10 7781 8803 8630 9065 8831 9182 8593 7660 7548 8617
出力例 3
3227530139
Score : 300 points
Problem Statement
You are given a length-N integer sequence A = (A_1, A_2, \dots, A_N).
Compute the value of \displaystyle \sum_{1\leq i< j\leq N} A_iA_j.
Constraints
- 2 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^4
- 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
Output
Output the answer.
Sample Input 1
3 4 2 3
Sample Output 1
26
We have \displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26.
Sample Input 2
2 9 45
Sample Output 2
405
Sample Input 3
10 7781 8803 8630 9065 8831 9182 8593 7660 7548 8617
Sample Output 3
3227530139
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\dots,S_N が与えられます。
S_1,S_2,\dots,S_N から文字列を好きな個数選ぶことを考えます。
このとき、「選んだ文字列の中でちょうど K 個の文字列に登場する英小文字」の種類数としてあり得る最大値を求めてください。
制約
- 1 \le N \le 15
- 1 \le K \le N
- N,K は整数
- S_i は英小文字からなる空でない文字列である。
- 1 \le i \le N を満たす整数 i に対し、S_i に同じ文字は 2 個以上含まれない。
- i \neq j ならば S_i \neq S_j である。
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
4 2 abi aef bc acg
出力例 1
3
S_1,S_3,S_4 を選んだ場合、a,b,c がちょうど 2 個の文字列に含まれます。
4 個以上の文字がちょうど 2 個の文字列に含まれるような選び方は存在しないため、答えは 3 です。
入力例 2
2 2 a b
出力例 2
0
同じ文字列を複数回選ぶことはできません。
入力例 3
5 2 abpqxyz az pq bc cy
出力例 3
7
Score : 300 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N consisting of lowercase English alphabets.
Consider choosing some number of strings from S_1,S_2,\dots,S_N.
Find the maximum number of distinct alphabets that satisfy the following condition: "the alphabet is contained in exactly K of the chosen strings."
Constraints
- 1 \le N \le 15
- 1 \le K \le N
- N and K are integers.
- S_i is a non-empty string consisting of lowercase English alphabets.
- For each integer i such that 1 \le i \le N, S_i does not contain two or more same alphabets.
- If i \neq j, then S_i \neq S_j.
Input
Input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
4 2 abi aef bc acg
Sample Output 1
3
When S_1,S_3, and S_4 are chosen, a,b, and c occur in exactly two of the strings.
There is no way to choose strings so that 4 or more alphabets occur in exactly 2 of the strings, so the answer is 3.
Sample Input 2
2 2 a b
Sample Output 2
0
You cannot choose the same string more than once.
Sample Input 3
5 2 abpqxyz az pq bc cy
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
1, 2, \ldots, N の番号のついた N 人の候補者から当選者を 1 人選ぶ選挙において、M 票の投票がありました。
各票ではそれぞれちょうど 1 人が投票先として選ばれており、i 票目の投票先は候補者 A_i です。
これから 1 票目から順に開票を行い、 1 票ごとにその時点で開票が終了した場合の当選者を更新して表示します。
開票された票において最も得票数が多かった候補者が当選となります。ただし、最も得票数が多かった候補者が複数いる場合は、その中で最も番号の小さい候補者が当選となります。
各 i = 1, 2, \ldots, M について、1, 2, \ldots, i 票目のみを開票した場合の当選者を求めてください。
制約
- 1 \leq N,M \leq 200000
- 1 \leq A_i \leq N
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_M
出力
M 行出力せよ。
i 行目には 1, 2, \ldots, i 票目のみを開票した場合の当選者の番号を出力せよ。
入力例 1
3 7 1 2 2 3 1 3 3
出力例 1
1 1 2 2 1 1 3
候補者 i の得票数を C_i で表すこととします。
- 1 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 0, 0) なので当選者は 1 です。
- 2 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 1, 0) なので当選者は 1 です。
- 3 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 0) なので当選者は 2 です。
- 4 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 1) なので当選者は 2 です。
- 5 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 1) なので当選者は 1 です。
- 6 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 2) なので当選者は 1 です。
- 7 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 3) なので当選者は 3 です。
入力例 2
100 5 100 90 80 70 60
出力例 2
100 90 80 70 60
入力例 3
9 8 8 8 2 2 8 8 2 2
出力例 3
8 8 8 2 8 8 8 2
Score : 350 points
Problem Statement
There is an election to choose one winner from N candidates with candidate numbers 1, 2, \ldots, N, and there have been M votes cast.
Each vote is for exactly one candidate, with the i-th vote being for candidate A_i.
The votes will be counted in order from first to last, and after each vote is counted, the current winner will be updated and displayed.
The candidate with the most votes among those counted is the winner. If there are multiple candidates with the most votes, the one with the smallest candidate number is the winner.
For each i = 1, 2, \ldots, M, determine the winner when counting only the first i votes.
Constraints
- 1 \leq N, M \leq 200000
- 1 \leq A_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_M
Output
Print M lines.
The i-th line should contain the winner's candidate number when counting only the first i votes.
Sample Input 1
3 7 1 2 2 3 1 3 3
Sample Output 1
1 1 2 2 1 1 3
Let C_i denote the number of votes for candidate i.
- After the first vote is counted, (C_1, C_2, C_3) = (1, 0, 0), so the winner is 1.
- After the second vote is counted, (C_1, C_2, C_3) = (1, 1, 0), so the winner is 1.
- After the third vote is counted, (C_1, C_2, C_3) = (1, 2, 0), so the winner is 2.
- After the fourth vote is counted, (C_1, C_2, C_3) = (1, 2, 1), so the winner is 2.
- After the fifth vote is counted, (C_1, C_2, C_3) = (2, 2, 1), so the winner is 1.
- After the sixth vote is counted, (C_1, C_2, C_3) = (2, 2, 2), so the winner is 1.
- After the seventh vote is counted, (C_1, C_2, C_3) = (2, 2, 3), so the winner is 3.
Sample Input 2
100 5 100 90 80 70 60
Sample Output 2
100 90 80 70 60
Sample Input 3
9 8 8 8 2 2 8 8 2 2
Sample Output 3
8 8 8 2 8 8 8 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
高橋くんは冒険に出ようとしています。
冒険では、N 個の出来事が起こります。 i 番目 (1\leq i\leq N) の出来事は整数の組 (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) で表され、次のような出来事です。
- t _ i=1 のとき、タイプ x _ i のポーションを 1 つ発見する。高橋くんは、発見したポーションを拾うか捨てるかのどちらかを選択する。
- t _ i=2 のとき、タイプ x _ i のモンスター 1 体と遭遇する。高橋くんがタイプ x _ i のポーションを持っている場合、それを 1 つ消費することでモンスターを撃退することができる。モンスターを撃退しなかった場合、高橋くんは敗北する。
高橋くんが敗北することなく全てのモンスターを撃退することができるか判定してください。
高橋くんが全てのモンスターを撃退することができない場合、-1 を出力してください。
高橋くんが全てのモンスターを撃退することができる場合、高橋君が冒険の途中で持っているポーションの個数の最大値を K とします。 高橋くんが敗北しないような戦略全体にわたる K の最小値を K _ {\min} とします。 K _ {\min} の値と、K _ {\min} を達成する高橋くんの行動を出力してください。
制約
- 1\leq N\leq2\times10^5
- 1\leq t _ i\leq2\ (1\leq i\leq N)
- 1\leq x _ i\leq N\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N t _ 1 x _ 1 t _ 2 x _ 2 \vdots t _ N x _ N
出力
高橋くんが全てのモンスターを撃退することができない場合、-1 を出力せよ。
高橋くんが全てのモンスターを撃退することができる場合、1 行目には K _ {\min} の値を、2 行目には t _ i=1 であるような i すべてについて昇順に、i 番目の出来事で発見したポーションを拾うなら 1 を、拾わないなら 0 を空白区切りで出力せよ。
K _ {\min} を達成し、高橋くんが敗北せず冒険を終えられるような行動が複数ある場合、どれを出力しても構わない。
入力例 1
13 1 2 1 3 1 1 1 3 1 2 2 3 1 3 1 3 2 3 1 3 2 2 2 3 2 1
出力例 1
3 1 1 1 0 0 1 0 1
出力例は、次のような行動に対応しています。
- タイプ 2,3,1 のポーションをこの順に発見する。これらのポーションをすべて拾う。
- タイプ 3,2 のポーションをこの順に発見する。これらのポーションをいずれも拾わない。
- タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
- タイプ 3 のポーションを発見する。このポーションを拾う。
- タイプ 3 のポーションを発見する。このポーションを拾わない。
- タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
- タイプ 3 のポーションを発見する。このポーションを拾う。
- タイプ 2 のモンスターと遭遇する。持っているタイプ 2 のポーションを 1 つ消費してモンスターを撃退する。
- タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
- タイプ 1 のモンスターと遭遇する。持っているタイプ 1 のポーションを 1 つ消費してモンスターを撃退する。
この行動では、K の値は 3 となります。
K\leq 2 として敗北しない方法はないので、求める K _ {\min} の値は 3 です。 K=3 を満たして高橋くんが敗北しない行動は複数ありますが、どれを出力しても構いません。
入力例 2
4 2 3 1 4 2 1 1 2
出力例 2
-1
高橋くんはかならず最初に遭遇するモンスターに敗北してしまいます。
入力例 3
30 1 25 1 2 1 10 1 18 2 18 1 11 2 11 1 21 1 6 2 2 2 10 1 11 1 24 1 11 1 3 1 2 1 18 2 25 1 8 1 10 1 11 2 18 2 10 1 10 2 2 1 24 1 10 2 10 1 25 2 6
出力例 3
4 1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
Score : 450 points
Problem Statement
Takahashi will embark on an adventure.
During the adventure, N events will occur. The i-th event (1\leq i\leq N) is represented by a pair of integers (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) and is as follows:
- If t _ i=1, he finds one potion of type x _ i. He can choose to pick it up or discard it.
- If t _ i=2, he encounters one monster of type x _ i. If he has a potion of type x _ i, he can use one to defeat the monster. If he does not defeat it, he will be defeated.
Determine whether he can defeat all the monsters without being defeated.
If he cannot defeat all the monsters, print -1.
Otherwise, let K be the maximum number of potions he has at some point during the adventure. Let K _ {\min} be the minimum value of K across all strategies where he will not be defeated. Print the value of K _ {\min} and the actions of Takahashi that achieve K _ {\min}.
Constraints
- 1\leq N\leq2\times10^5
- 1\leq t _ i\leq2\ (1\leq i\leq N)
- 1\leq x _ 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 t _ 1 x _ 1 t _ 2 x _ 2 \vdots t _ N x _ N
Output
If Takahashi cannot defeat all the monsters, print -1.
If he can, print the value of K _ {\min} in the first line, and in the second line, for each i such that t _ i=1 in ascending order, print 1 if he picks up the potion found at the i-th event, and 0 otherwise, separated by spaces.
If multiple sequences of actions achieve K _ {\min} and allow him to finish the adventure without being defeated, you may print any of them.
Sample Input 1
13 1 2 1 3 1 1 1 3 1 2 2 3 1 3 1 3 2 3 1 3 2 2 2 3 2 1
Sample Output 1
3 1 1 1 0 0 1 0 1
The sample output corresponds to the following actions:
- Find potions of types 2,3,1 in this order. Pick up all of them.
- Find potions of types 3,2 in this order. Do not pick up any of them.
- Encounter a type-3 monster. Use one type-3 potion to defeat it.
- Find a type-3 potion. Pick it up.
- Find a type-3 potion. Do not pick it up.
- Encounter a type-3 monster. Use one type-3 potion to defeat it.
- Find a type-3 potion. Pick it up.
- Encounter a type-2 monster. Use one type-2 potion to defeat it.
- Encounter a type-3 monster. Use one type-3 potion to defeat it.
- Encounter a type-1 monster. Use one type-1 potion to defeat it.
In this sequence of actions, the value of K is 3.
There is no way to avoid defeat with K\leq 2, so the sought value of K _ {\min} is 3. There are multiple sequences of actions that satisfy K=3 and allow him to avoid defeat; you may print any of them.
Sample Input 2
4 2 3 1 4 2 1 1 2
Sample Output 2
-1
He will inevitably be defeated by the first monster he encounters.
Sample Input 3
30 1 25 1 2 1 10 1 18 2 18 1 11 2 11 1 21 1 6 2 2 2 10 1 11 1 24 1 11 1 3 1 2 1 18 2 25 1 8 1 10 1 11 2 18 2 10 1 10 2 2 1 24 1 10 2 10 1 25 2 6
Sample Output 3
4 1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
H 行 W 列のグリッドがあります。上から r 行目、左から c 列目のマスをマス (r,c) と呼びます。各マスは黒か白で塗られており、k=1,2,\ldots,N に対しマス (R_k,C_k) は黒で、それ以外の HW-N マスは白で塗られています。
このグリッドの中の縦 h 行、横 w 列の長方形領域であって、含まれるマスが全て白で塗られているようなものがいくつあるか求めてください。
より厳密には、以下の条件を全て満たす整数の組 (r_0, c_0) の個数を求めてください。
- 1\le r_0 \le H-h+1
- 1\le c_0 \le W-w+1
- 0\le i< h,\ 0\le j< w を満たす全ての整数の組 (i,j) に対し、マス (r_0+i,c_0+j) は白で塗られている。
制約
- 1\le h\le H\le 10^9
- 1\le w\le W\le 10^9
- 0\le N\le 2\times 10^5
- 1\le R_k\le H
- 1\le C_k\le W
- (R_{k_1},C_{k_1}) \neq (R_{k_2},C_{k_2}) (k_1 \neq k_2)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W h w N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
答えを出力せよ。
入力例 1
3 4 2 2 3 1 3 2 4 3 1
出力例 1
2
下図の赤で囲まれた 2 つの長方形領域が条件を全て満たします。

入力例 2
4 4 3 2 2 2 2 3 4
出力例 2
0
条件を全て満たす長方形領域は存在しません。
入力例 3
449 449 3 14 0
出力例 3
194892
入力例 4
31 9 5 7 10 14 8 8 4 18 8 12 1 8 5 9 6 18 1 14 7 5 6 26 7
出力例 4
12
Score : 500 points
Problem Statement
There is a grid with H rows and W columns. The cell at the r-th row from the top and c-th column from the left is called cell (r,c). Each cell is painted black or white: cell (R_k,C_k) is black for k=1,2,\ldots,N, and the remaining HW-N cells are white.
Find the number of rectangular regions in this grid with h rows and w columns such that all cells contained in them are painted white.
More formally, find the number of pairs of integers (r_0, c_0) satisfying all of the following conditions:
- 1\le r_0 \le H-h+1
- 1\le c_0 \le W-w+1
- Cell (r_0+i,c_0+j) is painted white for all pairs of integers (i,j) satisfying 0\le i< h,\ 0\le j< w.
Constraints
- 1\le h\le H\le 10^9
- 1\le w\le W\le 10^9
- 0\le N\le 2\times 10^5
- 1\le R_k\le H
- 1\le C_k\le W
- (R_{k_1},C_{k_1}) \neq (R_{k_2},C_{k_2}) (k_1 \neq k_2)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W h w N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Output the answer.
Sample Input 1
3 4 2 2 3 1 3 2 4 3 1
Sample Output 1
2
The two rectangular regions enclosed in red in the figure below satisfy all the conditions.

Sample Input 2
4 4 3 2 2 2 2 3 4
Sample Output 2
0
No rectangular region satisfies all the conditions.
Sample Input 3
449 449 3 14 0
Sample Output 3
194892
Sample Input 4
31 9 5 7 10 14 8 8 4 18 8 12 1 8 5 9 6 18 1 14 7 5 6 26 7
Sample Output 4
12