Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
高橋くんは、カードゲーム「AtCoder Magics」のカードを N 枚持っています。i 番目のカードをカード i と呼ぶことにします。各カードには強さとコストのパラメーターがあり、カード i の強さは A_i で、コストは C_i です。
高橋くんは、弱いカードは要らないので捨てることにしました。具体的には、以下の操作をできなくなるまで繰り返します。
- 2 つのカード x, y であって、 A_x > A_y かつ C_x < C_y であるようなものを選ぶ。カード y を捨てる。
操作ができなくなったとき、捨てられなかったカードの集合は一意に定まることが証明できます。これを求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq 10^9
- A_1, A_2, \dots ,A_N は全て異なる
- C_1, C_2, \dots ,C_N は全て異なる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 C_1 A_2 C_2 \vdots A_N C_N
出力
捨てられなかったカードは m 枚あり、それらの番号が昇順に i_1, i_2, \dots ,i_m であったとする。このとき、以下の形式で出力せよ。
m i_1 i_2 \cdots i_m
入力例 1
3 2 4 1 1 3 2
出力例 1
2 2 3
カード 1, 3 に注目すると、 A_1 < A_3 かつ C_1 > C_3 なのでカード 1 を捨てることができます。
それ以上操作をすることはできません。このときカード 2, 3 が残っているので、これらを出力します。
入力例 2
5 1 1 10 2 100 3 1000 4 10000 5
出力例 2
5 1 2 3 4 5
この場合、どのカードも捨てることができません。
入力例 3
6 32 101 65 78 2 29 46 55 103 130 52 40
出力例 3
4 2 3 5 6
Score : 350 points
Problem Statement
Takahashi has N cards from the card game "AtCoder Magics." The i-th card will be called card i. Each card has two parameters: strength and cost. Card i has a strength of A_i and a cost of C_i.
He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed:
- Choose two cards x and y such that A_x > A_y and C_x < C_y. Discard card y.
It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq 10^9
- A_1, A_2, \dots ,A_N are all distinct.
- C_1, C_2, \dots ,C_N are all distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 C_1 A_2 C_2 \vdots A_N C_N
Output
Let there be m remaining cards, cards i_1, i_2, \dots, i_m, in ascending order. Print these in the following format:
m i_1 i_2 \cdots i_m
Sample Input 1
3 2 4 1 1 3 2
Sample Output 1
2 2 3
Focusing on cards 1 and 3, we have A_1 < A_3 and C_1 > C_3, so card 1 can be discarded.
No further operations can be performed. At this point, cards 2 and 3 remain, so print them.
Sample Input 2
5 1 1 10 2 100 3 1000 4 10000 5
Sample Output 2
5 1 2 3 4 5
In this case, no cards can be discarded.
Sample Input 3
6 32 101 65 78 2 29 46 55 103 130 52 40
Sample Output 3
4 2 3 5 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1, 2, \ldots, N の番号がついた N 人の人が二次元平面上におり、人 i は座標 (X_i,Y_i) で表される地点にいます。
人 1 がウイルスに感染しました。ウイルスに感染した人から距離が D 以内にいる人にウイルスはうつります。
ただし、距離はユークリッド距離、すなわち 2 点 (a_1, a_2) と (b_1, b_2) に対し、この 2 点間の距離が \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2} であるものとして定められています。
十分に時間が経過した、すなわち人 i がウイルスに感染しているならば 人 i との距離が D 以内にいるすべての人がウイルスに感染している状態になったときに、各 i について人 i がウイルスに感染しているか判定してください。
制約
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
N 行出力せよ。i 行目には、人 i がウイルスに感染しているならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 5 2 -1 3 1 8 8 0 5
出力例 1
Yes Yes No Yes
人 1 と人 2 の距離は \sqrt 5 であるため、人 2 はウイルスに感染します。
また、人 2 と人 4 の距離は 5 であるため、人 4 はウイルスに感染します。
人 3 は距離 5 以内に人がいないので、ウイルスに感染することはありません。
入力例 2
3 1 0 0 -1000 -1000 1000 1000
出力例 2
Yes No No
入力例 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
出力例 3
Yes No No Yes Yes Yes Yes Yes No
Score : 300 points
Problem Statement
There are N people numbered 1, 2, \ldots, N on a two-dimensional plane, and person i is at the point represented by the coordinates (X_i,Y_i).
Person 1 has been infected with a virus. The virus spreads to people within a distance of D from an infected person.
Here, the distance is defined as the Euclidean distance, that is, for two points (a_1, a_2) and (b_1, b_2), the distance between these two points is \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2}.
After a sufficient amount of time has passed, that is, when all people within a distance of D from person i are infected with the virus if person i is infected, determine whether person i is infected with the virus for each i.
Constraints
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print N lines. The i-th line should contain Yes if person i is infected with the virus, and No otherwise.
Sample Input 1
4 5 2 -1 3 1 8 8 0 5
Sample Output 1
Yes Yes No Yes
The distance between person 1 and person 2 is \sqrt 5, so person 2 gets infected with the virus.
Also, the distance between person 2 and person 4 is 5, so person 4 gets infected with the virus.
Person 3 has no one within a distance of 5, so they will not be infected with the virus.
Sample Input 2
3 1 0 0 -1000 -1000 1000 1000
Sample Output 2
Yes No No
Sample Input 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
Sample Output 3
Yes No No Yes Yes Yes Yes Yes No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君が選挙で戦っています。
選挙区は N 個あります。i 番目の選挙区には X_i + Y_i 人の有権者がいて、そのうち X_i 人が高橋派、Y_i 人が青木派です。(X_i + Y_i はすべて奇数です)
それぞれの区では、多数派がその区の Z_i 議席を全て獲得します。そして、N 個の選挙区全体として過半数の議席を獲得した方が選挙に勝利します。(\displaystyle \sum_{i=1}^N Z_i は奇数です)
高橋君が選挙で勝利するには最低で何人を青木派から高橋派に鞍替えさせる必要がありますか?
制約
- 1 \leq N \leq 100
- 0 \leq X_i, Y_i \leq 10^9
- X_i + Y_i は奇数
- 1 \leq Z_i
- \displaystyle \sum_{i=1}^N Z_i \leq 10^5
- \displaystyle \sum_{i=1}^N Z_i は奇数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
出力
答えを出力せよ。
入力例 1
1 3 8 1
出力例 1
3
選挙区が 1 個しかないので、1 番目の選挙区で議席を獲得した人が選挙に勝利します。
1 番目の選挙区の青木派 3 人を高橋派に鞍替えさせると、1 番目の選挙区にいる有権者のうち高橋派は 6 人、青木派は 5 人になり、高橋君は議席を獲得できます。
入力例 2
2 3 6 2 1 8 5
出力例 2
4
1 番目の選挙区の議席数よりも 2 番目の選挙区の議席数の方が多いため、高橋君が選挙に勝つには 2 番目の選挙区で高橋派を多数派にする必要があります。
2 番目の選挙区の青木派の 4 人を鞍替えさせると高橋君は 5 議席を獲得できます。このとき青木君の獲得する議席は 2 議席なので、高橋君は選挙に勝利できます。
入力例 3
3 3 4 2 1 2 3 7 2 6
出力例 3
0
青木派から高橋派に鞍替えする人が 0 人でも高橋君が選挙で勝つ場合は 0 人が答えになります。
入力例 4
10 1878 2089 16 1982 1769 13 2148 1601 14 2189 2362 15 2268 2279 16 2394 2841 18 2926 2971 20 3091 2146 20 3878 4685 38 4504 4617 29
出力例 4
86
Score : 400 points
Problem Statement
Takahashi and Aoki are competing in an election.
There are N electoral districts. The i-th district has X_i + Y_i voters, of which X_i are for Takahashi and Y_i are for Aoki. (X_i + Y_i is always an odd number.)
In each district, the majority party wins all Z_i seats in that district. Then, whoever wins the majority of seats in the N districts as a whole wins the election. (\displaystyle \sum_{i=1}^N Z_i is odd.)
At least how many voters must switch from Aoki to Takahashi for Takahashi to win the election?
Constraints
- 1 \leq N \leq 100
- 0 \leq X_i, Y_i \leq 10^9
- X_i + Y_i is odd.
- 1 \leq Z_i
- \displaystyle \sum_{i=1}^N Z_i \leq 10^5
- \displaystyle \sum_{i=1}^N Z_i is odd.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 Z_1 X_2 Y_2 Z_2 \vdots X_N Y_N Z_N
Output
Print the answer.
Sample Input 1
1 3 8 1
Sample Output 1
3
Since there is only one district, whoever wins the seat in that district wins the election.
If three voters for Aoki in the district switch to Takahashi, there will be six voters for Takahashi and five for Aoki, and Takahashi will win the seat.
Sample Input 2
2 3 6 2 1 8 5
Sample Output 2
4
Since there are more seats in the second district than in the first district, Takahashi must win a majority in the second district to win the election.
If four voters for Aoki in the second district switch sides, Takahashi will win five seats. In this case, Aoki will win two seats, so Takahashi will win the election.
Sample Input 3
3 3 4 2 1 2 3 7 2 6
Sample Output 3
0
If Takahashi will win the election even if zero voters switch sides, the answer is 0.
Sample Input 4
10 1878 2089 16 1982 1769 13 2148 1601 14 2189 2362 15 2268 2279 16 2394 2841 18 2926 2971 20 3091 2146 20 3878 4685 38 4504 4617 29
Sample Output 4
86
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の重み付き単純連結無向グラフと正整数 K が与えられます。
辺 i\ (1\leq i\leq M) は頂点 u _ i と頂点 v _ i を結んでおり、重みは w _ i です。
このグラフの全域木 T に対して、T のコストを T に含まれる辺の重みの総和を K で割ったあまりで定めます。 このグラフの全域木のコストの最小値を求めてください。
制約
- 2\leq N\leq8
- N-1\leq M\leq\dfrac{N(N-1)}2
- 1\leq K\leq10^{15}
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
- 0\leq w _ i\lt K\ (1\leq i\leq M)
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u _ 1 v _ 1 w _ 1 u _ 2 v _ 2 w _ 2 \vdots u _ M v _ M w _ M
出力
答えを出力せよ。
入力例 1
5 6 328 1 2 99 1 3 102 2 3 86 2 4 94 2 5 95 3 4 81
出力例 1
33
与えられるグラフは次のようになります。

