実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は宝くじを購入しました。
今日は当選発表の日です。当選番号として K 個の互いに異なる正整数 A_1, A_2, \ldots, A_K が発表されました。高橋君は M 枚の宝くじを持っており、各宝くじにはそれぞれ 1 つの番号(正整数)が書かれています。高橋君の宝くじに書かれている番号を順に B_1, B_2, \ldots, B_M とすると、これらはすべて互いに異なります。
高橋君が持っている宝くじのうち、当選番号のいずれかと一致する番号が書かれているものの枚数を求めてください。
制約
- 1 \leq K \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq K)
- 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
- A_1, A_2, \ldots, A_K はすべて異なる
- B_1, B_2, \ldots, B_M はすべて異なる
- 入力はすべて整数
入力
K M A_1 A_2 \ldots A_K B_1 B_2 \ldots B_M
- 1 行目には、当選番号の個数 K と、高橋君が持っている宝くじの枚数 M が、空白区切りで与えられる。
- 2 行目には、当選番号 A_1, A_2, \ldots, A_K が、空白区切りで与えられる。
- 3 行目には、高橋君が持っている宝くじの番号 B_1, B_2, \ldots, B_M が、空白区切りで与えられる。
出力
高橋君が持っている宝くじのうち、当選番号のいずれかと一致する番号が書かれているものの枚数を 1 行で出力せよ。
入力例 1
5 3 1 2 3 4 5 2 7 4
出力例 1
2
入力例 2
4 6 100 200 300 400 150 200 250 300 350 400
出力例 2
3
入力例 3
10 8 1000000000 999999999 123456789 987654321 555555555 111111111 222222222 333333333 444444444 666666666 123456789 100000000 555555555 777777777 888888888 999999999 12345 67890
出力例 3
3
Score : 266 pts
Problem Statement
Takahashi purchased lottery tickets.
Today is the day of the winning number announcement. K distinct positive integers A_1, A_2, \ldots, A_K were announced as the winning numbers. Takahashi has M lottery tickets, and each ticket has one number (a positive integer) written on it. Letting B_1, B_2, \ldots, B_M denote the numbers written on Takahashi's lottery tickets in order, these are all distinct from each other.
Find the number of Takahashi's lottery tickets that have a number matching one of the winning numbers.
Constraints
- 1 \leq K \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq K)
- 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
- A_1, A_2, \ldots, A_K are all distinct
- B_1, B_2, \ldots, B_M are all distinct
- All inputs are integers
Input
K M A_1 A_2 \ldots A_K B_1 B_2 \ldots B_M
- The first line contains the number of winning numbers K and the number of lottery tickets Takahashi has M, separated by a space.
- The second line contains the winning numbers A_1, A_2, \ldots, A_K, separated by spaces.
- The third line contains the numbers on Takahashi's lottery tickets B_1, B_2, \ldots, B_M, separated by spaces.
Output
Print in one line the number of Takahashi's lottery tickets that have a number matching one of the winning numbers.
Sample Input 1
5 3 1 2 3 4 5 2 7 4
Sample Output 1
2
Sample Input 2
4 6 100 200 300 400 150 200 250 300 350 400
Sample Output 2
3
Sample Input 3
10 8 1000000000 999999999 123456789 987654321 555555555 111111111 222222222 333333333 444444444 666666666 123456789 100000000 555555555 777777777 888888888 999999999 12345 67890
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君はテーブルの上に N 枚のカードを並べました。各カードには表面と裏面があり、それぞれに整数が書かれています。 i 番目のカードの表面には整数 F_i が、裏面には整数 B_i が書かれています。最初、すべてのカードは表面が上を向いた状態で置かれています。
高橋君は、これらの N 枚のカードの中から異なる K 枚を選んで裏返す(裏面が上を向くようにする)ことができます。裏返さなかった残りのカードは表面が上を向いたままです。
すべてのカードの上を向いている面に書かれた整数の合計値を最大化するとき、その最大値を求めてください。
制約
- 1 \leq N \leq 20
- 0 \leq K \leq N
- 1 \leq F_i \leq 100
- 1 \leq B_i \leq 100
- 入力はすべて整数である
入力
N K F_1 B_1 F_2 B_2 \vdots F_N B_N
- 1 行目には、カードの枚数を表す整数 N と、裏返すカードの枚数を表す整数 K が、スペース区切りで与えられる。
- i + 1 行目 (1 \leq i \leq N) には、 i 番目のカードの表面の値 F_i と裏面の値 B_i が、スペース区切りで与えられる。
出力
高橋君がちょうど K 枚のカードを裏返したときの、上を向いている面に書かれた整数の合計値の最大値を 1 行で出力してください。
入力例 1
3 1 1 5 3 2 4 1
出力例 1
12
入力例 2
3 0 2 5 3 1 4 3
出力例 2
9
入力例 3
8 3 10 20 15 5 8 12 6 6 20 25 3 1 7 18 9 4
出力例 3
104
入力例 4
15 5 12 30 25 10 8 15 50 45 33 40 19 2 7 55 44 44 16 28 5 90 61 20 38 42 14 9 27 35 10 10
出力例 4
540
入力例 5
1 1 3 7
出力例 5
7
Score : 300 pts
Problem Statement
Takahashi has placed N cards on a table. Each card has a front side and a back side, each with an integer written on it. The front side of the i-th card has the integer F_i, and the back side has the integer B_i. Initially, all cards are placed with their front side facing up.
Takahashi can choose exactly K distinct cards from these N cards and flip them over (so that their back side faces up). The remaining cards that are not flipped stay with their front side facing up.
Find the maximum possible value of the sum of the integers written on the face-up sides of all the cards.
Constraints
- 1 \leq N \leq 20
- 0 \leq K \leq N
- 1 \leq F_i \leq 100
- 1 \leq B_i \leq 100
- All input values are integers
Input
N K F_1 B_1 F_2 B_2 \vdots F_N B_N
- The first line contains the integer N representing the number of cards and the integer K representing the number of cards to flip, separated by a space.
- The (i + 1)-th line (1 \leq i \leq N) contains the front side value F_i and the back side value B_i of the i-th card, separated by a space.
Output
Print on a single line the maximum possible sum of the integers written on the face-up sides when Takahashi flips exactly K cards.
Sample Input 1
3 1 1 5 3 2 4 1
Sample Output 1
12
Sample Input 2
3 0 2 5 3 1 4 3
Sample Output 2
9
Sample Input 3
8 3 10 20 15 5 8 12 6 6 20 25 3 1 7 18 9 4
Sample Output 3
104
Sample Input 4
15 5 12 30 25 10 8 15 50 45 33 40 19 2 7 55 44 44 16 28 5 90 61 20 38 42 14 9 27 35 10 10
Sample Output 4
540
Sample Input 5
1 1 3 7
Sample Output 5
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は文化祭でフリーマーケットの運営を担当しています。フリーマーケットには N 個の出店ブースがあり、1 から N までの番号が付けられています。
各ブースは一直線上に配置されており、ブース i (1 \leq i \leq N) の 座標 は X_i、集客力 は S_i です。
文化祭の企画として、近くにあるブース同士でコラボレーション販売を行うことになりました。具体的には、二つの異なるブースの組 (i, j) (1 \leq i < j \leq N) について、座標の差の絶対値 |X_i - X_j| が D 以下であるとき、お客さんが両方のブースを行き来しやすいため、コラボレーションが成立します。|X_i - X_j| が D より大きいとき、コラボレーションは成立しません。
コラボレーションが成立するペアの「相乗効果」は、そのペアの二つのブースの集客力の積 S_i \times S_j で定義されます。
高橋君は、コラボレーションが成立するすべてのペアについて、相乗効果の総和を求めたいと考えています。
N 個のブースについて、それぞれの座標と集客力が与えられるので、コラボレーションが成立するすべてのブースのペア (i, j) (1 \leq i < j \leq N) について、相乗効果 S_i \times S_j の総和を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq D \leq 10^9
- 0 \leq X_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq S_i \leq 10^4 (1 \leq i \leq N)
- X_i はすべて異なる
- 入力はすべて整数
- 答え(相乗効果の総和)は 2^{63} - 1 以下であることが保証される
入力
N D X_1 S_1 X_2 S_2 \vdots X_N S_N
- 1 行目には、ブースの数 N と、コラボレーションが成立する座標の差の上限 D が、スペース区切りで与えられる。
- 続く N 行のうち i 行目 (1 \leq i \leq N) には、ブース i の座標 X_i と集客力 S_i が、スペース区切りで与えられる。
出力
コラボレーションが成立するすべてのペアの相乗効果の総和を 1 行で出力せよ。
入力例 1
3 3 1 2 3 3 7 4
出力例 1
6
入力例 2
4 10 1 1 2 2 5 3 8 4
出力例 2
35
入力例 3
8 5 0 3 2 7 4 1 8 5 10 2 15 6 17 4 20 8
出力例 3
162
入力例 4
15 100 10 50 200 30 350 80 500 20 610 90 750 40 880 60 1000 10 1050 70 1200 100 1350 25 1500 55 1600 35 1700 45 1800 15
出力例 4
4875
入力例 5
1 1000000000 500000000 10000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is in charge of running a flea market at a school festival. The flea market has N vendor booths, numbered from 1 to N.
The booths are arranged on a straight line. Booth i (1 \leq i \leq N) has coordinate X_i and attractiveness S_i.
As a festival event, nearby booths will conduct collaboration sales with each other. Specifically, for a pair of two distinct booths (i, j) (1 \leq i < j \leq N), if the absolute difference in their coordinates |X_i - X_j| is at most D, customers can easily go back and forth between both booths, so the collaboration is established. If |X_i - X_j| is greater than D, the collaboration is not established.
The "synergy" of a pair where collaboration is established is defined as the product of the attractiveness values of the two booths: S_i \times S_j.
Takahashi wants to find the total sum of synergy over all pairs where collaboration is established.
Given the coordinate and attractiveness of each of the N booths, find the sum of the synergy S_i \times S_j over all pairs of booths (i, j) (1 \leq i < j \leq N) where collaboration is established.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq D \leq 10^9
- 0 \leq X_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq S_i \leq 10^4 (1 \leq i \leq N)
- All X_i are distinct
- All input values are integers
- It is guaranteed that the answer (the total sum of synergy) is at most 2^{63} - 1
Input
N D X_1 S_1 X_2 S_2 \vdots X_N S_N
- The first line contains the number of booths N and the upper limit D on the coordinate difference for collaboration to be established, separated by a space.
- The i-th of the following N lines (1 \leq i \leq N) contains the coordinate X_i and attractiveness S_i of booth i, separated by a space.
Output
Print the total sum of synergy over all pairs where collaboration is established, in a single line.
Sample Input 1
3 3 1 2 3 3 7 4
Sample Output 1
6
Sample Input 2
4 10 1 1 2 2 5 3 8 4
Sample Output 2
35
Sample Input 3
8 5 0 3 2 7 4 1 8 5 10 2 15 6 17 4 20 8
Sample Output 3
162
Sample Input 4
15 100 10 50 200 30 350 80 500 20 610 90 750 40 880 60 1000 10 1050 70 1200 100 1350 25 1500 55 1600 35 1700 45 1800 15
Sample Output 4
4875
Sample Input 5
1 1000000000 500000000 10000
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は配送会社で働いています。今日、高橋君のもとに N 件の配送依頼が届きました。
配送エリアは 1 から M までの番号がついた M 個の拠点と、それらを結ぶ K 本の双方向の道路で構成されています。 j 番目の道路 (1 \leq j \leq K) は拠点 U_j と拠点 V_j を結び、その道路の長さは W_j です。
i 番目の配送依頼 (1 \leq i \leq N) では、出発拠点 S_i から届け先拠点 T_i まで荷物を届ける必要があります。各配送依頼について、出発拠点から届け先拠点までの最短距離(通る道路の長さの合計の最小値)を求め、 N 件すべての最短距離の合計を計算してください。
なお、すべての配送依頼について、出発拠点から届け先拠点へ道路を辿って到達可能であることが保証されています。
制約
- 1 \leq N \leq 10^5
- 2 \leq M \leq 200
- 1 \leq K \leq \frac{M(M-1)}{2}
- 1 \leq U_j < V_j \leq M (1 \leq j \leq K)
- 1 \leq W_j \leq 10^6 (1 \leq j \leq K)
- 同じ拠点の組を結ぶ道路は高々 1 本である(すなわち、(U_j, V_j) の組はすべて異なる)
- 1 \leq S_i \leq M (1 \leq i \leq N)
- 1 \leq T_i \leq M (1 \leq i \leq N)
- S_i \neq T_i (1 \leq i \leq N)
- すべての配送依頼について、S_i から T_i へ到達可能である
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_K V_K W_K S_1 T_1 S_2 T_2 \vdots S_N T_N
- 1 行目には、配送依頼の件数 N、拠点の数 M、道路の本数 K が空白区切りで与えられる。
- 続く K 行のうち j 行目には、j 番目の道路が結ぶ 2 つの拠点 U_j, V_j と、その道路の長さ W_j が空白区切りで与えられる。
- 続く N 行のうち i 行目には、i 番目の配送依頼の出発拠点 S_i と届け先拠点 T_i が空白区切りで与えられる。
出力
N 件の配送依頼における最短距離の合計を 1 行で出力せよ。
入力例 1
2 3 3 1 2 3 2 3 4 1 3 10 1 3 2 3
出力例 1
11
入力例 2
4 5 7 1 2 2 1 3 8 2 3 3 2 4 5 3 4 1 3 5 6 4 5 4 1 5 3 1 4 5 1 4
出力例 2
25
入力例 3
5 6 9 1 2 7 1 3 9 1 6 14 2 3 10 2 4 15 3 4 11 3 6 2 4 5 6 5 6 9 1 5 2 6 3 4 6 4 1 4
出力例 3
76
Score : 400 pts
Problem Statement
Takahashi works at a delivery company. Today, Takahashi received N delivery requests.
The delivery area consists of M bases numbered from 1 to M, connected by K bidirectional roads. The j-th road (1 \leq j \leq K) connects base U_j and base V_j, and has length W_j.
For the i-th delivery request (1 \leq i \leq N), a package must be delivered from the departure base S_i to the destination base T_i. For each delivery request, find the shortest distance from the departure base to the destination base (the minimum total length of roads traversed), and calculate the sum of the shortest distances for all N requests.
It is guaranteed that for every delivery request, the destination base is reachable from the departure base by following roads.
Constraints
- 1 \leq N \leq 10^5
- 2 \leq M \leq 200
- 1 \leq K \leq \frac{M(M-1)}{2}
- 1 \leq U_j < V_j \leq M (1 \leq j \leq K)
- 1 \leq W_j \leq 10^6 (1 \leq j \leq K)
- There is at most one road connecting any pair of bases (i.e., all pairs (U_j, V_j) are distinct)
- 1 \leq S_i \leq M (1 \leq i \leq N)
- 1 \leq T_i \leq M (1 \leq i \leq N)
- S_i \neq T_i (1 \leq i \leq N)
- For every delivery request, T_i is reachable from S_i
- All input values are integers
Input
The input is given from standard input in the following format:
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_K V_K W_K S_1 T_1 S_2 T_2 \vdots S_N T_N
- The first line contains the number of delivery requests N, the number of bases M, and the number of roads K, separated by spaces.
- In the following K lines, the j-th line contains the two bases U_j, V_j connected by the j-th road, and the length W_j of that road, separated by spaces.
- In the following N lines, the i-th line contains the departure base S_i and the destination base T_i of the i-th delivery request, separated by spaces.
Output
Output the sum of the shortest distances for all N delivery requests in a single line.
Sample Input 1
2 3 3 1 2 3 2 3 4 1 3 10 1 3 2 3
Sample Output 1
11
Sample Input 2
4 5 7 1 2 2 1 3 8 2 3 3 2 4 5 3 4 1 3 5 6 4 5 4 1 5 3 1 4 5 1 4
Sample Output 2
25
Sample Input 3
5 6 9 1 2 7 1 3 9 1 6 14 2 3 10 2 4 15 3 4 11 3 6 2 4 5 6 5 6 9 1 5 2 6 3 4 6 4 1 4
Sample Output 3
76
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は市の除雪作業の計画を担当しています。市内には N 個の交差点があり、交差点には 1 から N までの番号が付けられています。交差点間は M 本の道路で結ばれており、各道路は双方向に通行可能です。同じ頂点対を結ぶ道路が複数存在することもありますが、それらは異なる道路として区別されます。 i 番目の道路は交差点 U_i と交差点 V_i を結んでおり、除雪車がどちらの方向に通過する場合も W_i 分かかります。
高橋君は除雪車を交差点 1 にある車庫から出発させ、 M 本の道路のすべてをそれぞれ少なくとも 1 回ずつ通り、最終的に交差点 1 の車庫に戻るルートを計画しなければなりません。各道路はいずれか一方の方向に 1 回通過すれば除雪されたものとみなし、両方向それぞれで通過する必要はありません。また、除雪車は同じ道路を同じ方向・異なる方向を問わず 2 回以上通ることもできます。途中で交差点 1 を何度経由しても構いません。燃料には限りがあるため、できるだけ短い時間で巡回を完了させたいと考えています。
除雪車が交差点 1 を出発し、すべての道路をそれぞれ少なくとも 1 回ずつ通り、交差点 1 に戻ってくるために必要な最小の合計移動時間(分)を求めてください。
なお、与えられるグラフは連結であることが保証されます。
制約
- 2 \leq N \leq 10^5
- N - 1 \leq M \leq 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^6
- グラフは連結である
- 奇数次数の頂点の個数は 16 以下である。ここで頂点の次数とは、その頂点に接続する辺の本数であり、多重辺がある場合も各辺を 1 本ずつ別々に数える
- 同じ頂点対を結ぶ道路が複数存在することがある(多重辺が存在しうる)
- 入力はすべて整数である
入力
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- 1 行目には、交差点の数を表す N と道路の数を表す M が、スペース区切りで与えられる。
- 2 行目から M 行では、各道路の情報が与えられる。
- 1 + i 行目では、 i 番目の道路が結ぶ 2 つの交差点 U_i , V_i と、通過にかかる時間 W_i (分)が、スペース区切りで与えられる。
出力
除雪車がすべての道路をそれぞれ少なくとも 1 回ずつ通り、交差点 1 に戻るために必要な最小の合計移動時間を、整数で 1 行に出力せよ。
入力例 1
3 3 1 2 4 2 3 5 3 1 6
出力例 1
15
入力例 2
3 2 1 2 3 2 3 5
出力例 2
16
入力例 3
8 11 1 2 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 1 8 1 3 2 2 5 3 4 7 1
出力例 3
48
入力例 4
15 20 1 2 10 2 3 8 3 4 7 4 5 12 5 6 3 6 7 9 7 8 5 8 9 11 9 10 6 10 11 4 11 12 8 12 13 7 13 14 13 14 15 2 15 1 14 1 8 20 3 10 15 5 12 6 7 14 9 2 9 18
出力例 4
228
入力例 5
2 1 1 2 1000000
出力例 5
2000000
Score : 466 pts
Problem Statement
Takahashi is in charge of planning the city's snow removal operations. The city has N intersections, numbered from 1 to N. The intersections are connected by M roads, each of which is passable in both directions. There may be multiple roads connecting the same pair of vertices, but they are treated as distinct roads. The i-th road connects intersection U_i and intersection V_i, and it takes W_i minutes for the snow plow to traverse it in either direction.
Takahashi must plan a route for the snow plow that starts from the depot at intersection 1, traverses each of the M roads at least once, and finally returns to the depot at intersection 1. A road is considered cleared of snow once it is traversed in either one direction; it is not necessary to traverse it in both directions. The snow plow may traverse the same road more than once, regardless of whether it is in the same or opposite direction. It is also allowed to pass through intersection 1 any number of times during the route. Since fuel is limited, Takahashi wants to complete the patrol in as short a time as possible.
Find the minimum total travel time (in minutes) required for the snow plow to depart from intersection 1, traverse every road at least once, and return to intersection 1.
It is guaranteed that the given graph is connected.
Constraints
- 2 \leq N \leq 10^5
- N - 1 \leq M \leq 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^6
- The graph is connected
- The number of vertices with odd degree is at most 16. Here, the degree of a vertex is the number of edges incident to it, where each edge of a multi-edge is counted separately
- There may be multiple roads connecting the same pair of vertices (multi-edges may exist)
- All input values are integers
Input
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- The first line contains N, the number of intersections, and M, the number of roads, separated by a space.
- The following M lines provide information about each road.
- The (1 + i)-th line contains the two intersections U_i, V_i connected by the i-th road, and the time W_i (in minutes) required to traverse it, separated by spaces.
Output
Output in a single line the minimum total travel time, as an integer, required for the snow plow to traverse every road at least once and return to intersection 1.
Sample Input 1
3 3 1 2 4 2 3 5 3 1 6
Sample Output 1
15
Sample Input 2
3 2 1 2 3 2 3 5
Sample Output 2
16
Sample Input 3
8 11 1 2 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 1 8 1 3 2 2 5 3 4 7 1
Sample Output 3
48
Sample Input 4
15 20 1 2 10 2 3 8 3 4 7 4 5 12 5 6 3 6 7 9 7 8 5 8 9 11 9 10 6 10 11 4 11 12 8 12 13 7 13 14 13 14 15 2 15 1 14 1 8 20 3 10 15 5 12 6 7 14 9 2 9 18
Sample Output 4
228
Sample Input 5
2 1 1 2 1000000
Sample Output 5
2000000