Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。
高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。
- \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
- \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。
この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。
制約
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A B P Q R S
出力
Q-P+1 行出力せよ。
各行は # と . のみからなる長さ S-R+1 の文字列であり、
i 行目の文字列の j 番目の文字が
# であることは (P+i-1,R+j-1) が黒く塗られていることを、
. であることは (P+i-1,R+j-1) が白く塗られていることをさす。
入力例 1
5 3 2 1 5 1 5
出力例 1
...#. #.#.. .#... #.#.. ...#.
1 つめの操作で (2,1), (3,2), (4,3), (5,4) の 4 マスが、
2 つめの操作で (4,1), (3,2), (2,3), (1,4) の 4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。
入力例 2
5 3 3 4 5 2 5
出力例 2
#.#. ...#
操作によって、
(1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5) の 9 マスが
黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。
入力例 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
出力例 3
#.# .#. #.#
入力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.
Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.
- For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
- For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.
In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.
Constraints
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B P Q R S
Output
Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and ..
The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.
Sample Input 1
5 3 2 1 5 1 5
Sample Output 1
...#. #.#.. .#... #.#.. ...#.
The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.
Sample Input 2
5 3 3 4 5 2 5
Sample Output 2
#.#. ...#
The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.
Sample Input 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
Sample Output 3
#.# .#. #.#
Note that the input may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1,2,\dots,N が 1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。
- 全ての i (1 \leq i \leq N) に対して Q の p_i 番目の要素が i である。
ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。
制約
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) は長さ N の順列である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N p_1 p_2 \dots p_N
出力
数列 Q を空白区切りで 1 行で出力せよ。
q_1 q_2 \dots q_N
入力例 1
3 2 3 1
出力例 1
3 1 2
以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。
- i = 1 のとき p_i = 2, q_2 = 1
- i = 2 のとき p_i = 3, q_3 = 2
- i = 3 のとき p_i = 1, q_1 = 3
入力例 2
3 1 2 3
出力例 2
1 2 3
全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。
入力例 3
5 5 3 2 4 1
出力例 3
5 3 2 4 1
Score : 300 points
Problem Statement
We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.
- For every i (1 \leq i \leq N), the p_i-th element of Q is i.
It can be proved that there exists a unique Q that satisfies the condition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N p_1 p_2 \dots p_N
Output
Print the sequence Q in one line, with spaces in between.
q_1 q_2 \dots q_N
Sample Input 1
3 2 3 1
Sample Output 1
3 1 2
The permutation Q=(3,1,2) satisfies the condition, as follows.
- For i = 1, we have p_i = 2, q_2 = 1.
- For i = 2, we have p_i = 3, q_3 = 2.
- For i = 3, we have p_i = 1, q_1 = 3.
Sample Input 2
3 1 2 3
Sample Output 2
1 2 3
If p_i = i for every i (1 \leq i \leq N), we will have P = Q.
Sample Input 3
5 5 3 2 4 1
Sample Output 3
5 3 2 4 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0, 1, ? からなる文字列 S および整数 N が与えられます。
S に含まれる ? をそれぞれ 0 または 1 に置き換えて 2 進整数とみなしたときに得られる値の集合を T とします。
たとえば、S= ?0? のとき、
T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace です。
T に含まれる N 以下の値のうち最大のものを (10 進整数として) 出力してください。
N 以下の値が T に含まれない場合は、代わりに -1 を出力してください。
制約
- S は
0,1,?からなる文字列 - S の長さは 1 以上 60 以下
- 1\leq N \leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
S N
出力
答えを出力せよ。
入力例 1
?0? 2
出力例 1
1
問題文中で例示したとおり、T=\lbrace 0,1,4,5\rbrace です。 T に含まれる N 以下の値は 0 と 1 なので、そのうちの最大である 1 を出力します。
入力例 2
101 4
出力例 2
-1
T=\lbrace 5\rbrace であるため、N 以下の値は T に含まれません。
入力例 3
?0? 1000000000000000000
出力例 3
5
Score : 400 points
Problem Statement
You are given an integer N and a string S consisting of 0, 1, and ?.
Let T be the set of values that can be obtained by replacing each ? in S with 0 or 1 and interpreting the result as a binary integer.
For instance, if S= ?0?, we have T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace.
Print (as a decimal integer) the greatest value in T less than or equal to N.
If T does not contain a value less than or equal to N, print -1 instead.
Constraints
- S is a string consisting of
0,1, and?. - The length of S is between 1 and 60, inclusive.
- 1\leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
S N
Output
Print the answer.
Sample Input 1
?0? 2
Sample Output 1
1
As shown in the problem statement, T=\lbrace 0,1,4,5\rbrace. Among them, 0 and 1 are less than or equal to N, so you should print the greatest of them, 1.
Sample Input 2
101 4
Sample Output 2
-1
We have T=\lbrace 5\rbrace, which does not contain a value less than or equal to N.
Sample Input 3
?0? 1000000000000000000
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 X, Y に対し、2 次元平面上において以下の条件を満たす長方形を良い長方形と呼びます。
- 全ての辺は x 軸または y 軸に並行である。
- 全ての頂点に対し、その x 座標は 0 以上 X 以下の整数であり、その y 座標は 0 以上 Y 以下の整数である。
面積がそれぞれ A 以上、B 以上、C 以上であるような 3 つの良い長方形を重ならないように配置することができるか判定してください。
ただし、3 つの長方形が重ならないとは、どの 2 つの長方形についても、それらの共通部分の面積が 0 であることを指します。
制約
- 1 \leq X, Y \leq 10^9
- 1 \leq A, B, C \leq 10^{18}
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
X Y A B C
出力
問題文で与えられた条件を満たすように長方形を配置することができるならば Yes、できないならば No と出力せよ。
入力例 1
3 3 2 2 3
出力例 1
Yes
下の図のように配置すればよいです。長方形内の数値は面積を表します。
2 \geq A, 3 \geq B, 3 \geq C であるので、問題文で与えられた条件を満たします。

