#P12026. [USACO25OPEN] Compatible Pairs S

    ID: 11947 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>图论贪心USACO2025图论建模拓扑排序

[USACO25OPEN] Compatible Pairs S

题目描述

Deep in the countryside, Farmer John’s cows aren’t just ordinary farm animals—they are part of a clandestine bovine intelligence network. Each cow carries an ID number, carefully assigned by the elite cow cryptographers. However, due to Farmer John's rather haphazard tagging system, some cows ended up with the same ID.

Farmer John noted that there are NN (1N21051\le N\le 2\cdot 10^5) unique ID numbers, and for each unique ID did_i (0di1090\le d_i\le 10^9), there are nin_i (1ni1091\le n_i\le 10^9) cows who shared it.

The cows can only communicate in pairs, and their secret encryption method has one strict rule: two cows can only exchange information if they are not the same cow and the sum of their ID numbers equals either AA or BB (0AB21090\le A\le B\le 2\cdot 10^9). A cow can only engage in one conversation at a time (i.e., no cow can be part of more than one pair).

Farmer John would like to maximize the number of disjoint communication pairs to ensure the best information flow. Can you determine the largest number of conversations that can happen at once?

输入格式

The first line contains NN, AA, BB.

The next NN lines each contain nin_i and did_i. No two did_i are the same.

输出格式

The maximum number of disjoint communicating pairs that can be formed at the same time.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

4 4 5
17 2
100 0
10 1
200 4
118
4 4 5
100 0
10 1
100 3
20 4
30

提示

For Sample 1:

A cow with an ID of 00 can communicate with another cow with an ID of 44 because the sum of their IDs is 44. Since there are a total of 100100 cows of ID 00 and 200200 cows of ID 44, there can be up to 100100 communicating pairs with this combination of IDs.

A cow with an ID of 44 can also communicate with another cow with an ID of 11 (sum to 55). There are 1010 cows of ID 11 and 100100 remaining unpaired cows of ID 44, allowing for another 1010 pairs.

Finally, a cow with an ID of 22 can communicate with another cow of the same ID. Since there are a total of 1717 cows of ID 22, there can be up to 88 more pairs.

In total, there are 100+10+8=118100+10+8=118 communicating pairs. It can be shown that this is the maximum possible number of pairs.

For Sample 2:

Pairing IDs 00 and 44 makes 2020 pairs, while pairing IDs 11 and 33 makes 1010 pairs. It can be shown that this is the optimal pairing, resulting in a total of 3030 pairs.

SCORING:

  • Inputs 3-4: A=BA=B
  • Inputs 5-7: N1000N\le 1000
  • Inputs 8-12: No additional constraints