#P7985. [USACO21DEC] Paired Up P

    ID: 9103 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPUSACO2021动态规划优化

[USACO21DEC] Paired Up P

Problem Description

There are a total of NN cows on a number line (1N50001 \le N \le 5000). Each cow is either a Holstein or a Guernsey. For cow ii, its breed is bi{H,G}b_i \in \{H,G\}, its position is xix_i (0xi1090 \le x_i \le 10^9), and its weight is yiy_i (1yi1051 \le y_i \le 10^5).

Following Farmer John’s signal, some cows will form pairs such that:

  • Each pair consists of one Holstein cow hh and one Guernsey cow gg whose positions differ by at most KK (1K1091 \le K \le 10^9); that is, xhxgK|x_h - x_g| \le K.
  • Each cow is either included in exactly one pair or belongs to no pair.
  • The pairing is maximal; that is, no two unpaired cows can form a pair.

You need to find the possible range of the total weight of unpaired cows. Specifically:

  • If T=1T = 1, compute the minimum possible total weight of unpaired cows.
  • If T=2T = 2, compute the maximum possible total weight of unpaired cows.

Input Format

The first line contains TT, NN, and KK.

The next NN lines describe the cows. Line ii contains bi,xi,yib_i, x_i, y_i. The input guarantees that 0x1<x2<<xN1090 \le x_1 < x_2 < \cdots < x_N \le 10^9.

Output Format

Output the minimum or maximum possible total weight of unpaired cows.

2 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
16
1 5 4
G 1 1
H 3 4
G 4 2
H 6 6
H 8 9
6
2 10 76
H 1 18
H 18 465
H 25 278
H 30 291
H 36 202
G 45 96
G 60 375
G 93 941
G 96 870
G 98 540
1893

Hint

[Sample Explanation 1]

Cows 22 and 33 can be paired because their distance is 11, which does not exceed K=4K = 4. This pairing is maximal because cow 11, the only remaining Guernsey, is at distance 55 from cow 44 and at distance 77 from cow 55, both greater than K=4K = 4. The total weight of unpaired cows is 1+6+9=161 + 6 + 9 = 16.

[Sample Explanation 2]

Cows 11 and 22 can be paired because their distance is 2K=42 \le K = 4, and cows 33 and 55 can be paired because their distance is 4K=44 \le K = 4. This pairing is maximal because only cow 44 remains. The total weight of unpaired cows is the weight of the only unpaired cow, which is 66.

[Sample Explanation 3]

The answer for this example is 18+465+870+540=189318 + 465 + 870 + 540 = 1893.

[Constraints]

  • Test cases 4-7 satisfy T=1T = 1.
  • Test cases 8-14 satisfy T=2T = 2 and N300N \le 300.
  • Test cases 15-22 satisfy T=2T = 2.

Note: The memory limit for this problem is 512MB\text{512MB}, which is twice the usual limit.

Translated by ChatGPT 5