Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。
長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。
-
S において左から x,y\ (x < y) 文字目が
Bであるとする。このとき x と y の偶奇が異なる。 -
Kは 2 つのRの間にある。より形式的には、S において左から x,y\ (x < y) 文字目がRであり、 z 文字目がKであるとする。このとき、 x< z < y が成り立つ。
制約
- S は 長さ 8 の文字列であり、
K,Qがちょうど 1 文字ずつ、R,B,Nがちょうど 2 文字ずつ含まれる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
RNBQKBNR
出力例 1
Yes
B は左から 3 番目、6 番目にあり、3 と 6 は偶奇が異なります。
また、K は 2 つの R の間にあります。よって条件を満たします。
入力例 2
KRRBBNNQ
出力例 2
No
K は 2 つの R の間にありません。
入力例 3
BRKRBQNN
出力例 3
No
Score : 200 points
Problem Statement
Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.
You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.
-
Suppose that the x-th and y-th (x < y) characters from the left of S are
B; then, x and y have different parities. -
Kis between twoR's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S areRand the z-th isK; then x< z < y.
Constraints
- S is a string of length 8 that contains exactly one
KandQ, and exactly twoR's,B's , andN's.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S satisfies the conditions; print No otherwise.
Sample Input 1
RNBQKBNR
Sample Output 1
Yes
The 3-rd and 6-th characters are B, and 3 and 6 have different parities.
Also, K is between the two R's. Thus, the conditions are fulfilled.
Sample Input 2
KRRBBNNQ
Sample Output 2
No
K is not between the two R's.
Sample Input 3
BRKRBQNN
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
キーエンスには世界各地に N 個の拠点があり、1 から N までの番号が付けられています。 拠点 i には W_i 人の社員が所属しており、世界標準時で 0 時のとき拠点 i は X_i 時です。
あなたはキーエンス全社で 1 時間の会議を開きたいです。 各社員は、会議の開催時間帯が所属する拠点における 9:00-18:00 の時間帯に完全に含まれる場合にのみ会議に参加できます。 なるべく多くの社員が参加できるように会議の開催時間帯を決めるとき、会議に参加できる社員の数の最大値を求めてください。
制約
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N W_1 X_1 W_2 X_2 \vdots W_N X_N
出力
会議に参加できる社員の数の最大値を出力せよ。
入力例 1
3 5 0 3 3 2 18
出力例 1
8
世界標準時で 14:00-15:00 の時間帯に会議を開催することを考えます。
- 拠点 1 における 14:00-15:00 の時間帯に会議が開催されるため、拠点 1 に所属する 5 人の社員は会議に参加できます。
- 拠点 2 における 17:00-18:00 の時間帯に会議が開催されるため、拠点 2 に所属する 3 人の社員は会議に参加できます。
- 拠点 3 における 8:00-9:00 の時間帯に会議が開催されるため、拠点 3 に所属する 2 人の社員は会議に参加できません。
よって、合計で 5+3=8 人の社員が会議に参加できます。 これより多くの社員が参加できるような会議の開催時間帯はありません。
入力例 2
2 1 10 1000000 20
出力例 2
1000000
入力例 3
6 31 3 20 8 11 5 4 3 47 14 1 18
出力例 3
67
Score : 250 points
Problem Statement
Keyence has N bases worldwide, numbered 1 to N. Base i has W_i employees, and at 0 o'clock in Coordinated Universal Time (UTC), it is X_i o'clock at base i.
You want to hold a one-hour meeting across the entire company. Each employee can only participate in the meeting if the meeting time is completely within the 9:00-18:00 time slot at their base. Find the maximum number of employees who can participate when deciding the meeting time to allow as many employees as possible to participate.
Constraints
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W_1 X_1 W_2 X_2 \vdots W_N X_N
Output
Print the maximum number of employees who can participate in the meeting.
Sample Input 1
3 5 0 3 3 2 18
Sample Output 1
8
Consider holding the meeting from 14:00 to 15:00 in UTC.
- The meeting is held from 14:00 to 15:00 at base 1, so the 5 employees at base 1 can participate in the meeting.
- The meeting is held from 17:00 to 18:00 at base 2, so the 3 employees at base 2 can participate in the meeting.
- The meeting is held from 8:00 to 9:00 at base 3, so the 2 employees at base 3 cannot participate in the meeting.
Thus, a total of 5+3=8 employees can participate in the meeting. No meeting time allows more employees to participate.
Sample Input 2
2 1 10 1000000 20
Sample Output 2
1000000
Sample Input 3
6 31 3 20 8 11 5 4 3 47 14 1 18
Sample Output 3
67
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。
長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。
- C を A と B を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
- C を昇順にソートする。
A _ 1,A _ 2,\ldots,A _ N と B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。
制約
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
出力
答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
入力例 1
4 3 3 14 15 92 6 53 58
出力例 1
1 3 4 7 2 5 6
C は (3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。
入力例 2
4 4 1 2 3 4 100 200 300 400
出力例 2
1 2 3 4 5 6 7 8
入力例 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
出力例 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Score : 300 points
Problem Statement
You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.
- Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
- Sort C in ascending order.
For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.
Constraints
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
Output
Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.
Sample Input 1
4 3 3 14 15 92 6 53 58
Sample Output 1
1 3 4 7 2 5 6
C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).
Sample Input 2
4 4 1 2 3 4 100 200 300 400
Sample Output 2
1 2 3 4 5 6 7 8
Sample Input 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
Sample Output 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A があり、最初 A_i = i です。 以下のクエリを全部で Q 個処理してください。
- タイプ 1 : A_p を x に変更する。
- タイプ 2 : A_p を出力する。
- タイプ 3 : 「 A の先頭の要素を末尾にする」という操作を k 回繰り返す。
- 厳密には、 A を (A_2,A_3,\dots,A_N,A_1) に置き換えることを k 回繰り返す。
制約
- 入力は全て整数
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- 全てのクエリは以下の制約を満たす
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 p x
タイプ 2 のクエリは以下の形式で与えられる。
2 p
タイプ 3 のクエリは以下の形式で与えられる。
3 k
出力
タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
入力例 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
出力例 1
3 1 1000000
この入力には 5 個のクエリが含まれます。
- 最初、 A=(1,2,3,4,5) です。
- 1 番目のクエリはタイプ 2 で、 A_3=3 を出力します。
- 2 番目のクエリはタイプ 1 で、 A_2=1000000 に置き換えます。
- クエリの結果、 A=(1,1000000,3,4,5) となります。
- 3 番目のクエリはタイプ 3 で、「 A の先頭の要素を末尾にする」という操作を 4 回繰り返します。
- クエリの結果、 A=(5,1,1000000,3,4) となります。
- 4 番目のクエリはタイプ 2 で、 A_2=1 を出力します。
- 5 番目のクエリはタイプ 2 で、 A_3=1000000 を出力します。
入力例 2
1000000 2 1 1000000 999999 3 1000000000
出力例 2
出力が空である場合もあります。
Score : 300 points
Problem Statement
There is an integer sequence A of length N, initially A_i = i. Process a total of Q queries of the following types:
- Type 1: Change A_p to x.
- Type 2: Output A_p.
- Type 3: Repeat the operation "move the first element of A to the end" k times.
- Formally, replace A with (A_2,A_3,\dots,A_N,A_1) k times.
Constraints
- All input values are integers.
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- All queries satisfy the following constraints:
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
Input
The input is given from Standard Input in the following format:
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
Here, \rm{Query}_i represents the i-th query.
Type 1 queries are given in the following format:
1 p x
Type 2 queries are given in the following format:
2 p
Type 3 queries are given in the following format:
3 k
Output
For each Type 2 query, output the answer on a line.
Sample Input 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
Sample Output 1
3 1 1000000
This input contains 5 queries.
- Initially, A=(1,2,3,4,5).
- The 1st query is Type 2: output A_3=3.
- The 2nd query is Type 1: replace A_2 with 1000000.
- After the query, A=(1,1000000,3,4,5).
- The 3rd query is Type 3: repeat the operation "move the first element of A to the end" 4 times.
- After the query, A=(5,1,1000000,3,4).
- The 4th query is Type 2: output A_2=1.
- The 5th query is Type 2: output A_3=1000000.
Sample Input 2
1000000 2 1 1000000 999999 3 1000000000
Sample Output 2
The output may be empty.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N 頂点 M 辺の単純無向グラフ G があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。
あなたは、以下の 2 つの操作を好きな順序で好きな回数繰り返すことができます。
- G に無向辺を 1 つ追加する
- G から無向辺を 1 つ削除する
G をすべての頂点の次数が 2 である単純無向グラフにするために行う操作回数として考えられる最小値を求めてください。
単純無向グラフとは
単純無向グラフとは、自己ループと多重辺を持たない無向グラフのことを指します。
制約
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- 入力で与えられるグラフ G は単純無向グラフ
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
5 4 1 2 1 5 2 4 4 5
出力例 1
3
例えば以下のようにすることで、3 回の操作で G をすべての頂点の次数が 2 の単純無向グラフにすることができます。
- 頂点 2 と頂点 3 を結ぶ辺を追加する。
- 頂点 2 と頂点 4 を結ぶ辺を削除する。
- 頂点 3 と頂点 4 を結ぶ辺を追加する。
入力例 2
3 0
出力例 2
3
入力例 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
出力例 3
2
入力例 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
出力例 4
13
Score : 425 points
Problem Statement
There is a simple undirected graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and the i-th edge is an undirected edge connecting vertices A_i and B_i.
You can repeat the following two operations in any order and any number of times:
- Add one undirected edge to G
- Remove one undirected edge from G
Find the minimum number of operations to make G a simple undirected graph where all vertices have degree 2.
What is a simple undirected graph?
A simple undirected graph refers to an undirected graph that has no self-loops and no multi-edges.
Constraints
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- The graph G given in the input is a simple undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Output the answer.
Sample Input 1
5 4 1 2 1 5 2 4 4 5
Sample Output 1
3
For example, the following three operations make G a simple undirected graph where all vertices have degree 2.
- Add an edge connecting vertices 2 and 3.
- Remove the edge connecting vertices 2 and 4.
- Add an edge connecting vertices 3 and 4.
Sample Input 2
3 0
Sample Output 2
3
Sample Input 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
Sample Output 3
2
Sample Input 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
Sample Output 4
13