辺 1,3,5,6 の 4 本を含む全域木のコストは (99+86+81+95)\bmod{328}=361\bmod{328}=33 となります。 このグラフの全域木のコストはすべて 33 以上であるため、33 を出力してください。
入力例 2
6 5 998244353 1 2 337361568 1 6 450343304 2 3 61477244 2 5 745383438 4 5 727360840
出力例 2
325437688
このグラフのただ一つの全域木のコスト 325437688 を出力してください。
入力例 3
8 28 936294041850197 1 2 473294720906780 1 3 743030800139244 1 4 709363019414774 1 5 383643612490312 1 6 557102781022861 1 7 623179288538138 1 8 739618599410809 2 3 857687812294404 2 4 893923168139714 2 5 581822471860662 2 6 740549363586558 2 7 307226438833222 2 8 447399029952998 3 4 636318083622768 3 5 44548707643622 3 6 307262781240755 3 7 12070267388230 3 8 700247263184082 4 5 560567890325333 4 6 704726113717147 4 7 588263818615687 4 8 549007536393172 5 6 779230871080408 5 7 825982583786498 5 8 713928998174272 6 7 751331074538826 6 8 449873635430228 7 8 11298381761479
出力例 3
11360716373
入力や答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
Score : 475 points
Problem Statement
You are given a weighted simple connected undirected graph with N vertices and M edges, where vertices are numbered 1 to N, and edges are numbered 1 to M. Additionally, a positive integer K is given.
Edge i\ (1\leq i\leq M) connects vertices u_i and v_i and has a weight of w_i.
For a spanning tree T of this graph, the cost of T is defined as the sum, modulo K, of the weights of the edges in T. Find the minimum cost of a spanning tree of this graph.
Constraints
- 2\leq N\leq8
- N-1\leq M\leq\dfrac{N(N-1)}2
- 1\leq K\leq10^{15}
- 1\leq u_i\lt v_i\leq N\ (1\leq i\leq M)
- 0\leq w_i\lt K\ (1\leq i\leq M)
- The given graph is simple and connected.
- 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_M v_M w_M
Output
Print the answer.
Sample Input 1
5 6 328 1 2 99 1 3 102 2 3 86 2 4 94 2 5 95 3 4 81
Sample Output 1
33
The given graph is shown below:

