A - Consecutive High-Score Segments

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は学校のテスト結果を分析しています。

クラスには N 人の生徒がおり、出席番号 1, 2, \ldots, N が付けられています。出席番号 i の生徒のテストの点数は A_i 点です。

高橋君は、基準点 K 点以上の点数を取った生徒を「合格者」と呼ぶことにしました。合格者たちをグループに分け、グループごとに 1 回ずつ表彰式を開きます。

グループ分けは次のルールで行います。合格者の出席番号の集合を、出席番号が連続する区間(これ以上両端に合格者を含めて広げることができない、極大な連続区間)に分割し、それぞれの区間を 1 つのグループとします。この分割の仕方は一意に定まります。

例えば、N = 10 で合格者の出席番号が 2, 3, 4, 7, 9, 10 のとき、グループは \{2, 3, 4\}, \{7\}, \{9, 10\} の 3 つに分かれます。

グループの個数を求めてください。なお、合格者がいない場合はグループの個数は 0 です。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 100
  • 0 \leq A_i \leq 100
  • 入力はすべて整数

入力

N K
A_1 A_2 \cdots A_N
  • 1 行目には、生徒の人数を表す整数 N と、合格の基準点を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各生徒のテストの点数を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

グループの個数を 1 行で出力せよ。


入力例 1

5 60
72 45 80 85 50

出力例 1

2

入力例 2

10 70
65 75 80 90 60 55 85 70 72 40

出力例 2

2

入力例 3

15 50
48 52 55 60 45 30 70 80 75 65 40 90 35 85 88

出力例 3

4

Score : 233 pts

Problem Statement

Takahashi is analyzing the test results at his school.

There are N students in the class, assigned attendance numbers 1, 2, \ldots, N. The test score of the student with attendance number i is A_i points.

Takahashi decided to call students who scored at least K points (the passing threshold) "passers." The passers are divided into groups, and one award ceremony is held for each group.

The grouping is done according to the following rule. The set of attendance numbers of passers is partitioned into intervals of consecutive attendance numbers (maximal consecutive intervals that cannot be extended further by including additional passers at either end), and each interval forms one group. This partition is uniquely determined.

For example, when N = 10 and the attendance numbers of the passers are 2, 3, 4, 7, 9, 10, the groups are \{2, 3, 4\}, \{7\}, \{9, 10\}, resulting in 3 groups.

Find the number of groups. If there are no passers, the number of groups is 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 100
  • 0 \leq A_i \leq 100
  • All inputs are integers

Input

N K
A_1 A_2 \cdots A_N
  • The first line contains an integer N representing the number of students and an integer K representing the passing threshold, separated by a space.
  • The second line contains N integers A_1, A_2, \ldots, A_N representing each student's test score, separated by spaces.

Output

Print the number of groups in one line.


Sample Input 1

5 60
72 45 80 85 50

Sample Output 1

2

Sample Input 2

10 70
65 75 80 90 60 55 85 70 72 40

Sample Output 2

2

Sample Input 3

15 50
48 52 55 60 45 30 70 80 75 65 40 90 35 85 88

Sample Output 3

4
B - Card Taking Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君と青木君はカード取りゲームで対戦しています。

テーブルの上には N 枚のカードが並べられています。各カードには 1 から N までの番号が付けられており、カード i には正の整数 A_i が書かれています。

ゲームは以下のルールで進行します:

  1. 両者の得点は最初 0 点です。
  2. 高橋君が先手で、高橋君と青木君は交互にターンを行います。
  3. 自分のターンでは、テーブルに残っているカードの中からちょうど 1 枚を選んで取らなければなりません(パスはできません)。取ったカードはテーブルから除かれ、そのカードに書かれた整数がそのプレイヤーの得点に加算されます。
  4. すべてのカードが取られたらゲーム終了です。

以上のルールにより、高橋君は \lceil N/2 \rceil 枚、青木君は \lfloor N/2 \rfloor 枚のカードを取ることになります。

高橋君は(高橋君の得点)-(青木君の得点)を最大化するように、青木君は(高橋君の得点)-(青木君の得点)を最小化するように、それぞれ最適にプレイします。両者が最適にプレイしたときの(高橋君の得点)-(青木君の得点)を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、カードの枚数を表す整数 N が与えられる。
  • 2 行目には、カード i (i = 1, 2, \ldots, N) に書かれた整数 A_i が N 個、スペース区切りで与えられる。