入力例 2
3 3 4 4 1
出力例 2
No
条件を満たすように配置することはできません。
入力例 3
1000000000 1000000000 1000000000000000000 1000000000000000000 1000000000000000000
出力例 3
No
Score : 500 points
Problem Statement
For positive integers X and Y, a rectangle in a two-dimensional plane that satisfies the conditions below is said to be good.
- Every edge is parallel to the x- or y-axis.
- For every vertex, its x-coordinate is an integer between 0 and X (inclusive), and y-coordinate is an integer between 0 and Y (inclusive).
Determine whether it is possible to place the following three good rectangles without overlapping: a good rectangle of an area at least A, another of an area at least B, and another of an area at least C.
Here, three rectangles are considered to be non-overlapping when the intersection of any two of them has an area of 0.
Constraints
- 1 \leq X, Y \leq 10^9
- 1 \leq A, B, C \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
X Y A B C
Output
If it is possible to place three rectangles under the conditions specified in the Problem Statement, print Yes; otherwise, print No.
Sample Input 1
3 3 2 2 3
Sample Output 1
Yes
The figure below shows a possible placement, where the number in a rectangle represents its area.
We can see that 2 \geq A, 3 \geq B, 3 \geq C, satisfying the conditions.

Sample Input 2
3 3 4 4 1
Sample Output 2
No
There is no possible placement under the conditions.
Sample Input 3
1000000000 1000000000 1000000000000000000 1000000000000000000 1000000000000000000
Sample Output 3
No
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 種類の品物があり、 i 種類目の品物の重みは w_i、価値は v_i です。どの種類の品物も 10^{10} 個ずつあります。
高橋君はこれから、品物をいくつか選んで、容量 W のバッグに入れます。高橋君は、選ぶ品物の価値を大きくしつつ、同じ種類の品物ばかりにならないようにしたいです。そこで高橋君は、i 種類目の品物を k_i 個選んだときの うれしさ を k_i v_i - k_i^2 と定義したとき、選んだ品物の重さの総和を W 以下にしつつ、各種類のうれしさの総和が最大になるように品物を選びます。高橋君が達成できる、うれしさの総和の最大値を求めてください。
制約
- 1 \leq N \leq 3000
- 1 \leq W \leq 3000
- 1 \leq w_i \leq W
- 1 \leq v_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
出力
答えを出力せよ。
入力例 1
2 10 3 4 3 2
出力例 1
5
1 種類目の品物を 2 個、2 種類目の品物を 1 個選ぶと、うれしさの総和を 5 にすることができ、これが最適です。 1 種類目の品物についてのうれしさは 2 \times 4 - 2^2 = 4、2 種類目の品物についてのうれしさは 1 \times 2 - 1^2 = 1 です。 また、重さの総和は 9 であり、容量 10 のバッグに入ります。
入力例 2
3 6 1 4 2 3 2 7
出力例 2
14
入力例 3
1 10 1 7
出力例 3
12
Score : 550 points
Problem Statement
There are N types of items. The i-th type of item has a weight of w_i and a value of v_i. Each type has 10^{10} items available.
Takahashi is going to choose some items and put them into a bag with capacity W. He wants to maximize the value of the selected items while avoiding choosing too many items of the same type. Hence, he defines the happiness of choosing k_i items of type i as k_i v_i - k_i^2. He wants to choose items to maximize the total happiness over all types while keeping the total weight at most W. Calculate the maximum total happiness he can achieve.
Constraints
- 1 \leq N \leq 3000
- 1 \leq W \leq 3000
- 1 \leq w_i \leq W
- 1 \leq v_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
Output
Print the answer.
Sample Input 1
2 10 3 4 3 2
Sample Output 1
5
By choosing 2 items of type 1 and 1 item of type 2, the total happiness can be 5, which is optimal.
Here, the happiness for type 1 is 2 \times 4 - 2^2 = 4, and the happiness for type 2 is 1 \times 2 - 1^2 = 1.
The total weight is 9, which is within the capacity 10.
Sample Input 2
3 6 1 4 2 3 2 7
Sample Output 2
14
Sample Input 3
1 10 1 7
Sample Output 3
12