Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は果樹園でリンゴ拾いをしています。
果樹園には N 本のリンゴの木が一列に並んでおり、左から順に木 1, 木 2, \ldots, 木 N と番号が付けられています。木 i には D_i 個のリンゴが実っています。
高橋君は木 1 から順番に右方向へ進みながら、以下のパターンに従ってリンゴを収穫します。
- 連続する K 本の木からリンゴを収穫する(収穫する木では、その木に実っているリンゴをすべて収穫する)。
- その直後の 1 本の木は、疲れて休憩するため通り過ぎ、収穫しない。
このパターンを木 1 から始めて、木 N に到達するまで繰り返します。パターンの途中で木 N に到達した場合は、そこで終了します。高橋君が収穫する木やその順序を自由に選ぶことはできません。
高橋君が収穫したリンゴの個数の合計を求めてください。
例えば、N = 7, K = 3 のとき、高橋君は木 1, 2, 3 から収穫し、木 4 を通り過ぎ、木 5, 6, 7 から収穫します。
また、N = 8, K = 2 のとき、高橋君は木 1, 2 から収穫し、木 3 を通り過ぎ、木 4, 5 から収穫し、木 6 を通り過ぎ、木 7, 8 から収穫します。
制約
- 1 \leq N \leq 10^6
- 1 \leq K \leq N
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である。
入力
N K D_1 D_2 \ldots D_N
- 1 行目には、木の本数を表す整数 N と、連続して収穫できる木の本数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各木に実っているリンゴの個数を表す整数 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。
出力
高橋君が収穫したリンゴの個数の合計を 1 行で出力せよ。
入力例 1
7 3 5 3 2 8 4 1 6
出力例 1
21
入力例 2
8 2 3 1 4 1 5 9 2 6
出力例 2
18
入力例 3
15 4 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
出力例 3
900
入力例 4
20 3 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
出力例 4
750
入力例 5
1 1 1000000000
出力例 5
1000000000
Score : 266 pts
Problem Statement
Takahashi is picking apples in an orchard.
There are N apple trees lined up in a row in the orchard, numbered tree 1, tree 2, \ldots, tree N from left to right. Tree i has D_i apples on it.
Takahashi starts from tree 1 and proceeds to the right, harvesting apples according to the following pattern:
- Harvest apples from K consecutive trees (for each tree he harvests, he picks all the apples on that tree).
- Immediately after, he skips 1 tree without harvesting, as he is tired and takes a rest.
He repeats this pattern starting from tree 1 until he reaches tree N. If he reaches tree N in the middle of a pattern, he stops there. Takahashi cannot freely choose which trees to harvest or the order in which to harvest them.
Find the total number of apples Takahashi harvests.
For example, when N = 7, K = 3, Takahashi harvests from trees 1, 2, 3, skips tree 4, and harvests from trees 5, 6, 7.
Also, when N = 8, K = 2, Takahashi harvests from trees 1, 2, skips tree 3, harvests from trees 4, 5, skips tree 6, and harvests from trees 7, 8.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq K \leq N
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N K D_1 D_2 \ldots D_N
- The first line contains an integer N representing the number of trees and an integer K representing the number of consecutive trees that can be harvested, separated by a space.
- The second line contains integers D_1, D_2, \ldots, D_N representing the number of apples on each tree, separated by spaces.
Output
Print the total number of apples Takahashi harvests on a single line.
Sample Input 1
7 3 5 3 2 8 4 1 6
Sample Output 1
21
Sample Input 2
8 2 3 1 4 1 5 9 2 6
Sample Output 2
18
Sample Input 3
15 4 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
Sample Output 3
900
Sample Input 4
20 3 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Sample Output 4
750
Sample Input 5
1 1 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君と青木君は、それぞれ引っ越しを終えて新しい街に住み始めました。二人は自分の住んでいる場所から最寄りの郵便局を見つけて、利用登録をしたいと考えています。
この街は 1 本の道路を数直線で表すことができ、道路上には N 個の郵便局が点在しています。i 番目の郵便局は座標 X_i の位置にあり、現在 C_i 人の利用者が登録されています。なお、郵便局は座標の昇順に並んでいるとは限りません。
高橋君は座標 P に、青木君は座標 Q に住んでいます。二人はそれぞれ独立に、自分の住んでいる場所から最も近い郵便局に利用登録を行います。ここで、自分の住んでいる場所と郵便局との距離は、座標の差の絶対値で測ります。最も近い郵便局が複数ある場合は、その中で座標が最も小さいものを選びます。
二人がそれぞれ登録を行った結果、登録先の郵便局の利用登録者数はそれぞれ 1 増えます。二人が同じ郵便局に登録した場合、その郵便局の利用登録者数は 2 増えます。
二人の登録がすべて完了した後、高橋君が登録した郵便局と青木君が登録した郵便局からなる集合を S とします(二人が同じ郵便局に登録した場合、S にはその郵便局が 1 つだけ含まれます)。S に含まれる各郵便局について登録完了後の利用登録者数を求め、その合計を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq P \leq 10^9
- -10^9 \leq Q \leq 10^9
- -10^9 \leq X_i \leq 10^9
- 0 \leq C_i \leq 10^9
- X_i \neq X_j (i \neq j)
- 入力はすべて整数
入力
N P Q X_1 C_1 X_2 C_2 \vdots X_N C_N
- 1 行目には、郵便局の数を表す N 、高橋君の住んでいる座標を表す P 、青木君の住んでいる座標を表す Q が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各郵便局の情報が与えられる。
- 1 + i 行目では、 i 番目の郵便局の座標 X_i と現在の利用登録者数 C_i が、スペース区切りで与えられる。
出力
S に含まれる各郵便局の登録完了後の利用登録者数の合計を、1 行に出力してください。
入力例 1
3 5 15 0 10 10 20 20 30
出力例 1
32
入力例 2
4 0 0 -5 100 5 200 3 50 -3 80
出力例 2
82
入力例 3
5 -1000000000 1000000000 0 500000000 -999999999 100 1000000000 300000000 -500000000 200000000 500000000 150000000
出力例 3
300000102
Score : 333 pts
Problem Statement
Takahashi and Aoki have each finished moving and started living in a new town. They want to find the nearest post office from where they live and register for its services.
This town can be represented as a number line with a single road, and there are N post offices scattered along the road. The i-th post office is located at coordinate X_i and currently has C_i registered users. Note that the post offices are not necessarily ordered by coordinate.
Takahashi lives at coordinate P, and Aoki lives at coordinate Q. Each of them independently registers at the post office closest to where they live. Here, the distance between a person's residence and a post office is measured as the absolute difference of their coordinates. If there are multiple closest post offices, they choose the one with the smallest coordinate.
As a result of each person's registration, the number of registered users at their chosen post office increases by 1. If both register at the same post office, the number of registered users at that post office increases by 2.
After both registrations are complete, let S be the set of post offices where Takahashi and Aoki registered (if both registered at the same post office, S contains that post office only once). For each post office in S, determine the number of registered users after registration is complete, and output the total.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq P \leq 10^9
- -10^9 \leq Q \leq 10^9
- -10^9 \leq X_i \leq 10^9
- 0 \leq C_i \leq 10^9
- X_i \neq X_j (i \neq j)
- All inputs are integers
Input
N P Q X_1 C_1 X_2 C_2 \vdots X_N C_N
- The first line contains N, the number of post offices, P, the coordinate where Takahashi lives, and Q, the coordinate where Aoki lives, separated by spaces.
- From the 2nd line to the (N + 1)-th line, information about each post office is given.
- The (1 + i)-th line contains the coordinate X_i and the current number of registered users C_i of the i-th post office, separated by spaces.
Output
Output in one line the total number of registered users after registration is complete for each post office in S.
Sample Input 1
3 5 15 0 10 10 20 20 30
Sample Output 1
32
Sample Input 2
4 0 0 -5 100 5 200 3 50 -3 80
Sample Output 2
82
Sample Input 3
5 -1000000000 1000000000 0 500000000 -999999999 100 1000000000 300000000 -500000000 200000000 500000000 150000000
Sample Output 3
300000102
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は引っ越し業者でアルバイトをしています。今日の現場では、N 個の荷物と N 台の台車があります。
荷物 i (1 \leq i \leq N) の重さは W_i であり、台車 j (1 \leq j \leq N) の耐荷重は C_j です。荷物 i を台車 j に載せるには、荷物の重さが台車の耐荷重以下、すなわち W_i \leq C_j でなければなりません。
各台車には荷物を高々 1 つしか載せられず、各荷物も高々 1 台の台車にしか載せられません。台車に載せない荷物や、荷物を載せない台車があっても構いません。
高橋君は、できるだけ多くの荷物を台車に載せて運びたいと考えています。台車に載せて運べる荷物の最大個数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq 10^9 (1 \leq j \leq N)
- 入力はすべて整数である。
入力
N W_1 W_2 \ldots W_N C_1 C_2 \ldots C_N
- 1 行目には、荷物および台車の個数を表す整数 N が与えられる。
- 2 行目には、各荷物の重さを表す整数 W_1, W_2, \ldots, W_N が空白区切りで与えられる。
- 3 行目には、各台車の耐荷重を表す整数 C_1, C_2, \ldots, C_N が空白区切りで与えられる。
出力
運べる荷物の最大個数を 1 行で出力せよ。
入力例 1
4 3 5 2 8 4 7 1 6
出力例 1
3
入力例 2
7 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 2
7
入力例 3
10 5 12 8 3 15 20 7 1 9 50 10 6 25 2 4 14 8 30 3 11
出力例 3
9
Score : 366 pts
Problem Statement
Takahashi is working a part-time job at a moving company. At today's site, there are N packages and N carts.
The weight of package i (1 \leq i \leq N) is W_i, and the weight capacity of cart j (1 \leq j \leq N) is C_j. To place package i on cart j, the weight of the package must not exceed the weight capacity of the cart, i.e., W_i \leq C_j.
Each cart can hold at most 1 package, and each package can be placed on at most 1 cart. It is acceptable to have packages not placed on any cart, or carts with no packages.
Takahashi wants to load as many packages as possible onto the carts for transport. Find the maximum number of packages that can be loaded onto carts and transported.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq 10^9 (1 \leq j \leq N)
- All inputs are integers.
Input
N W_1 W_2 \ldots W_N C_1 C_2 \ldots C_N
- The first line contains an integer N representing the number of packages and carts.
- The second line contains integers W_1, W_2, \ldots, W_N separated by spaces, representing the weight of each package.
- The third line contains integers C_1, C_2, \ldots, C_N separated by spaces, representing the weight capacity of each cart.
Output
Print the maximum number of packages that can be transported in a single line.
Sample Input 1
4 3 5 2 8 4 7 1 6
Sample Output 1
3
Sample Input 2
7 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 2
7
Sample Input 3
10 5 12 8 3 15 20 7 1 9 50 10 6 25 2 4 14 8 30 3 11
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、整数の多重集合(同じ値を複数含むことができる集合)に対して要素の追加と削除を繰り返し、ある条件を満たす削除が何回発生するかを数えたいと思っています。
最初、多重集合は空です。以下の 2 種類のクエリが Q 個、順番に与えられます。i 番目のクエリで指定される値を X_i とします。
+X_i:多重集合に値 X_i を 1 つ追加する。-X_i:多重集合から値 X_i を 1 つ削除する。この時点で多重集合に値 X_i が少なくとも 1 つ存在することは保証される。
削除クエリにおいて、削除する値 X_i が 削除する直前の多重集合の中央値と一致する 場合、この削除を「中央値ヒット」と呼びます。
ここで、要素数 n(n \geq 1)の多重集合の中央値を次のように定めます。すべての要素を昇順に並べたとき、\lceil n/2 \rceil 番目(1-indexed)の値を中央値とします。すなわち、要素数が奇数のときは真ん中の値、偶数のときは真ん中 2 つのうち小さい方の値です。
削除クエリの時点では多重集合に少なくとも 1 つの要素が存在するため、中央値は常に定義されます。
すべてのクエリを順に処理したとき、「中央値ヒット」が発生した回数を求めてください。
制約
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X_i \leq 10^9
-X_i のクエリが与えられる時点で、多重集合に値 X_i が少なくとも 1 つ存在する。- 入力で与えられる数値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q Query_1 Query_2 \vdots Query_Q
各 Query_i(1 \leq i \leq Q)は + X_i または - X_i の形式であり、クエリの種類を表す文字(+ または -)と正の整数 X_i が空白区切りで与えられる。
出力
「中央値ヒット」が発生した回数を整数として一行で出力せよ。
入力例 1
6 + 1 + 3 + 2 - 2 - 1 - 3
出力例 1
3
入力例 2
8 + 10 + 20 - 20 + 15 + 25 - 25 - 10 - 15
出力例 2
2
入力例 3
15 + 5 + 1 + 5 + 3 + 3 - 3 + 4 - 5 + 2 - 3 + 5 - 4 - 5 - 1 - 2
出力例 3
4
入力例 4
25 + 1000000000 + 7 + 7 + 500 + 8 - 7 + 6 - 8 + 9 + 10 - 7 + 9 - 9 + 1 - 6 + 2 + 3 - 9 + 4 - 4 - 1000000000 - 3 - 1 - 10 - 2
出力例 4
7
入力例 5
1 + 1000000000
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi wants to repeatedly add and remove elements from a multiset of integers (a set that can contain multiple copies of the same value), and count how many removals satisfy a certain condition.
Initially, the multiset is empty. You are given Q queries in order, of the following 2 types. Let X_i denote the value specified by the i-th query.
+X_i: Add one copy of the value X_i to the multiset.-X_i: Remove one copy of the value X_i from the multiset. It is guaranteed that at least one copy of the value X_i exists in the multiset at this point.
For a removal query, if the value X_i being removed matches the median of the multiset immediately before the removal, this removal is called a "median hit".
Here, the median of a multiset with n elements (n \geq 1) is defined as follows: when all elements are sorted in ascending order, the \lceil n/2 \rceil-th value (1-indexed) is the median. In other words, when the number of elements is odd, it is the middle value; when even, it is the smaller of the two middle values.
Since the multiset contains at least one element at the time of a removal query, the median is always well-defined.
Process all queries in order and determine the number of times a "median hit" occurs.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X_i \leq 10^9
- At the time a
-X_i query is given, at least one copy of the value X_i exists in the multiset. - All numerical values given in the input are integers.
Input
Input is given from standard input in the following format:
Q Query_1 Query_2 \vdots Query_Q
Each Query_i (1 \leq i \leq Q) is in the format + X_i or - X_i, where the character representing the query type (+ or -) and the positive integer X_i are given separated by a space.
Output
Output the number of times a "median hit" occurs, as a single integer on one line.
Sample Input 1
6 + 1 + 3 + 2 - 2 - 1 - 3
Sample Output 1
3
Sample Input 2
8 + 10 + 20 - 20 + 15 + 25 - 25 - 10 - 15
Sample Output 2
2
Sample Input 3
15 + 5 + 1 + 5 + 3 + 3 - 3 + 4 - 5 + 2 - 3 + 5 - 4 - 5 - 1 - 2
Sample Output 3
4
Sample Input 4
25 + 1000000000 + 7 + 7 + 500 + 8 - 7 + 6 - 8 + 9 + 10 - 7 + 9 - 9 + 1 - 6 + 2 + 3 - 9 + 4 - 4 - 1000000000 - 3 - 1 - 10 - 2
Sample Output 4
7
Sample Input 5
1 + 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は N 種類の薬品を持っています。i 番目の薬品には通常モードと加熱モードの 2 つの使い方があり、通常モードで使うと A_i mL、加熱モードで使うと B_i mL の溶液が得られます。
高橋君はこれらの薬品の中から、いくつか(0 個でもよい)を選び、選んだ各薬品について通常モードと加熱モードのどちらで使うかを独立に決めます。各薬品は最大 1 回しか使えません。選んだ薬品から得られる溶液の量の合計がちょうど K mL となるようにできるかどうかを判定してください。
可能な場合は Yes を、不可能な場合は No を出力してください。
制約
- 1 \leq N \leq 26
- 0 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^{14}
- 1 \leq B_i \leq 10^{14}
- 入力はすべて整数
入力
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
- 1 行目には、薬品の種類数を表す整数 N と、目標の溶液量を表す整数 K が、スペース区切りで与えられる。
- 続く N 行にわたって、各薬品の情報が与えられる。
- 1 + i 行目には、i 番目の薬品の通常モードでの生成量 A_i と、加熱モードでの生成量 B_i が、スペース区切りで与えられる。
出力
選んだ薬品から得られる溶液の量の合計がちょうど K mL となるようにできるならば Yes を、そうでなければ No を 1 行で出力せよ。
入力例 1
3 10 3 5 4 6 1 8
出力例 1
Yes
入力例 2
2 5 2 4 6 8
出力例 2
No
入力例 3
8 57 11 14 7 10 9 15 13 20 17 18 6 16 8 12 5 21
出力例 3
Yes
入力例 4
20 590000000000000 10000000000000 11000000000000 12000000000000 13000000000000 14000000000000 15000000000000 16000000000000 17000000000000 18000000000000 19000000000000 20000000000000 21000000000000 22000000000000 23000000000000 24000000000000 25000000000000 26000000000000 27000000000000 28000000000000 29000000000000 30000000000000 31000000000000 32000000000000 33000000000000 34000000000000 35000000000000 36000000000000 37000000000000 38000000000000 39000000000000 40000000000000 41000000000000 42000000000000 43000000000000 44000000000000 45000000000000 46000000000000 47000000000000 48000000000000 49000000000000
出力例 4
Yes
入力例 5
1 0 1 100000000000000
出力例 5
Yes
Score : 433 pts
Problem Statement
Takahashi has N types of chemicals. The i-th chemical has two modes of use: normal mode and heated mode. Using it in normal mode yields A_i mL of solution, and using it in heated mode yields B_i mL of solution.
From these chemicals, Takahashi selects some (possibly zero) of them, and for each selected chemical, independently decides whether to use it in normal mode or heated mode. Each chemical can be used at most once. Determine whether it is possible for the total amount of solution obtained from the selected chemicals to be exactly K mL.
If it is possible, output Yes; if it is impossible, output No.
Constraints
- 1 \leq N \leq 26
- 0 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^{14}
- 1 \leq B_i \leq 10^{14}
- All input values are integers.
Input
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
- The first line contains an integer N representing the number of types of chemicals, and an integer K representing the target amount of solution, separated by a space.
- The following N lines provide information about each chemical.
- The (1 + i)-th line contains the amount produced in normal mode A_i and the amount produced in heated mode B_i for the i-th chemical, separated by a space.
Output
If it is possible for the total amount of solution obtained from the selected chemicals to be exactly K mL, output Yes; otherwise, output No on a single line.
Sample Input 1
3 10 3 5 4 6 1 8
Sample Output 1
Yes
Sample Input 2
2 5 2 4 6 8
Sample Output 2
No
Sample Input 3
8 57 11 14 7 10 9 15 13 20 17 18 6 16 8 12 5 21
Sample Output 3
Yes
Sample Input 4
20 590000000000000 10000000000000 11000000000000 12000000000000 13000000000000 14000000000000 15000000000000 16000000000000 17000000000000 18000000000000 19000000000000 20000000000000 21000000000000 22000000000000 23000000000000 24000000000000 25000000000000 26000000000000 27000000000000 28000000000000 29000000000000 30000000000000 31000000000000 32000000000000 33000000000000 34000000000000 35000000000000 36000000000000 37000000000000 38000000000000 39000000000000 40000000000000 41000000000000 42000000000000 43000000000000 44000000000000 45000000000000 46000000000000 47000000000000 48000000000000 49000000000000
Sample Output 4
Yes
Sample Input 5
1 0 1 100000000000000
Sample Output 5
Yes