Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 166 点
問題文
高橋君はオンラインゲームをプレイしています。現在の高橋君のスコアは A 点です。ランキング上位に入るにはスコアが B 点以上である必要があります。
高橋君がスコアを増やす唯一の方法は、ゲーム内のアイテムショップで「スコアブースター」を購入することです。スコアブースターは 1 個あたり C コインで購入でき、1 個購入するごとにスコアが 1 点増加します。スコアブースターは 0 個以上の任意の個数を購入できます。
高橋君のスコアを B 点以上にするために必要なコインの最小額を求めてください。
なお、現在のスコアがすでに B 点以上である場合は、スコアブースターを購入する必要がないため、答えは 0 です。
制約
- 1 \leq A \leq 10^9
- 1 \leq B \leq 10^9
- 1 \leq C \leq 10^9
- 入力はすべて整数である
入力
A B C
1 行に、高橋君の現在のスコア A、ランキング上位に必要な最低スコア B、スコアブースター 1 個あたりの価格 C がスペース区切りで与えられる。
出力
高橋君のスコアを B 点以上にするために必要なコインの最小額を 1 行で出力せよ。
なお、答えは最大で 10^{18} 程度になる場合があることに注意せよ。
入力例 1
50 100 3
出力例 1
150
入力例 2
150 100 5
出力例 2
0
入力例 3
999999000 1000000000 100
出力例 3
100000
Score : 166 pts
Problem Statement
Takahashi is playing an online game. His current score is A points. To reach the top of the rankings, he needs a score of at least B points.
The only way for Takahashi to increase his score is by purchasing "Score Boosters" from the in-game item shop. Each Score Booster costs C coins, and each one purchased increases his score by 1 point. He can purchase any number of Score Boosters, including 0.
Find the minimum number of coins required to bring Takahashi's score to at least B points.
Note that if his current score is already at least B points, he does not need to purchase any Score Boosters, so the answer is 0.
Constraints
- 1 \leq A \leq 10^9
- 1 \leq B \leq 10^9
- 1 \leq C \leq 10^9
- All inputs are integers
Input
A B C
A single line contains Takahashi's current score A, the minimum score B required to reach the top of the rankings, and the price C per Score Booster, separated by spaces.
Output
Print on a single line the minimum number of coins required to bring Takahashi's score to at least B points.
Note that the answer may be as large as approximately 10^{18}.
Sample Input 1
50 100 3
Sample Output 1
150
Sample Input 2
150 100 5
Sample Output 2
0
Sample Input 3
999999000 1000000000 100
Sample Output 3
100000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は、ある学校の生徒会長です。この学校には N 人の生徒がおり、それぞれ 1 から N までの出席番号が付けられています。
この学校では、生徒同士の間で「友達申請」を送ることができるシステムがあります。友達申請は一方向のものです。すなわち、生徒 u から生徒 v への友達申請と、生徒 v から生徒 u への友達申請は異なる申請として扱われ、一方のみが存在することも、両方が同時に存在することもありえます。
現在、M 件の友達申請が送られています。i 件目 (1 \leq i \leq M) の友達申請は、生徒 A_i から生徒 B_i へ送られたものです。
高橋君は、学校祭でペアを組んで活動するイベントを企画しています。このイベントでは、相互に友達申請を送り合っている生徒のペアだけが参加できます。ここで、生徒 u と生徒 v が「相互に友達申請を送り合っている」とは、生徒 u から生徒 v への友達申請と、生徒 v から生徒 u への友達申請の両方が存在する状態を指します。
高橋君のために、相互に友達申請を送り合っている生徒のペアの数を求めてください。ただし、生徒 u と生徒 v のペアと、生徒 v と生徒 u のペアは同じペアとして 1 つと数えます。
制約
- 1 \leq N \leq 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N (1 \leq i \leq M)
- A_i \neq B_i(自分自身への友達申請はない)
- i \neq j ならば (A_i, B_i) \neq (A_j, B_j)(同じ生徒から同じ生徒への友達申請は高々 1 件である)
- 入力はすべて整数である
入力
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
1 行目には、生徒の人数 N と友達申請の件数 M が、スペース区切りで与えられる。
続く M 行のうち i 番目の行 (1 \leq i \leq M) には、生徒 A_i から生徒 B_i への友達申請を表す整数の組 A_i, B_i がスペース区切りで与えられる。
出力
相互に友達申請を送り合っている生徒のペアの数を 1 行で出力してください。
入力例 1
4 5 1 2 2 1 1 3 3 4 4 3
出力例 1
2
入力例 2
3 6 1 2 2 1 2 3 3 2 1 3 3 1
出力例 2
3
入力例 3
10 12 1 2 2 1 3 4 4 3 5 6 6 5 7 8 1 3 2 4 9 10 10 9 8 7
出力例 3
5
Score : 266 pts
Problem Statement
Takahashi is the student council president of a school. There are N students in this school, each assigned a student number from 1 to N.
This school has a system where students can send "friend requests" to each other. Friend requests are one-directional. That is, a friend request from student u to student v and a friend request from student v to student u are treated as different requests — it is possible for only one of them to exist, or for both to exist simultaneously.
Currently, M friend requests have been sent. The i-th friend request (1 \leq i \leq M) was sent from student A_i to student B_i.
Takahashi is planning an event for the school festival where students participate in pairs. Only pairs of students who have mutually sent friend requests to each other can participate in this event. Here, students u and v "have mutually sent friend requests to each other" means that both a friend request from student u to student v and a friend request from student v to student u exist.
For Takahashi, find the number of pairs of students who have mutually sent friend requests to each other. Note that the pair of student u and student v and the pair of student v and student u are considered the same pair and counted as 1.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N (1 \leq i \leq M)
- A_i \neq B_i (no friend request to oneself)
- If i \neq j, then (A_i, B_i) \neq (A_j, B_j) (there is at most one friend request from the same student to the same student)
- All input values are integers
Input
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
The first line contains the number of students N and the number of friend requests M, separated by a space.
The i-th of the following M lines (1 \leq i \leq M) contains a pair of integers A_i, B_i separated by a space, representing a friend request from student A_i to student B_i.
Output
Print the number of pairs of students who have mutually sent friend requests to each other, in a single line.
Sample Input 1
4 5 1 2 2 1 1 3 3 4 4 3
Sample Output 1
2
Sample Input 2
3 6 1 2 2 1 2 3 3 2 1 3 3 1
Sample Output 2
3
Sample Input 3
10 12 1 2 2 1 3 4 4 3 5 6 6 5 7 8 1 3 2 4 9 10 10 9 8 7
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君はチェスの勝ち抜き大会を主催することになりました。この大会には N 人の選手が参加し、それぞれ 1 から N までの番号が付けられています。各選手 i にはレーティング S_i が定められており、すべての選手のレーティングは異なります。
大会では、最後に 1 人の選手が残るまで試合を繰り返します。各試合では、その時点でまだ脱落していない選手の中から 2 人を選んで 1 対 1 で戦わせます。試合ではレーティングが大きい方の選手が必ず勝利し、負けた選手は大会から脱落します。したがって、大会全体で合計 N - 1 回の試合が行われます。
どの試合でどの 2 人を戦わせるかは、主催者である高橋君が自由に決めることができます。高橋君はすべての選手のレーティングを事前に知っており、試合の勝敗は完全に予測できるため、大会開始前にすべての対戦の組み合わせを最適に計画できます。
高橋君には応援している選手がいます。それは選手 K です。高橋君は対戦の組み合わせを最適に決めることで、選手 K が勝利する試合の回数をできるだけ大きくしたいと考えています。選手 K は、自分よりレーティングが大きい選手と対戦すると負けて脱落し、それ以降は勝利数が増えることはありません。
選手 K が最大で何回勝利できるかを求めてください。
制約
- 2 \leq N \leq 10^5
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^9
- S_i はすべて異なる
- 入力はすべて整数
入力
N K S_1 S_2 \ldots S_N
- 1 行目には、選手の人数を表す N と、応援する選手の番号を表す K が、スペース区切りで与えられる。
- 2 行目には、各選手のレーティングを表す S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
出力
選手 K が最大で勝利できる試合数を 1 行で出力せよ。
入力例 1
4 3 5 1 3 2
出力例 1
2
入力例 2
5 2 3 10 7 1 5
出力例 2
4
入力例 3
10 5 20 50 10 5 30 80 15 25 40 60
出力例 3
5
Score : 300 pts
Problem Statement
Takahashi is going to organize a chess elimination tournament. N players participate in this tournament, each numbered from 1 to N. Each player i has a rating S_i, and all players have distinct ratings.
In the tournament, matches are repeated until only 1 player remains. In each match, 2 players who have not yet been eliminated are chosen to compete against each other in a one-on-one match. The player with the higher rating always wins, and the losing player is eliminated from the tournament. Therefore, a total of N - 1 matches are held throughout the tournament.
Takahashi, as the organizer, is free to decide which 2 players compete in each match. Since Takahashi knows all players' ratings in advance and can perfectly predict the outcome of every match, he can optimally plan all match pairings before the tournament begins.
Takahashi has a player he is rooting for: player K. Takahashi wants to optimally arrange the match pairings to maximize the number of matches that player K wins. Player K will lose and be eliminated if matched against a player with a higher rating, after which their win count can no longer increase.
Determine the maximum number of matches player K can win.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^9
- All S_i are distinct
- All inputs are integers
Input
N K S_1 S_2 \ldots S_N
- The first line contains N, the number of players, and K, the number of the player Takahashi is rooting for, separated by a space.
- The second line contains the ratings S_1, S_2, \ldots, S_N of each player, separated by spaces.
Output
Output in one line the maximum number of matches player K can win.
Sample Input 1
4 3 5 1 3 2
Sample Output 1
2
Sample Input 2
5 2 3 10 7 1 5
Sample Output 2
4
Sample Input 3
10 5 20 50 10 5 30 80 15 25 40 60
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は、ある地域の都市計画を担当することになりました。この地域には N 個の都市があり、それぞれの都市には 1 から N までの番号が付けられています。
各都市 i(1 \leq i \leq N)の位置は M 次元空間上の整数座標で表され、その第 k 次元の座標値を A_{i,k} とします(1 \leq k \leq M)。
高橋君は、2つの都市間の距離をマンハッタン距離で定義しています。すなわち、都市 i と都市 j の距離は \displaystyle\sum_{k=1}^{M} |A_{i,k} - A_{j,k}| です。
高橋君は、異なる2都市の順序なしの組すべてについて距離を計算し、その総和を求めたいと考えています。
具体的には、次の値を求めてください。
\sum_{1 \leq i < j \leq N} \sum_{k=1}^{M} |A_{i,k} - A_{j,k}|
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10
- -10^6 \leq A_{i,k} \leq 10^6
- N, M, A_{i,k} はすべて整数
- 答えは符号付き64ビット整数(2^{63} - 1 以下の非負整数)に収まることが保証される
入力
N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
- 1 行目には、都市の個数 N と座標の次元数 M が、スペース区切りで与えられる。
- 続く N 行のうち i 番目の行(1 \leq i \leq N)には、都市 i の M 次元の座標値 A_{i,1}, A_{i,2}, \ldots, A_{i,M} がスペース区切りで与えられる。
出力
すべての異なる2都市の組についてのマンハッタン距離の総和を、整数として 1 行で出力せよ。
入力例 1
3 2 1 2 4 6 7 1
出力例 1
22
入力例 2
2 1 3 10
出力例 2
7
入力例 3
6 3 1 0 3 5 2 1 -3 4 7 2 -1 0 6 3 5 -1 2 4
出力例 3
146
入力例 4
10 4 100 -200 300 -400 -150 250 -350 450 200 -100 400 -300 -250 150 -450 350 300 -300 200 -200 -100 400 -100 500 50 -50 150 -150 -350 350 -250 250 400 -400 100 -100 -200 200 -300 300
出力例 4
62500
入力例 5
2 1 -1000000 1000000
出力例 5
2000000
Score : 333 pts
Problem Statement
Takahashi has been put in charge of urban planning for a certain region. This region has N cities, each numbered from 1 to N.
The position of each city i (1 \leq i \leq N) is represented by integer coordinates in M-dimensional space, where A_{i,k} denotes the coordinate value in the k-th dimension (1 \leq k \leq M).
Takahashi defines the distance between two cities using the Manhattan distance. That is, the distance between city i and city j is \displaystyle\sum_{k=1}^{M} |A_{i,k} - A_{j,k}|.
Takahashi wants to compute the distances for all unordered pairs of distinct cities and find their total sum.
Specifically, compute the following value:
\sum_{1 \leq i < j \leq N} \sum_{k=1}^{M} |A_{i,k} - A_{j,k}|
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10
- -10^6 \leq A_{i,k} \leq 10^6
- N, M, A_{i,k} are all integers
- It is guaranteed that the answer fits in a signed 64-bit integer (a non-negative integer at most 2^{63} - 1)
Input
N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
- The first line contains the number of cities N and the number of dimensions M, separated by a space.
- The i-th of the following N lines (1 \leq i \leq N) contains the M-dimensional coordinate values of city i: A_{i,1}, A_{i,2}, \ldots, A_{i,M}, separated by spaces.
Output
Output the total sum of Manhattan distances over all unordered pairs of distinct cities, as a single integer on one line.
Sample Input 1
3 2 1 2 4 6 7 1
Sample Output 1
22
Sample Input 2
2 1 3 10
Sample Output 2
7
Sample Input 3
6 3 1 0 3 5 2 1 -3 4 7 2 -1 0 6 3 5 -1 2 4
Sample Output 3
146
Sample Input 4
10 4 100 -200 300 -400 -150 250 -350 450 200 -100 400 -300 -250 150 -450 350 300 -300 200 -200 -100 400 -100 500 50 -50 150 -150 -350 350 -250 250 400 -400 100 -100 -200 200 -300 300
Sample Output 4
62500
Sample Input 5
2 1 -1000000 1000000
Sample Output 5
2000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は、アルバイト先の店長としてシフト管理を任されています。来週、スタッフを配置したい時間帯が N 個あります。
アルバイトスタッフの候補者は M 人おり、1 から M までの番号が付いています。各時間帯 i (1 \leq i \leq N) に対して、その時間帯に勤務可能な候補者の番号が K_i 個与えられます。具体的には、時間帯 i に勤務可能な候補者の番号は C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} です。
高橋君は、各時間帯に対して、その時間帯に勤務可能な候補者の中から 1 人を割り当てるか、誰も割り当てないかのいずれかを選びます。ただし、同じ候補者を複数の時間帯に割り当てることはできません。すなわち、各候補者は高々 1 つの時間帯にのみ割り当てることができます。
候補者を割り当てた時間帯の数の最大値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 0 \leq K_i \leq M (1 \leq i \leq N)
- 1 \leq C_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq K_i)
- 各 i について、C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} はすべて異なる
- 入力はすべて整数
入力
N M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
- 1 行目には、時間帯の数 N と候補者の数 M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目には、各時間帯に勤務可能な候補者の情報が与えられる。
- 1 + i 行目 (1 \leq i \leq N) には、時間帯 i に勤務可能な候補者の人数 K_i と、その候補者の番号 C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} がスペース区切りで与えられる。K_i = 0 の場合、その行には K_i の値 0 のみが与えられる。
出力
候補者を割り当てた時間帯の数の最大値を 1 行で出力してください。
入力例 1
3 3 2 1 2 2 2 3 2 1 3
出力例 1
3
入力例 2
4 5 3 1 2 3 2 2 4 1 5 2 3 4
出力例 2
4
入力例 3
6 8 4 1 2 3 4 3 2 5 6 2 1 3 3 4 7 8 2 6 8 0
出力例 3
5
Score : 466 pts
Problem Statement
Takahashi has been put in charge of shift management as the store manager at his part-time job. Next week, there are N time slots for which he wants to assign staff.
There are M part-time staff candidates, numbered from 1 to M. For each time slot i (1 \leq i \leq N), K_i candidate numbers who are available to work during that time slot are given. Specifically, the candidates available for time slot i are numbered C_{i,1}, C_{i,2}, \ldots, C_{i,K_i}.
For each time slot, Takahashi chooses either to assign exactly one candidate who is available for that time slot, or to assign no one. However, the same candidate cannot be assigned to multiple time slots. That is, each candidate can be assigned to at most one time slot.
Find the maximum number of time slots to which a candidate can be assigned.
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 0 \leq K_i \leq M (1 \leq i \leq N)
- 1 \leq C_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq K_i)
- For each i, C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} are all distinct
- All input values are integers
Input
N M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
- The first line contains the number of time slots N and the number of candidates M, separated by a space.
- From the 2nd line to the (N + 1)-th line, information about the candidates available for each time slot is given.
- The (1 + i)-th line (1 \leq i \leq N) contains the number of candidates K_i available for time slot i, followed by their candidate numbers C_{i,1}, C_{i,2}, \ldots, C_{i,K_i}, separated by spaces. If K_i = 0, only the value 0 for K_i is given on that line.
Output
Print the maximum number of time slots to which a candidate can be assigned, on a single line.
Sample Input 1
3 3 2 1 2 2 2 3 2 1 3
Sample Output 1
3
Sample Input 2
4 5 3 1 2 3 2 2 4 1 5 2 3 4
Sample Output 2
4
Sample Input 3
6 8 4 1 2 3 4 3 2 5 6 2 1 3 3 4 7 8 2 6 8 0
Sample Output 3
5