From http://blog.csdn.net/shuangde800
Question: Click to open the link
Question
Petra and Jan divide each person into N sweets, and each person can take one candy at a time.
Each candy has two values X and Y. If Petra gets the value x, Jan gets the value Y.
Petra selects the largest (x largest) value for itself each time. If multiple X values are the same, select the smallest value of Y.
The strategy selected by Jan is to maximize the final total value, and on this premise, the Petra value should be as big as possible.
Q: What is the value they ultimately get?
Ideas
This question is clever.
First, we only consider the situation where Petra gets sugar. His strategy is greedy. After sorting, we can know that he must choose from the order.
Example:
Jan
4 1
3 1
2 1
1 1
1 2
1 3
1 4
This example has been sorted according to the greedy policy of Petra. The first is obtained by Jan, and the second is certainly taken by Petra.
Next, if Jan chooses 3rd, Petra will take 4th. If jam chooses any one except 3rd, Petra will take 3rd.
Therefore, every time Jan chooses a policy, do you want to "grab" The One Petra wants next time "!
It can be found that the first time is Jan (if it is Petra for the first time, it is counted from the second time)
The first one, Jan can grab a maximum of one
The first two can grab a maximum of one (if you get 1st, 2nd will be taken for Petra. If you don't get 1st, the 1st will be taken by Petra, why can't Jan take two of them)
The first 3, which can be up to 2
The first four, up to two
The first five, up to three
... (Omitted below)
The rule is: the first I can grab a maximum of (I + 1)/2
Therefore, we can use the state f (I, j) to represent the first I. When we grab J, we have the maximum value.
F (I, j) = max {f (I-1, J), F (I-1, J-1) + Y (I) | when F (I-1, J-1) status is available );
In addition, there must be a limit on the question: in the case of Jan's greatest value, Petra has the greatest value.
So sum = X1 + X2 + X3 +... XN, sum is the sum of the values of all sweets for Petra.
When Jan grabs one, the sum of Petra reduces XI, and we need to minimize the sum of all the reduced XI,
In this way, the X value of an item can be viewed as the cost, and the Y value as the value. The goal is to minimize the cost when Jan obtains the maximum value.
Then we can maintain an array cost (I, j ).
Code