Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ある提案に対し、N 人の人が賛成か反対かを表明しています。なお、N は奇数です。
i \, (i = 1, 2, \dots, N) 番目の人の意見は文字列 S_i で表され、S_i = For のとき賛成しており、S_i = Against のとき反対しています。
過半数の人がこの提案に賛成しているかどうかを判定してください。
制約
- N は 1 以上 99 以下の奇数
- 全ての i = 1, 2, \dots, N に対し、S_i =
Forまたは S_i =Against
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 人のうち過半数が提案に賛成しているならば Yes、そうでなければ No と出力せよ。
入力例 1
3 For Against For
出力例 1
Yes
提案に賛成している人数は 2 人であり、これは半数を超えているので Yes と出力します。
入力例 2
5 Against Against For Against For
出力例 2
No
提案に賛成している人数は 2 人であり、これは半数以下なので No と出力します。
入力例 3
1 For
出力例 3
Yes
Score : 100 points
Problem Statement
There are N people. Each of them agrees or disagrees with a proposal. Here, N is an odd number.
The i-th (i = 1, 2, \dots, N) person's opinion is represented by a string S_i: the person agrees if S_i = For and disagrees if S_i = Against.
Determine whether the majority agrees with the proposal.
Constraints
- N is an odd number between 1 and 99, inclusive.
- S_i =
Foror S_i =Against, for all i = 1, 2, \dots, N.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print Yes if the majority of the N people agree with the proposal; print No otherwise.
Sample Input 1
3 For Against For
Sample Output 1
Yes
The proposal is supported by two people, which is the majority, so Yes should be printed.
Sample Input 2
5 Against Against For Against For
Sample Output 2
No
The proposal is supported by two people, which is not the majority, so No should be printed.
Sample Input 3
1 For
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) と、長さ M の数列 B=(B_1,B_2,\dots,B_M) が与えられます。ここで、A,B のすべての要素は互いに相異なります。A,B のすべての要素を昇順に並べた長さ N+M の数列 C=(C_1,C_2,\dots,C_{N+M}) において、A に現れる要素が2つ連続するかどうか判定してください。
制約
- 1\leq N,M \leq 100
- 1\leq A_i,B_j \leq 200
- A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M は相異なる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
A に現れる要素が C において2つ連続するならば Yes を、そうでないなら No を出力せよ。
入力例 1
3 2 3 2 5 4 1
出力例 1
Yes
C=(1,2,3,4,5) です。A に現れる 2,3 が C で連続しているため、Yes を出力します。
入力例 2
3 2 3 1 5 4 2
出力例 2
No
C=(1,2,3,4,5) です。A に現れる要素が C で連続している箇所はないため、No を出力します。
入力例 3
1 1 1 2
出力例 3
No
Score : 200 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_N) of length N and a sequence B=(B_1,B_2,\dots,B_M) of length M. Here, all elements of A and B are pairwise distinct. Determine whether the sequence C=(C_1,C_2,\dots,C_{N+M}) formed by sorting all elements of A and B in ascending order contains two consecutive elements appearing in A.
Constraints
- 1 \leq N, M \leq 100
- 1 \leq A_i, B_j \leq 200
- A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
If C contains two consecutive elements appearing in A, print Yes; otherwise, print No.
Sample Input 1
3 2 3 2 5 4 1
Sample Output 1
Yes
C=(1,2,3,4,5). Since 2 and 3 from A occur consecutively in C, print Yes.
Sample Input 2
3 2 3 1 5 4 2
Sample Output 2
No
C=(1,2,3,4,5). Since no two elements from A occur consecutively in C, print No.
Sample Input 3
1 1 1 2
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
二次元平面上の点 (0,0) から点 (A,B) に向かって距離 1 だけ移動します。移動後の座標を求めてください。
ただし、点 X から点 Y に向かって距離 d (\le 線分 XY の長さ) だけ移動すると、線分 XY 上で点 X からの距離が d であるような点に辿りつくものとします。
なお、制約より点 (0,0) と点 (A,B) の距離は 1 以上であることが保証されます。
制約
- 入力は全て整数
- 0 \le A,B \le 1000
- (A,B) \neq (0,0)
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
移動後の点を (x,y) とするとき、 x と y をこの順に空白区切りで出力せよ。
なお、各出力について、想定解との絶対誤差または相対誤差が 10^{−6} 以下であれば正解として扱われる。
入力例 1
3 4
出力例 1
0.600000000000 0.800000000000
他にも、例えば 0.5999999999 0.8000000001 という出力も許容されます。
入力例 2
1 0
出力例 2
1.000000000000 0.000000000000
点 (A,B) に到着する場合もあります。
入力例 3
246 402
出力例 3
0.521964870245 0.852966983083
Score : 200 points
Problem Statement
From the point (0,0) in a two-dimensional plane, let us move the distance of 1 toward the point (A, B). Find our coordinates after the move.
Here, after moving the distance of d from a point X to a point Y (d \le length of the segment XY), we are at the point on the segment XY whose distance from X is d.
The Constraints guarantee that the distance between the points (0, 0) and (A, B) is at least 1.
Constraints
- All values in input are integers.
- 0 \le A,B \le 1000
- (A,B) \neq (0,0)
Input
Input is given from Standard Input in the following format:
A B
Output
Let (x, y) be our coordinates after the move. Print x and y in this order, separated by a space.
Your output is considered correct when, for each printed value, the absolute or relative error from the judge's answer is at most 10^{−6}.
Sample Input 1
3 4
Sample Output 1
0.600000000000 0.800000000000
Printing 0.5999999999 0.8000000001, for example, would also be accepted.
Sample Input 2
1 0
Sample Output 2
1.000000000000 0.000000000000
We may arrive at (A, B).
Sample Input 3
246 402
Sample Output 3
0.521964870245 0.852966983083
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ヘビの待ち行列があります。最初、列は空です。
クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 3 種類です。
- タイプ 1 :
1 lの形式で与えられる。長さ l のヘビが列の末尾に追加される。このとき追加するヘビの頭の位置は、元の列が空の場合は座標 0、そうでない場合は最後尾のヘビの頭の座標に最後尾のヘビの長さを加えた座標となる。 - タイプ 2 :
2の形式で与えられる。列の先頭にいるヘビが列から抜ける。このとき、列が空でないことは保証される。抜けたヘビの長さを m として、列に残っている全てのヘビの頭の座標が m だけ減少する。 - タイプ 3 :
3 kの形式で与えられる。列の先頭から数えて k 番目にいるヘビの頭の座標を出力せよ。このとき、列には少なくとも k 匹のヘビがいることが保証される。
制約
- 1 \leq Q \leq 3 \times 10^{5}
- タイプ 1 のクエリにおいて、1 \leq l \leq 10^{9}
- タイプ 2 のクエリにおいて、列が空でないことが保証される
- タイプ 3 のクエリにおいて、列にいるヘビの数を n として、1 \leq k \leq n
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ただし、\text{query}_i は i 個目のクエリを表し、以下のいずれかの形式である。
1 l
2
3 k
出力
タイプ 3 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 3 のクエリに対する答えを出力せよ。
入力例 1
7 1 5 1 7 3 2 1 3 1 4 2 3 3
出力例 1
5 10
- 1 個目のクエリ : 長さ 5 のヘビが列に追加される。列にヘビはいないため、追加されたヘビの頭の座標は 0 となる。
- 2 個目のクエリ : 長さ 7 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 0 で長さが 5 のため、追加されたヘビの頭の座標は 5 となる。
- 3 個目のクエリ : 前から 2 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 5 であるため、5 を出力する。
- 4 個目のクエリ : 長さ 3 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 5 で長さが 7 のため、追加されたヘビの頭の座標は 12 となる。
- 5 個目のクエリ : 長さ 4 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 12 で長さが 3 のため、追加されたヘビの頭の座標は 15 となる。
- 6 個目のクエリ : 先頭のヘビが列から抜ける。抜けたヘビの長さが 5 であるため、列にいるヘビの頭の座標は 5 だけ減少する。列に残っているヘビの頭の座標は先頭から順に 0,7,10 となる。
- 7 個目のクエリ : 前から 3 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 7, 10 であるため、10 を出力する。
入力例 2
3 1 1 2 1 3
出力例 2
タイプ 3 のクエリが 1 つもない場合もあります。
入力例 3
10 1 15 1 10 1 5 2 1 5 1 10 1 15 2 3 4 3 2
出力例 3
20 5
Score : 300 points
Problem Statement
There is a queue of snakes. Initially, the queue is empty.
You are given Q queries, which should be processed in the order they are given. There are three types of queries:
- Type 1: Given in the form
1 l. A snake of length l is added to the end of the queue. If the queue was empty before adding, the head position of the newly added snake is 0; otherwise, it is the sum of the head coordinate of the last snake in the queue and the last snake’s length. - Type 2: Given in the form
2. The snake at the front of the queue leaves the queue. It is guaranteed that the queue is not empty at this time. Let m be the length of the snake that left, then the head coordinate of every snake remaining in the queue decreases by m. - Type 3: Given in the form
3 k. Output the head coordinate of the snake that is k-th from the front of the queue. It is guaranteed that there are at least k snakes in the queue at this time.
Constraints
- 1 \leq Q \leq 3 \times 10^{5}
- For a query of type 1, 1 \leq l \leq 10^{9}
- For a query of type 2, it is guaranteed that the queue is not empty.
- For a query of type 3, let n be the number of snakes in the queue, then 1 \leq k \leq n.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query in one of the following forms:
1 l
2
3 k
Output
Let q be the number of queries of type 3. Print q lines. The i-th line should contain the answer to the i-th type 3 query.
Sample Input 1
7 1 5 1 7 3 2 1 3 1 4 2 3 3
Sample Output 1
5 10
- 1st query: A snake of length 5 is added to the queue. Since the queue was empty, the head coordinate of this snake is 0.
- 2nd query: A snake of length 7 is added to the queue. Before adding, the last snake has head coordinate 0 and length 5, so the newly added snake’s head coordinate is 5.
- 3rd query: Output the head coordinate of the snake that is 2nd from the front. Currently, the head coordinates of the snakes in order are 0, 5, so output 5.
- 4th query: A snake of length 3 is added to the queue. Before adding, the last snake has head coordinate 5 and length 7, so the new snake’s head coordinate is 12.
- 5th query: A snake of length 4 is added to the queue. Before adding, the last snake has head coordinate 12 and length 3, so the new snake’s head coordinate is 15.
- 6th query: The snake at the front leaves the queue. The length of the snake that left is 5, so the head coordinate of each remaining snake decreases by 5. The remaining snake’s head coordinate becomes 0, 7, 10.
- 7th query: Output the head coordinate of the snake that is 3rd from the front. Currently, the head coordinates of the snakes in order are 0, 7, 10, so output 10.
Sample Input 2
3 1 1 2 1 3
Sample Output 2
It is possible that there are no queries of type 3.
Sample Input 3
10 1 15 1 10 1 5 2 1 5 1 10 1 15 2 3 4 3 2
Sample Output 3
20 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の非負整数列 A が与えられます。
A から K 要素を選んで順序を保ったまま抜き出した列を B としたとき、 MEX(B) としてありえる最大値を求めてください。
但し、数列 X に対して MEX(X) は以下の条件を満たす唯一の非負整数 m として定義されます。
- 0 \le i < m を満たす全ての整数 i が X に含まれる。
- m が X に含まれない。
制約
- 入力は全て整数
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
7 3 2 0 2 3 2 1 9
出力例 1
3
この入力では A=(2,0,2,3,2,1,9) であり、ここから K=3 要素を選んで抜き出して数列 B を得ます。例えば、
- 1,2,3 要素目を抜き出した時、 MEX(B)=MEX(2,0,2)=1
- 3,4,6 要素目を抜き出した時、 MEX(B)=MEX(2,3,1)=0
- 2,6,7 要素目を抜き出した時、 MEX(B)=MEX(0,1,9)=2
- 2,3,6 要素目を抜き出した時、 MEX(B)=MEX(0,2,1)=3
のようになります。
達成可能な MEX の最大値は 3 です。
Score : 300 points
Problem Statement
You are given a length-N sequence of non-negative integers.
When B is a sequence obtained by choosing K elements from A and concatenating them without changing the order, find the maximum possible value of MEX(B).
Here, for a sequence X, we define MEX(X) as the unique non-negative integer m that satisfies the following conditions:
- Every integer i such that 0 \le i < m is contained in X.
- m is not contained in X.
Constraints
- All values in the input are integers.
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
7 3 2 0 2 3 2 1 9
Sample Output 1
3
In this input, A=(2,0,2,3,2,1,9), and you obtain the sequence B by picking K=3 elements. For example,
- If the 1-st, 2-nd, and 3-rd elements are chosen, MEX(B)=MEX(2,0,2)=1.
- If the 3-rd, 4-th, and 6-th elements are chosen, MEX(B)=MEX(2,3,1)=0.
- If the 2-nd, 6-th, and 7-th elements are chosen, MEX(B)=MEX(0,1,9)=2.
- If the 2-nd, 3-rd, and 6-th elements are chosen, MEX(B)=MEX(0,2,1)=3.
The maximum possible MEX is 3.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
同時に最大 K 人の客を入れられるレストラン店があります。この店の前には脇道があり、ここで 1 つの待ち行列を管理しています。
時刻 0 の時点で店内に客はおらず、待ち行列も空です。
今日は N 個の団体客が来る予定であり、来訪が早い順に 1 から N までの番号が付けられています。団体客 i の人数は C_i 人で、時刻 A_i に待ち行列の末尾に加わります。また、この団体客は入店してから B_i 単位時間後に退店します。
それぞれの団体客は、以下の 2 条件が同時に満たされた最も早い時刻に、待ち行列を離れて入店します。
- その団体客は、待ち行列の先頭にいる。つまりその団体客は、その時点で待ち行列にいる団体客のうち、最も早く待ち行列に加わっている。
- その団体客と、店内にいるすべての団体客 (ちょうどその時刻に入店したぶんを含み、退店したぶんを除く) の人数を合算すると、K 人以下になる。
それぞれの団体客が入店する時刻を求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 B_1 C_1 \vdots A_N B_N C_N
出力
N 行出力せよ。i 行目 (1 \leq i \leq N) には、団体客 i が入店する時刻を整数で出力せよ。
入力例 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
出力例 1
30 60 105 120
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 3 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わった直後にそのまま入店し、店内の客は 7 名になる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 105 に、団体客 2 が退店し、店内の客は 3 名になる。その直後、団体客 3 が入店し、店内の客は 8 名になる。
- 時刻 120 に、団体客 4 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 150 に、団体客 3 が退店し、店内の客は 5 名になる。
- 時刻 165 に、団体客 4 が退店し、店内の客は 3 名になる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。
入力例 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
出力例 2
30 330 330 330
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 120 に、団体客 4 が待ち行列に加わる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。その直後、団体客 2,3,4 が入店し、店内の客は 9 名になる。
- 時刻 375 に、団体客 2,3,4 が退店し、店内の客は 0 名になる。
入力例 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
出力例 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
Score : 400 points
Problem Statement
There is a restaurant that can accommodate at most K customers simultaneously. In front of this restaurant, there is a side street where one queue is managed.
At time 0, there are no customers in the restaurant, and the queue is also empty.
Today, N groups of customers are scheduled to come, and they are numbered from 1 to N in the order of their arrival. Group i consists of C_i people, joins the end of the queue at time A_i, and leaves the restaurant B_i time units after entering.
Each group enters the restaurant by leaving the queue at the earliest time when both of the following two conditions are satisfied simultaneously:
- The group is at the front of the queue. In other words, the group is the earliest to have joined among those still in the queue at that point.
- When the number of people in that group and all groups currently in the restaurant (including those entering at exactly that time and excluding those leaving) are combined, there are K or fewer people.
Find the time when each group enters the restaurant.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 B_1 C_1 \vdots A_N B_N C_N
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain the time when group i enters the restaurant as an integer.
Sample Input 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
Sample Output 1
30 60 105 120
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 3.
- At time 60, group 2 joins the queue and immediately enters, making the number of customers in the restaurant 7.
- At time 90, group 3 joins the queue.
- At time 105, group 2 leaves, making the number of customers in the restaurant 3. Immediately after, group 3 enters, making the number of customers in the restaurant 8.
- At time 120, group 4 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 150, group 3 leaves, making the number of customers in the restaurant 5.
- At time 165, group 4 leaves, making the number of customers in the restaurant 3.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0.
Sample Input 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
Sample Output 2
30 330 330 330
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 60, group 2 joins the queue.
- At time 90, group 3 joins the queue.
- At time 120, group 4 joins the queue.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0. Immediately after, groups 2,3,4 enter, making the number of customers in the restaurant 9.
- At time 375, groups 2,3,4 leave, making the number of customers in the restaurant 0.
Sample Input 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
Sample Output 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
この問題は インタラクティブな問題 (あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
1 から N の番号がついた N 頂点からなる木 G が与えられます。i 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
この木 G を使って、あなたと高橋君がゲームをします。まず、あなたは先手後手を決めます。その後、先手から順に交互に以下の操作を行います。
- 1 \leq i < j \leq N を満たす整数の組 (i,j) であって、以下の条件をともに満たすものを選び、頂点 i と頂点 j を結ぶ辺を G に追加する。
- G は頂点 i と頂点 j を結ぶ辺を持たない
- 頂点 i と頂点 j を結ぶ辺を G に追加しても、奇閉路ができない
操作を行えなくなった方が負けであり、負けでない方が勝ちです。実際に高橋君とゲームをして勝ってください。
奇閉路とは?
G の頂点の列 (v_0,v_1,\ldots,v_k) が以下の条件を全て満たすとき、かつ、そのときに限りこの列を G の奇閉路といいます。
- k は奇数である。
- v_0=v_k である。
- 全ての 1\leq i \leq k に対して、v_{i-1} と v_{i} を結ぶ辺が存在する。
制約
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- 与えられるグラフは木である
- 入力は全て整数である
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
最初に、N および G の情報を標準入力から受け取ってください。以下の形式で与えられます。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
次に、あなたが先手と後手のどちらを選ぶか決めてください。先手を選ぶ場合 First、後手を選ぶ場合 Second と標準出力に出力してください。
その後ゲームが始まります。
あなたの手番では、操作のために選んだ整数の組 (i,j) を、i,j の順に空白区切りで標準出力に出力してください。
i j
高橋君の手番では、2 つの整数 i,j が順に空白区切りで標準入力に与えられます。
i j
(i,j)=(-1,-1) のとき、あなたがゲームに勝利しゲームが終了したことを意味します。この場合、直ちにプログラムを終了してください。
それ以外のとき、(i,j) は高橋君が操作のために選んだ整数の組を表します。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- ゲームが終了したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
| 入力 | 出力 | 説明 |
|---|---|---|
41 22 33 4 | まず整数 N および G の情報が与えられます。 | |
First | あなたは先手を選びました。 | |
1 4 | あなたは (1,4) に対して操作を行いました。 | |
-1 -1 | 高橋君が操作を行えなくなったためゲームを終了します。ジャッジ結果は になります。 |
Score : 425 points
Problem Statement
This problem is an interactive problem (in which your program and the judge system communicate via input and output).
You are given a tree G with N vertices numbered 1 to N. The i-th edge connects vertices U_i and V_i.
You will play a game with Takahashi using this tree G. First, you decide who is first and who is second. Then, starting from the first player, take turns performing the following operation:
- Choose a pair of integers (i,j) with 1 \leq i < j \leq N that satisfies both of the following conditions, then add an edge connecting vertices i and j to G.
- G does not already have an edge connecting vertices i and j.
- Adding an edge connecting vertices i and j does not create an odd cycle.
A player who cannot perform this operation loses, and the other player wins. Play this game against Takahashi and win.
What is an odd cycle?
A sequence of vertices (v_0,v_1,\ldots,v_k) of G is called an odd cycle if and only if all of the following conditions are satisfied:
- k is odd.
- v_0=v_k.
- For every 1\leq i \leq k, there is an edge connecting v_{i-1} and v_{i}.
Constraints
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- The given graph is a tree.
- All input values are integers.
Interaction
This is an interactive problem (in which your program and the judge system communicate via input and output).
First, read N and the edges of G from Standard Input, given in the following format:
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Then, decide whether you go first or second. If you choose first, print First; if second, print Second.
Then, the game begins.
On your turn, output the chosen pair (i,j) by printing two integers i and j in this order, separated by a space:
i j
On Takahashi's turn, two integers i,j will be given in order, separated by a space, via Standard Input:
i j
If (i,j)=(-1,-1), it means he can no longer make a move and you win. Immediately terminate your program in this case.
Otherwise, (i,j) is the pair of integers he has chosen.
Notes
- After each output, be sure to print a newline and flush Standard Output. Otherwise, you may get TLE.
- If you produce output in an incorrect format or your program ends prematurely, the judge result is indeterminate.
- Once the game finishes, terminate your program immediately. Otherwise, the judge result is indeterminate.
Sample Interaction
| Input | Output | Explanation |
|---|---|---|
41 22 33 4 |
First, you receive N and the edges of G. | |
First |
You choose to go first. | |
1 4 |
You add an edge between vertices 1 and 4. | |
-1 -1 |
Takahashi can no longer move, so you win. The judge result will be . |
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
縦 N マス、横 N マスの N ^ 2 マスからなるマス目があります。 上から i 行目 (1\leq i\leq N) 、左から j 列目 (1\leq j\leq N) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。 マス目には合計で M 個のコマが置かれており、k 番目 (1\leq k\leq M) のコマはマス (a _ k,b _ k) に置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどれかの条件を満たすコマを取ることができます。
- i 行目に置かれている
- j 列目に置かれている
- i+j=a+b となるようなマス (a,b)\ (1\leq a\leq N,1\leq b\leq N) に置かれている
- i-j=a-b となるようなマス (a,b)\ (1\leq a\leq N,1\leq b\leq N) に置かれている
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。

あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- 1\leq N\leq10 ^ 9
- 1\leq M\leq10 ^ 3
- 1\leq a _ k\leq N,1\leq b _ k\leq N\ (1\leq k\leq M)
- (a _ k,b _ k)\neq(a _ l,b _ l)\ (1\leq k\lt l\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M a _ 1 b _ 1 a _ 2 b _ 2 \vdots a _ M b _ M
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
出力例 1
2
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。

よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスはマス (6,6) とマス (7,7) の 2 つです。
入力例 2
1000000000 1 1 1
出力例 2
999999997000000002
10 ^ {18} マスのうち、置くことができないマスは 1 行目のマス、1 列目のマス、およびマス (1,1), マス (2,2),\ldots, マス (10 ^ 9,10 ^ 9) の 3\times10 ^ 9-2 マスです。
答えが 2 ^ {32} 以上になる場合があることに注意してください。
入力例 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
出力例 3
77
Score : 550 points
Problem Statement
There is a grid of N^2 squares with N rows and N columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq N) and j-th column from the left (1\leq j\leq N).
Each square is either empty or has a piece placed on it. There are M pieces placed on the grid, and the k-th (1\leq k\leq M) piece is placed on square (a_k,b_k).
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 any of the following conditions:
- Placed in row i
- Placed in column j
- Placed on any square (a,b)\ (1\leq a\leq N,1\leq b\leq N) where i+j=a+b
- Placed on any square (a,b)\ (1\leq a\leq N,1\leq b\leq N) where i-j=a-b
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
- 1\leq N\leq10^9
- 1\leq M\leq10^3
- 1\leq a_k\leq N,1\leq b_k\leq N\ (1\leq k\leq M)
- (a_k,b_k)\neq(a_l,b_l)\ (1\leq k\lt l\leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
Sample Output 1
2
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece on only two squares: squares (6,6) and (7,7).
Sample Input 2
1000000000 1 1 1
Sample Output 2
999999997000000002
Out of 10^{18} squares, the squares that cannot be used are: squares in row 1, squares in column 1, and squares (1,1), (2,2), \ldots, (10^9,10^9), totaling 3\times10^9-2 squares.
Note that the answer may be 2^{32} or greater.
Sample Input 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
Sample Output 3
77