出力

両者が最適にプレイしたときの(高橋君の得点)-(青木君の得点)を整数として 1 行で出力せよ。


入力例 1

3
10 20 30

出力例 1

20

入力例 2

5
1 2 3 4 5

出力例 2

3

入力例 3

8
100 50 80 120 90 30 70 60

出力例 3

60

Score : 300 pts

Problem Statement

Takahashi and Aoki are playing a card taking game against each other.

There are N cards laid out on the table. Each card is numbered from 1 to N, and card i has a positive integer A_i written on it.

The game proceeds according to the following rules:

  1. Both players start with a score of 0.
  2. Takahashi goes first, and Takahashi and Aoki take turns alternately.
  3. On their turn, a player must choose exactly 1 card from the cards remaining on the table and take it (passing is not allowed). The taken card is removed from the table, and the integer written on that card is added to that player's score.
  4. The game ends when all cards have been taken.

According to the above rules, Takahashi will take \lceil N/2 \rceil cards and Aoki will take \lfloor N/2 \rfloor cards.

Takahashi plays optimally to maximize (Takahashi's score) - (Aoki's score), and Aoki plays optimally to minimize (Takahashi's score) - (Aoki's score). Find (Takahashi's score) - (Aoki's score) when both players play optimally.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of cards.
  • The second line contains N integers A_i (i = 1, 2, \ldots, N) written on card i, separated by spaces.

Output

Output in a single line, as an integer, the value of (Takahashi's score) - (Aoki's score) when both players play optimally.


Sample Input 1

3
10 20 30

Sample Output 1

20

Sample Input 2

5
1 2 3 4 5

Sample Output 2

3

Sample Input 3

8
100 50 80 120 90 30 70 60

Sample Output 3

60
C - Summer Homework Plan

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は夏休みの最終日を迎えました。夏休みが終わるまで残り M 分しかありません。

高橋君には N 個の宿題が残っています。各宿題 i(1 \leq i \leq N)には、完了したときに先生からもらえる評価点 R_i と、完了するのに必要な時間 T_i 分が決まっています。

高橋君はこの M 分の間に、いくつかの宿題を選んで取り組むことができます。ただし、以下の条件があります。

  • 各宿題は、完了するかまったく手をつけないかのいずれかです。途中まで取り組んで部分的に完了させることはできません。取り組む場合は T_i 分をかけて完了させる必要があります。
  • 同じ宿題に複数回取り組むことはできません。すなわち、各宿題を完了できるのは最大 1 回です。
  • 選んだ宿題の必要時間の合計は M 分以下でなければなりません。

高橋君は、これらの条件のもとで、完了した宿題の評価点の合計を最大化したいと考えています。

高橋君が得られる評価点の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq T_i \leq M
  • 入力はすべて整数

入力

N M
R_1 T_1
R_2 T_2
\vdots
R_N T_N
  • 1 行目には、宿題の数 N と、夏休み終了までの残り時間(分) M が空白区切りで与えられる。
  • 続く N 行の i 行目(1 \leq i \leq N)には、宿題 i の評価点 R_i と必要時間 T_i(分)が空白区切りで与えられる。

出力

高橋君が得られる評価点の合計の最大値を 1 行で出力してください。


入力例 1

3 10
6 3
10 5
4 4

出力例 1

16

入力例 2

5 15
100 8
60 5
50 4
40 3
30 2

出力例 2

190

入力例 3

8 1000
1000000000 500
800000000 400
600000000 300
500000000 250
400000000 200
300000000 150
200000000 100
100000000 50

出力例 3

2000000000

Score : 366 pts

Problem Statement

Takahashi has reached the last day of summer vacation. There are only M minutes remaining until summer vacation ends.

Takahashi has N homework assignments left. Each homework assignment i (1 \leq i \leq N) has a fixed evaluation score R_i received from the teacher upon completion, and a required time of T_i minutes to complete.

During these M minutes, Takahashi can choose some homework assignments to work on. However, the following conditions apply:

  • Each homework assignment is either completed entirely or not attempted at all. It is not possible to partially complete an assignment. If he works on it, he must spend T_i minutes to complete it.
  • He cannot work on the same homework assignment multiple times. That is, each homework assignment can be completed at most once.
  • The total required time of the chosen homework assignments must be at most M minutes.