The cost of the spanning tree containing edges 1,3,5,6 is (99+86+81+95)\bmod{328}=361\bmod{328}=33. The cost of every spanning tree of this graph is at least 33, so print 33.
Sample Input 2
6 5 998244353 1 2 337361568 1 6 450343304 2 3 61477244 2 5 745383438 4 5 727360840
Sample Output 2
325437688
Print the cost of the only spanning tree of this graph, which is 325437688.
Sample Input 3
8 28 936294041850197 1 2 473294720906780 1 3 743030800139244 1 4 709363019414774 1 5 383643612490312 1 6 557102781022861 1 7 623179288538138 1 8 739618599410809 2 3 857687812294404 2 4 893923168139714 2 5 581822471860662 2 6 740549363586558 2 7 307226438833222 2 8 447399029952998 3 4 636318083622768 3 5 44548707643622 3 6 307262781240755 3 7 12070267388230 3 8 700247263184082 4 5 560567890325333 4 6 704726113717147 4 7 588263818615687 4 8 549007536393172 5 6 779230871080408 5 7 825982583786498 5 8 713928998174272 6 7 751331074538826 6 8 449873635430228 7 8 11298381761479
Sample Output 3
11360716373
Note that the input and the answer may not fit into a 32\operatorname{bit} integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
N 人の人がおり、人にはそれぞれ 1, 2, \ldots, N の番号が付けられています。
N 人が競争を行い、順位が付きました。この順位に対して以下の情報が与えられています。
- それぞれの人に対して付けられた順位は相異なる
- 各 1 \leq i \leq M について人 A_i の順位を x、人 B_i の順位を y とすると、x - y = C_i である
ただし、この問題では与えられた情報に矛盾しないような順位付けが 1 つ以上存在するような入力のみが与えられます。
N 個のクエリの答えを求めてください。i 番目のクエリの答えは以下により定まる整数です。
- 人 i の順位が一意に定まるならば、その値を答えとする。そうでない場合、答えは -1 である。
制約
- 2 \leq N \leq 16
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i, B_i \leq N
- 1 \leq C_i \leq N - 1
- (A_i, B_i) \neq (A_j, B_j) (i \neq j)
- 与えられた情報に矛盾しない順位付けが 1 つ以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
1, 2, \ldots ,N 番目のクエリに対する答えをこの順に空白区切りで出力せよ。
入力例 1
5 2 2 3 3 5 4 3
出力例 1
3 -1 -1 -1 -1
人 i の順位を X_i とおくと、(X_1, X_2, X_3, X_4, X_5) は (3, 4, 1, 2, 5), (3, 5, 2, 1, 4) のいずれかです。
したがって、1 番目のクエリに対する答えは 3、2, 3, 4, 5 番目のクエリに対する答えは -1 となります。
入力例 2
3 0
出力例 2
-1 -1 -1
入力例 3
8 5 6 7 3 8 1 7 4 5 1 7 2 1 6 2 4
出力例 3
1 -1 -1 -1 -1 -1 -1 8
Score: 525 points
Problem Statement
There are N people, numbered 1 to N.
A competition was held among these N people, and they were ranked accordingly. The following information is given about their ranks:
- Each person has a unique rank.
- For each 1 \leq i \leq M, if person A_i is ranked x-th and person B_i is ranked y-th, then x - y = C_i.
The given input guarantees that there is at least one possible ranking that does not contradict the given information.
Answer N queries. The answer to the i-th query is an integer determined as follows:
- If the rank of person i can be uniquely determined, return that rank. Otherwise, return -1.
Constraints
- 2 \leq N \leq 16
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i, B_i \leq N
- 1 \leq C_i \leq N - 1
- (A_i, B_i) \neq (A_j, B_j) (i \neq j)
- There is at least one possible ranking that does not contradict the given information.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the answers to the 1-st, 2-nd, \ldots, N-th queries in this order, separated by spaces.
Sample Input 1
5 2 2 3 3 5 4 3
Sample Output 1
3 -1 -1 -1 -1
Let X_i be the rank of person i. Then, (X_1, X_2, X_3, X_4, X_5) could be (3, 4, 1, 2, 5) or (3, 5, 2, 1, 4).
Therefore, the answer to the 1-st query is 3, and the answers to the 2-nd, 3-rd, 4-th, and 5-th queries are -1.
Sample Input 2
3 0
Sample Output 2
-1 -1 -1
Sample Input 3
8 5 6 7 3 8 1 7 4 5 1 7 2 1 6 2 4
Sample Output 3
1 -1 -1 -1 -1 -1 -1 8