UVA 12260 - Free Goodies(dp+贪心)
2014-03-01 19:04
459 查看
Petra and Jan have just received a box full of free goodies, and want to divide the goodies between them. However, it is not easy to do this fairly, since they both value different goodies differently.
To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie.
Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks
the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.)
Jan's strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible.
You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with?
One line with an integer n (1 ≤ n ≤ 1 000): the number of goodies.
One line with a string, either "Petra" or "Jan": the person that chooses first.
n lines with two integers pi and ji (0 ≤ pi,ji ≤ 1 000) each: the values that Petra and Jan assign to the i-th goodie, respectively.
One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations.
题意:Petra和Jan两个人,有一堆糖果,其中一个人先选,每个糖果有p,j值,代表Petra和Jan取得糖果后的开心值。Petra取糖果的策略是选择P尽量大,J尽量小的糖果,Jan的策略是保证自己最终快乐值尽量大并且Petra的快乐值也尽量大,问最终两个人的快乐值分别是多少。
思路:Petra的策略满足贪心,所以先把糖果按P在按J排序,然后去取,就看Jan会取哪些糖果了,那么每个糖果就可以看成是Jan取不取,但是要注意,由于Petra是一定会往后一个取,所以Jan取糖果的时候是有一定的限制的,该限制为:假如都是Jan先取,1个糖果,Jan可以拿到,2个糖果,Jan可以拿其中一个,3个糖果Jan可以拿其中两个,4个糖果也是其中两个,以此类推,Jan能拿的糖果数为(i + 1)/2。因此dp[i][j]表示Jan在前i个糖果中拿了j个,j <= (i + 1)/2.
状态转移方程为:如果不取 dp[i][j] = dp[i - 1][j] 如果取dp[i][j] = dp[i - 1][j - 1] + c[i].j;由于还要保证Petra尽量大,所以转移过程中记录被Jan取走糖果的p总和作为cost,然后Petra最后的快乐值为sump - cost;所以cost要尽量小,在状态转移过程中维护即可。
题目可能是从Petra先取,不过这个稍微处理一下,假如Petra先取,就把第一个糖果给他,剩下的糖果在去dp。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N = 1005;
int t, n, dp
, cost
, sum;
char name[10];
struct Candy {
int p, j;
} c
;
bool cmp(Candy a, Candy b) {
if (a.p != b.p)
return a.p > b.p;
return a.j < b.j;
}
void solve() {
int bo = 0;
if (name[0] == 'P')
bo = 1;
memset(dp, 0, sizeof(dp));
memset(cost, 0, sizeof(cost));
for (int i = 1; i <= n - bo; i++) {
for (int j = 1; j <= (i + 1)/2; j++) {
if (dp[i - 1][j] > dp[i - 1][j - 1] + c[i + bo].j) {
dp[i][j] = dp[i - 1][j];
cost[i][j] = cost[i - 1][j];
}
else if (dp[i - 1][j] == dp[i - 1][j - 1] + c[i + bo].j) {
dp[i][j] = dp[i - 1][j];
cost[i][j] = min(cost[i - 1][j], cost[i - 1][j - 1] + c[i + bo].p);
}
else {
dp[i][j] = dp[i - 1][j - 1] + c[i + bo].j;
cost[i][j] = cost[i - 1][j - 1] + c[i + bo].p;
}
}
}
printf("%d %d\n", sum - cost[n - bo][(n - bo + 1) / 2], dp[n - bo][(n - bo + 1) / 2]);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%s", &n, name);
sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &c[i].p, &c[i].j);
sum += c[i].p;
}
sort(c + 1, c + n + 1, cmp);
solve();
}
return 0;
}
To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie.
Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks
the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.)
Jan's strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible.
You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with?
Input
On the first line a positive integer: the number of test cases, at most 100. After that per test case:One line with an integer n (1 ≤ n ≤ 1 000): the number of goodies.
One line with a string, either "Petra" or "Jan": the person that chooses first.
n lines with two integers pi and ji (0 ≤ pi,ji ≤ 1 000) each: the values that Petra and Jan assign to the i-th goodie, respectively.
Output
Per test case:One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations.
Sample in- and output
Input | Output |
3 4 Petra 100 80 70 80 50 80 30 50 4 Petra 10 1 1 10 6 6 4 4 7 Jan 4 1 3 1 2 1 1 1 1 2 1 3 1 4 | 170 130 14 16 9 10 |
思路:Petra的策略满足贪心,所以先把糖果按P在按J排序,然后去取,就看Jan会取哪些糖果了,那么每个糖果就可以看成是Jan取不取,但是要注意,由于Petra是一定会往后一个取,所以Jan取糖果的时候是有一定的限制的,该限制为:假如都是Jan先取,1个糖果,Jan可以拿到,2个糖果,Jan可以拿其中一个,3个糖果Jan可以拿其中两个,4个糖果也是其中两个,以此类推,Jan能拿的糖果数为(i + 1)/2。因此dp[i][j]表示Jan在前i个糖果中拿了j个,j <= (i + 1)/2.
状态转移方程为:如果不取 dp[i][j] = dp[i - 1][j] 如果取dp[i][j] = dp[i - 1][j - 1] + c[i].j;由于还要保证Petra尽量大,所以转移过程中记录被Jan取走糖果的p总和作为cost,然后Petra最后的快乐值为sump - cost;所以cost要尽量小,在状态转移过程中维护即可。
题目可能是从Petra先取,不过这个稍微处理一下,假如Petra先取,就把第一个糖果给他,剩下的糖果在去dp。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N = 1005;
int t, n, dp
, cost
, sum;
char name[10];
struct Candy {
int p, j;
} c
;
bool cmp(Candy a, Candy b) {
if (a.p != b.p)
return a.p > b.p;
return a.j < b.j;
}
void solve() {
int bo = 0;
if (name[0] == 'P')
bo = 1;
memset(dp, 0, sizeof(dp));
memset(cost, 0, sizeof(cost));
for (int i = 1; i <= n - bo; i++) {
for (int j = 1; j <= (i + 1)/2; j++) {
if (dp[i - 1][j] > dp[i - 1][j - 1] + c[i + bo].j) {
dp[i][j] = dp[i - 1][j];
cost[i][j] = cost[i - 1][j];
}
else if (dp[i - 1][j] == dp[i - 1][j - 1] + c[i + bo].j) {
dp[i][j] = dp[i - 1][j];
cost[i][j] = min(cost[i - 1][j], cost[i - 1][j - 1] + c[i + bo].p);
}
else {
dp[i][j] = dp[i - 1][j - 1] + c[i + bo].j;
cost[i][j] = cost[i - 1][j - 1] + c[i + bo].p;
}
}
}
printf("%d %d\n", sum - cost[n - bo][(n - bo + 1) / 2], dp[n - bo][(n - bo + 1) / 2]);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%s", &n, name);
sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &c[i].p, &c[i].j);
sum += c[i].p;
}
sort(c + 1, c + n + 1, cmp);
solve();
}
return 0;
}
相关文章推荐
- uva 12260 - Free Goodies (dp,贪心 | 好题)
- uva 12260 - Free Goodies(dp+贪心)
- uva 12260 - Free Goodies (dp,贪心 | 好题)
- uva12260 - Free Goodies 贪心+dp
- UVA - 12260 Free Goodies(dp比较难想到)
- Free Goodies - UVa 12260 dp
- UVA 12260 Free Goodies (DP)
- uva 12260 Free Goodies DP+一点点贪心
- UVaLive 4731 UVa 1456 - Cellular Network(概率DP 贪心)
- uva 1382 Distant Galaxy (枚举&&扫描&&贪心dp)
- UVa 607 Scheduling Lectures(DP, 贪心)
- Uva 607 Scheduling Lectures (Dp+贪心)
- UVA1456|LA4731------Cellular Network------带贪心的概率型DP
- UVALive 4731 dp+贪心
- uva_607 - Scheduling Lectures( 贪心+DP )
- UVA 1456 Cellular Network 贪心+概率dp
- uva 12260(dp)
- uvalive4945(dp,贪心)
- Uva-1228 Integer Transmission(贪心+DP)
- SDUT 3222 Free Goodies(贪心+dp)