Under these conditions, Takahashi wants to maximize the total evaluation score of completed homework assignments.

Find the maximum total evaluation score that Takahashi can obtain.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq T_i \leq M
  • All inputs are integers

Input

N M
R_1 T_1
R_2 T_2
\vdots
R_N T_N
  • The first line contains the number of homework assignments N and the remaining time until the end of summer vacation (in minutes) M, separated by a space.
  • The i-th of the following N lines (1 \leq i \leq N) contains the evaluation score R_i and the required time T_i (in minutes) of homework assignment i, separated by a space.

Output

Print the maximum total evaluation score that Takahashi can obtain, on a single line.


Sample Input 1

3 10
6 3
10 5
4 4

Sample Output 1

16

Sample Input 2

5 15
100 8
60 5
50 4
40 3
30 2

Sample Output 2

190

Sample Input 3

8 1000
1000000000 500
800000000 400
600000000 300
500000000 250
400000000 200
300000000 150
200000000 100
100000000 50

Sample Output 3

2000000000
D - Bridges Connecting Remote Islands

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は、N 個の離島からなる諸島の開発を担当する建設会社の社長です。それぞれの島には 1 から N までの番号が付けられています。

高橋君は、住民がどの島からどの島へも橋を経由して行き来できるようにするプロジェクトを任されました。現在、M 個の橋の建設案があり、i 番目の案 (1 \leq i \leq M) では、異なる 2 つの島 U_i と島 V_i を双方向に結ぶ橋を建設できます。この橋を建設するには C_i 万円のコストがかかります。なお、同じ 2 つの島を結ぶ建設案が複数存在することもあります。

高橋君は、これらの M 個の建設案の中からいくつかを選んで(各建設案は高々 1 回しか選べません)橋を建設し、すべての島を連結にしたいと考えています。すべての島が連結であるとは、どの 2 つの島の間についても、建設した橋をいくつか順に経由して一方からもう一方へ移動できることを意味します。特に、N = 1 の場合は橋を建設しなくても連結であり、最小総コストは 0 です。

会社の予算は限られているため、目的を達成するための最小の総コストを求める必要があります。ここで、総コストとは選んだ建設案のコストの合計を指します。

すべての島を連結にすることが可能な場合は、必要な最小総コスト(万円)を整数で出力してください。不可能な場合は -1 を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • 入力はすべて整数である

入力

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • 1 行目には、島の数 N と橋の建設案の数 M が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 番目の建設案で結ぶ 2 つの島の番号 U_i, V_i およびコスト C_i がスペース区切りで与えられる。

出力

すべての島を連結にするために必要な最小総コスト(万円)を整数で 1 行に出力せよ。連結にすることが不可能な場合は -1 を出力せよ。なお、答えが 32 ビット整数の範囲に収まらない場合があることに注意せよ。


入力例 1

4 5
1 2 3
1 3 5
2 3 4
2 4 6
3 4 2

出力例 1

9

入力例 2

3 1
1 2 100

出力例 2

-1

入力例 3

6 9
1 2 10
1 3 15
2 3 5
2 4 20
3 4 8
4 5 12
4 6 18
5 6 7
1 6 30

出力例 3

42

Score : 400 pts

Problem Statement

Takahashi is the president of a construction company in charge of developing an archipelago consisting of N remote islands. Each island is numbered from 1 to N.

Takahashi has been assigned a project to enable residents to travel between any pair of islands via bridges. Currently, there are M bridge construction proposals. The i-th proposal (1 \leq i \leq M) allows building a bridge that bidirectionally connects two distinct islands U_i and V_i. The cost to build this bridge is C_i ten-thousand yen. Note that there may be multiple proposals connecting the same pair of islands.

Takahashi wants to select some of these M proposals (each proposal can be selected at most once) to build bridges and make all islands connected. All islands being connected means that for any two islands, it is possible to travel from one to the other by traversing some sequence of built bridges. In particular, when N = 1, the islands are connected without building any bridges, and the minimum total cost is 0.

Since the company's budget is limited, he needs to find the minimum total cost to achieve this goal. Here, the total cost refers to the sum of the costs of the selected proposals.

