#P6851. onu
onu
Description
To make the game a bit more fun, Xiao C and Xiao D each buy candies as chips.
The rules of onu are as follows:
The game has a total of rounds and is played by two players, one going first and one going second. Here, we assume the first player is Xiao C, and the second player is Xiao D.
At the very beginning, Xiao C gets cards, each with a suit and a rank. Xiao D gets cards.
At the start of each round, Xiao C plays one card and places it on the table to show Xiao D.
After that, Xiao D must follow suit, i.e., play one card from his hand whose suit is the same as the suit of Xiao C’s card. If Xiao D has no valid card, or he chooses to give up (that is, he may choose whether to play a card in the current round), then Xiao C’s card is discarded and the round is skipped; Xiao D is considered to lose the round.
After Xiao D plays a valid card, they compare ranks. If the rank of Xiao D’s card is greater than or equal to the rank of Xiao C’s card, then Xiao D wins; otherwise, Xiao D loses. It is easy to see that there will be no tie.
Finally, the winner takes candies from the loser. Both players discard the cards they played, and then each buys a number of candies equal to the rank of the card they played. For example, if Xiao C and Xiao D play ranks and respectively, then Xiao C buys candies and Xiao D buys candies.
To avoid harming their friendship and to prevent one side from winning all of the other’s candies, they have already agreed when buying candies at the beginning that .
Now, using some mysterious method, Xiao D knows all the cards Xiao C will play in these rounds. He wants to maximize the number of candies he has after the rounds are finished. Can you help him find the best strategy?
Input Format
The first line contains four positive integers , with meanings as described above.
In the next lines, the -th line contains two positive integers , indicating the suit and rank of the -th card that Xiao D initially has.
In the next lines, the -th line contains two positive integers , indicating the suit and rank of the card that Xiao C will play in round .
Note that there may be cards with both the same suit and the same rank.
Output Format
Output a total of lines.
The first line outputs a positive integer, representing the maximum number of candies Xiao D can have left under an optimal strategy.
In the next lines, the -th line outputs one integer, representing the index of the card Xiao D plays in round . If Xiao D skips the round, output -1.
This problem uses Special Judge. If there are multiple optimal strategies, output any one of them.
3 3 1 4
3 5
1 2
2 6
1 6
3 5
1 4
10
2
1
-1
1 2 1 5
1 5
1 8
1 4
10
-1
1
Hint
"Sample 1 Explanation"
Use to represent a card with suit and rank .
Initially, Xiao D has candies. Xiao C will play three cards in order: .
One optimal strategy is:
Round 1: Xiao C plays the first card . Xiao D plays the second card . Xiao D loses, gets candy taken away, and buys candies. Now he has candies.
Round 2: Xiao C plays . Xiao D plays . Since the rank is greater than or equal to Xiao C’s card, Xiao D wins, takes candy, and buys candies. Now he has candies.
Round 3: Xiao C plays . Since Xiao D already played the second card in round 1, he has no card to play, so he outputs and is judged to lose. He gets candy taken away. Now he has candies.
"Sample 2 Explanation"
Initially there are candies.
In round 1, Xiao C plays . Xiao D chooses to give up and loses, so he has candies left.
In round 2, Xiao C plays . Xiao D plays and wins, gaining candies, so in the end Xiao D has candies.
"Special Judge Notes"
Please read the output format carefully.
Each test point is either points or full points. If your output has any of the following issues, you will get points:
- The output format is incorrect, such as missing correct line breaks, outputting strange characters, etc.
- The optimal candy count you output is different from the standard answer.
- The card-playing strategy is invalid, i.e., you play a card that has already been discarded, or you play a card whose suit is different from the suit of the card Xiao C played.
- After playing according to your strategy, Xiao D’s remaining number of candies does not match the number you output on the first line.
Constraints
This problem uses bundled tests.
- Subtask 1 (10 points): .
- Subtask 2 (30 points): .
- Subtask 3 (20 points): .
- Subtask 4 (20 points): .
- Subtask 5 (20 points): no special restrictions.
All testdata guarantees , , .
Translated by ChatGPT 5
京公网安备 11011102002149号