実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
0 以上 255 以下の整数 A,B が与えられます。 A \text{ xor }C=B となる 0 以上の整数 C を求めてください。
なお、そのような C はただ 1 つ存在し、0 以上 255 以下であることが証明されます。
\text{ xor } とは
整数 a, b のビットごとの排他的論理和 a \text{ xor } b は、以下のように定義されます。
- a \text{ xor } b を二進表記した際の 2^k (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
制約
- 0\leq A,B \leq 255
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを出力せよ。
入力例 1
3 6
出力例 1
5
3 は 二進表記で 11、5 は二進表記で 101 なので、これらの \text{xor} は二進表記で 110 であり、十進表記で 6 です。
このように、3 \text{ xor } 5 = 6 となるので、答えは 5 です。
入力例 2
10 12
出力例 2
6
Score : 100 points
Problem Statement
You are given integers A and B between 0 and 255 (inclusive). Find a non-negative integer C such that A \text{ xor }C=B.
It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).
What is bitwise \mathrm{XOR}?
The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:
- When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
Constraints
- 0\leq A,B \leq 255
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B
Output
Print the answer.
Sample Input 1
3 6
Sample Output 1
5
When written in binary, 3 will be 11, and 5 will be 101. Thus, their \text{xor} will be 110 in binary, or 6 in decimal.
In short, 3 \text{ xor } 5 = 6, so the answer is 5.
Sample Input 2
10 12
Sample Output 2
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
相異なる二つの文字列 S, T が与えられます。
S が T よりも辞書順で小さい場合は Yes
を、大きい場合は No
を出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
なお、主要なプログラミング言語の多くでは、文字列の辞書順による比較は標準ライブラリに含まれる関数や演算子として実装されています。詳しくは各言語のリファレンスをご参照ください。
制約
- S, T は英小文字からなる長さ 1 以上 10 以下の相異なる文字列である。
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S が T より辞書順で小さい場合は Yes
を、大きい場合は No
を出力せよ。
入力例 1
abc atcoder
出力例 1
Yes
abc
と atcoder
は 1 文字目が同じで 2 文字目が異なります。 アルファベットの b
は t
よりもアルファベット順で先に来るので、 abc
の方が atcoder
よりも辞書順で小さいことがわかります。
入力例 2
arc agc
出力例 2
No
入力例 3
a aa
出力例 3
Yes
Score : 100 points
Problem Statement
You are given two different strings S and T.
If S is lexicographically smaller than T, print Yes
; otherwise, print No
.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.
Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.
- Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
- If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.
Note that many major programming languages implement lexicographical comparison of strings as operators or functions in standard libraries. For more detail, see your language's reference.
Constraints
- S and T are different strings, each of which consists of lowercase English letters and has a length of between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
S T
Output
If S is lexicographically smaller than T, print Yes
; otherwise, print No
.
Sample Input 1
abc atcoder
Sample Output 1
Yes
abc
and atcoder
begin with the same character, but their second characters are different. Since b
comes earlier than t
in alphabetical order, we can see that abc
is lexicographically smaller than atcoder
.
Sample Input 2
arc agc
Sample Output 2
No
Sample Input 3
a aa
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 個の整数 A_1, A_2, \ldots, A_N が与えられます。 このうち最大でない整数の中で最大である整数を求めてください。
ただし、この問題の制約下で答えは必ず存在します。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_1, A_2, \ldots, A_N がすべて等しいということはない
- 入力値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 2 1 3 3 2
出力例 1
2
2,1,3,3,2 の中で最大である整数は 3 です。
2,1,3,3,2 の中で 3 でない整数は 2,1,2 の 3 つであり、これらの中で最大である整数は 2 です。
入力例 2
4 4 3 2 1
出力例 2
3
入力例 3
8 22 22 18 16 22 18 18 22
出力例 3
18
Score : 200 points
Problem Statement
You are given N integers A_1, A_2, \ldots, A_N. Find the largest among those integers that are not the largest.
The constraints of this problem guarantee that the answer exists.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- It is not the case that all A_1, A_2, \ldots, A_N are equal.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 2 1 3 3 2
Sample Output 1
2
The largest integer among 2,1,3,3,2 is 3.
The integers that are not 3 among 2,1,3,3,2 are 2,1,2, among which the largest is 2.
Sample Input 2
4 4 3 2 1
Sample Output 2
3
Sample Input 3
8 22 22 18 16 22 18 18 22
Sample Output 3
18
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
文字列 S が与えられます。
S の連続する部分文字列のうち、回文であるものの長さの最大値を求めてください。
ただし、S の連続する部分文字列であって回文であるものは常に存在します。
制約
- S は長さ 2 以上 100 以下の英大文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
TOYOTA
出力例 1
5
TOYOTA
の連続する部分文字列 TOYOT
は長さ 5 の回文です。
TOYOTA
の唯一の長さ 6 の連続する部分文字列 TOYOTA
は回文でないので、5 を出力します。
入力例 2
ABCDEFG
出力例 2
1
すべての長さ 1 の連続する部分文字列は回文です。
入力例 3
AAAAAAAAAA
出力例 3
10
Score : 200 points
Problem Statement
You are given a string S.
Find the maximum length of a contiguous substring of S that is a palindrome.
Note that there is always a contiguous substring of S that is a palindrome.
Constraints
- S is a string of length between 2 and 100, inclusive, consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
TOYOTA
Sample Output 1
5
TOYOT
, a contiguous substring of TOYOTA
, is a palindrome of length 5.
TOYOTA
, the only length-6 contiguous substring of TOYOTA
, is not a palindrome, so print 5.
Sample Input 2
ABCDEFG
Sample Output 2
1
Every contiguous substring of length 1 is a palindrome.
Sample Input 3
AAAAAAAAAA
Sample Output 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 N と、長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
A の空でない連続部分列であって、同じ値を複数個含むようなものが存在するか判定してください。存在するならばそのようなもののうち最も短いものの長さを求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^6 (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
問題文中の条件を満たす連続部分列が存在しないならば -1
と出力せよ。存在するならば、そのようなもののうち最も短いものの長さを出力せよ。
入力例 1
5 3 9 5 3 1
出力例 1
4
(3,9,5,3) と (3,9,5,3,1) が条件を満たします。短いのは (3,9,5,3) で、その長さは 4 です。
入力例 2
4 2 5 3 1
出力例 2
-1
条件を満たす連続部分列は存在しません。
入力例 3
10 1 1 2 3 5 8 13 21 34 55
出力例 3
2
Score : 300 points
Problem Statement
You are given a positive integer N and an integer sequence A = (A_1,A_2,\dots,A_N) of length N.
Determine whether there exists a non-empty (contiguous) subarray of A that has a repeated value, occurring multiple times in A. If such a subarray exists, find the length of the shortest such subarray.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^6 \ (1 \leq i \leq N)
- 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
If there is no (contiguous) subarray satisfying the condition in the problem statement, print -1
. Otherwise, print the length of the shortest such subarray.
Sample Input 1
5 3 9 5 3 1
Sample Output 1
4
(3,9,5,3) and (3,9,5,3,1) satisfy the condition. The shorter one is (3,9,5,3), which has length 4.
Sample Input 2
4 2 5 3 1
Sample Output 2
-1
There is no subarray that satisfies the condition.
Sample Input 3
10 1 1 2 3 5 8 13 21 34 55
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
りんご市場に N 人の売り手と M 人の買い手がいます。
i 番目の売り手は、A_i 円以上でならりんごを売ってもよいと考えています。
i 番目の買い手は、B_i 円以下でならりんごを買ってもよいと考えています。
次の条件を満たすような最小の整数 X を求めてください。
条件:りんごを X 円で売ってもよいと考える売り手の人数が、りんごを X 円で買ってもよいと考える買い手の人数以上である。
制約
- 1 \leq N,M \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 \ldots A_N B_1 \ldots B_M
出力
答えを出力せよ。
入力例 1
3 4 110 90 120 100 80 120 10000
出力例 1
110
りんごを 110 円で売ってもよいと考える売り手は 1,2 番目の 2 人であり、りんごを 110 円で買ってもよいと考える買い手は 3,4 番目の 2 人であるため、110 は条件を満たします。
110 未満の整数が条件を満たすことはないため、これが答えです。
入力例 2
5 2 100000 100000 100000 100000 100000 100 200
出力例 2
201
入力例 3
3 2 100 100 100 80 120
出力例 3
100
Score : 300 points
Problem Statement
There are N sellers and M buyers in an apple market.
The i-th seller may sell an apple for A_i yen or more (yen is the currency in Japan).
The i-th buyer may buy an apple for B_i yen or less.
Find the minimum integer X that satisfies the following condition.
Condition: The number of people who may sell an apple for X yen is greater than or equal to the number of people who may buy an apple for X yen.
Constraints
- 1 \leq N,M \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 M A_1 \ldots A_N B_1 \ldots B_M
Output
Print the answer.
Sample Input 1
3 4 110 90 120 100 80 120 10000
Sample Output 1
110
Two sellers, the 1-st and 2-nd, may sell an apple for 110 yen; two buyers, the 3-rd and 4-th, may buy an apple for 110 yen. Thus, 110 satisfies the condition.
Since an integer less than 110 does not satisfy the condition, this is the answer.
Sample Input 2
5 2 100000 100000 100000 100000 100000 100 200
Sample Output 2
201
Sample Input 3
3 2 100 100 100 80 120
Sample Output 3
100
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
AtCoder 食堂では N 種類の主菜と M 種類の副菜が提供されており、i 種類目の主菜の価格は A_i、j 種類目の副菜の価格は B_j です。 AtCoder 食堂では新たに定食のメニューを設けることを検討しています。 定食は 1 種類の主菜と 1 種類の副菜から構成され、主菜と副菜の価格の和を s としたとき、定食の価格は \min(s,P) です。 ここで、P は入力で与えられる定数です。
定食を構成する主菜と副菜の選び方は NM 通りありますが、それらすべてに対する定食の価格の総和を求めてください。
制約
- 1\leq N,M \leq 2\times 10^5
- 1\leq A_i,B_j \leq 10^8
- 1\leq P \leq 2\times 10^8
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを整数として出力せよ。 なお、本問題の制約下では、答えは 64 bit 符号付き整数型の範囲に収まることが証明できる。
入力例 1
2 2 7 3 5 6 1
出力例 1
24
- 1 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(3+6,7)=7 です。
- 1 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(3+1,7)=4 です。
- 2 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(5+6,7)=7 です。
- 2 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(5+1,7)=6 です。
よって、答えは 7+4+7+6=24 です。
入力例 2
1 3 2 1 1 1 1
出力例 2
6
入力例 3
7 12 25514963 2436426 24979445 61648772 23690081 33933447 76190629 62703497 11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857
出力例 3
2115597124
Score : 400 points
Problem Statement
AtCoder cafeteria offers N main dishes and M side dishes. The price of the i-th main dish is A_i, and that of the j-th side dish is B_j. The cafeteria is considering introducing a new set meal menu. A set meal consists of one main dish and one side dish. Let s be the sum of the prices of the main dish and the side dish, then the price of the set meal is \min(s,P). Here, P is a constant given in the input.
There are NM ways to choose a main dish and a side dish for a set meal. Find the total price of all these set meals.
Constraints
- 1\leq N,M \leq 2\times 10^5
- 1\leq A_i,B_j \leq 10^8
- 1\leq P \leq 2\times 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print the answer as an integer. Under the constraints of this problem, it can be proved that the answer fits into a 64-bit signed integer.
Sample Input 1
2 2 7 3 5 6 1
Sample Output 1
24
- If you choose the first main dish and the first side dish, the price of the set meal is \min(3+6,7)=7.
- If you choose the first main dish and the second side dish, the price of the set meal is \min(3+1,7)=4.
- If you choose the second main dish and the first side dish, the price of the set meal is \min(5+6,7)=7.
- If you choose the second main dish and the second side dish, the price of the set meal is \min(5+1,7)=6.
Thus, the answer is 7+4+7+6=24.
Sample Input 2
1 3 2 1 1 1 1
Sample Output 2
6
Sample Input 3
7 12 25514963 2436426 24979445 61648772 23690081 33933447 76190629 62703497 11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857
Sample Output 3
2115597124
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
\lbrace 1, 2, \dots, N \rbrace の部分集合であって大きさが K のものを 1 つ選び S とします。この時、以下の式の値としてあり得る最小値を求めてください。
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1 \leq T \leq 2 \times 10^5
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^6
- 全てのテストケースに対する N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
各テストケースは以下の形式で与えられる。
N K A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
入力例 1
3 3 2 3 7 6 9 2 4 5 3 6 4 1 5 9 8 6 5 1 7 10 6 61 95 61 57 69 49 46 47 14 43 39 79 48 92 90 76 30 16 30 94
出力例 1
42 60 14579
1 番目のテストケースでは、S = \lbrace 2, 3 \rbrace を選ぶと式の値が 7 \times (2 + 4) = 42 になり、これが最小です。
Score : 475 points
Problem Statement
You are given sequences of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
Let S be a subset of \lbrace1, 2, \dots, N\rbrace of size K.
Here, find the minimum possible value of the following expression:
You are given T test cases; solve each of them.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^6
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format. Here, \mathrm{case}_i denotes the i-th test case.
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
Each test case is given in the following format:
N K A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 3 2 3 7 6 9 2 4 5 3 6 4 1 5 9 8 6 5 1 7 10 6 61 95 61 57 69 49 46 47 14 43 39 79 48 92 90 76 30 16 30 94
Sample Output 1
42 60 14579
In the first test case, for S = \{2, 3\}, the value of the expression is 7 \times (2 + 4) = 42, which is the minimum.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\le L\le R\le N を満たす整数の組 (L,R) に対し f(L,R) を以下のように定義します。
- 何も書かれていない黒板に R-L+1 個の整数 A_L,A_{L+1},\ldots,A_R を順に書き込む。
- 以下の操作を黒板に書かれた整数が全て消えるまで繰り返す。
- 整数 l,r を選ぶ。ただし、 l\le r かつ黒板に l 以上 r 以下の整数が全て 1 つ以上書かれているように l,r を選ぶ必要がある。その後、黒板に書かれた l 以上 r 以下の整数を全て消す。
- 黒板に書かれた整数が全て消えるまでに必要な操作回数の最小値を f(L,R) とする。
\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R) を求めてください。
制約
- 1\le N\le 3\times 10^5
- 1\le A_i\le N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 1 3 1 4
出力例 1
16
例えば (L,R)=(1,4) の場合、以下の手順で f(L,R) を計算することができます。
- 黒板に 1,3,1,4 が書かれている。
- (l,r)=(1,1) を選び、黒板に書かれた 1 を全て消す。黒板には 3,4 が書かれた状態になる。
- (l,r)=(3,4) を選び、黒板に書かれた 3,4 を全て消す。黒板は何も書かれていない状態になる。
- 2 回未満の操作で黒板の整数を全て消すことはできないので、f(1,4)=2 である。
同様の計算で、例えば f(2,4)=2, f(1,1)=1 なども分かります。
\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R)=16 なので、 16 を出力してください。
入力例 2
5 3 1 4 2 4
出力例 2
23
入力例 3
10 5 1 10 9 2 5 6 9 1 6
出力例 3
129
Score : 525 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
For each integer pair (L,R) with 1 \le L \le R \le N, define f(L,R) as follows:
- Start with an empty blackboard. Write the R-L+1 integers A_L, A_{L+1}, \ldots, A_R on the blackboard in order.
- Repeat the following operation until all integers on the blackboard are erased:
- Choose integers l, r with l \le r such that every integer from l through r appears at least once on the blackboard. Then, erase all integers from l through r that are on the blackboard.
- Let f(L,R) be the minimum number of such operations needed to erase all the integers from the blackboard.
Find \displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R).
Constraints
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 1 3 1 4
Sample Output 1
16
For example, in the case of (L,R)=(1,4):
- The blackboard has 1,3,1,4.
- Choose (l,r)=(1,1) and erase all occurrences of 1. The blackboard now has 3,4.
- Choose (l,r)=(3,4) and erase all occurrences of 3 and 4. The blackboard becomes empty.
- It cannot be done in fewer than two operations, so f(1,4) = 2.
Similarly, you can find f(2,4)=2, f(1,1)=1, etc.
\displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R) = 16, so print 16.
Sample Input 2
5 3 1 4 2 4
Sample Output 2
23
Sample Input 3
10 5 1 10 9 2 5 6 9 1 6
Sample Output 3
129