If it is possible to connect all islands, output the minimum total cost (in ten-thousand yen) as an integer. If it is impossible, output -1.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • All input values are integers

Input

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • The first line contains the number of islands N and the number of bridge construction proposals M, separated by a space.
  • The following M lines each describe a proposal: the i-th line (1 \leq i \leq M) contains the numbers of the two islands U_i, V_i connected by the i-th proposal and the cost C_i, separated by spaces.

Output

Output on a single line the minimum total cost (in ten-thousand yen) as an integer required to connect all islands. If it is impossible to connect all islands, output -1. Note that the answer may not fit within the range of a 32-bit integer.


Sample Input 1

4 5
1 2 3
1 3 5
2 3 4
2 4 6
3 4 2

Sample Output 1

9

Sample Input 2

3 1
1 2 100

Sample Output 2

-1

Sample Input 3

6 9
1 2 10
1 3 15
2 3 5
2 4 20
3 4 8
4 5 12
4 6 18
5 6 7
1 6 30

Sample Output 3

42
E - Reduce Inversions with Adjacent Swaps

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は、N 枚のカードを横一列に並べています。カードにはそれぞれ 1 から N までの番号が書かれており、現在は左から順に P_1, P_2, \ldots, P_N の順番で並んでいます。

高橋君は、カードが番号の昇順(左から 1, 2, \ldots, N )に並んでいる状態を目指しています。並びの乱れ具合を測る指標として、転倒数を用います。

カード列 Q_1, Q_2, \ldots, Q_N の転倒数とは、1 \leq i < j \leq N を満たすすべての組 (i, j) のうち、Q_i > Q_j となる組の個数です。

高橋君は、隣り合う 2 枚のカードを選んで、その 2 枚の位置を入れ替える操作を行うことができます。この操作を最大 K 回まで行うことができ、各回で入れ替える隣り合うペアは自由に選べます(同じペアを複数回選んでもかまいません)。

高橋君が転倒数を最小にするように最適に操作を行ったとき、操作後のカード列の転倒数として達成できる最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^{18}
  • (P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) の順列である
  • 入力はすべて整数である

入力

N K
P_1 P_2 \ldots P_N
  • 1 行目には、カードの枚数を表す整数 N と、操作の最大回数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、現在のカードの並び順を表す整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。これは 1 から N までの順列である。

出力

高橋君が最大 K 回の隣接スワップ操作を行った後に達成できる、カード列の転倒数の最小値を 1 行で出力せよ。


入力例 1

5 2
3 1 4 5 2

出力例 1

2

入力例 2

3 100
3 2 1

出力例 2

0

入力例 3

10 5
5 3 8 1 7 2 10 4 6 9

出力例 3

12

Score : 466 pts

Problem Statement

Takahashi has N cards arranged in a horizontal row. Each card has a number from 1 to N written on it, and they are currently arranged in the order P_1, P_2, \ldots, P_N from left to right.

Takahashi aims to arrange the cards in ascending order of their numbers (i.e., 1, 2, \ldots, N from left to right). As a measure of how disordered the arrangement is, he uses the inversion count.

The inversion count of a card sequence Q_1, Q_2, \ldots, Q_N is the number of pairs (i, j) satisfying 1 \leq i < j \leq N such that Q_i > Q_j.

Takahashi can perform an operation where he selects two adjacent cards and swaps their positions. He can perform this operation at most K times, and in each operation, he is free to choose any adjacent pair to swap (the same pair may be chosen multiple times).

When Takahashi performs the operations optimally to minimize the inversion count, find the minimum inversion count of the card sequence that can be achieved after the operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^{18}
  • (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N)
  • All inputs are integers

Input

N K
P_1 P_2 \ldots P_N
  • The first line contains an integer N representing the number of cards and an integer K representing the maximum number of operations, separated by a space.
  • The second line contains integers P_1, P_2, \ldots, P_N representing the current order of the cards, separated by spaces. This is a permutation of 1 through N.

Output

Print in one line the minimum inversion count of the card sequence that Takahashi can achieve after performing at most K adjacent swap operations.


Sample Input 1

5 2
3 1 4 5 2

Sample Output 1

2

Sample Input 2

3 100
3 2 1

Sample Output 2

0

Sample Input 3

10 5
5 3 8 1 7 2 10 4 6 9

Sample Output 3

12