SDUT 3222 Free Goodies(贪心+dp)
2016-05-31 10:13
381 查看
Free Goodies
Time Limit: 1000MS Memory limit: 65536K
题目描述
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?
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?
输入
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.
输出
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.
示例输入
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
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; struct node { int pi, ji; bool operator < (node a)const { return pi == a.pi ? ji < a.ji : pi > a.pi; } }good[maxn]; int dp1[maxn][maxn], dp2[maxn][maxn]; int main() { std::ios::sync_with_stdio(false); int T, n; char str[12]; cin>>T; while(T-- && cin>>n>>str) { memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); long long sum = 0; for(int i=1; i<=n; i++){ cin>>good[i].pi>>good[i].ji; sum += good[i].pi; } sort(good+1, good+1+n); if(str[0] == 'J') { for(int i=1; i<=n; i++){ for(int j=1; j<=(i+1)/2; j++){ if(dp1[i-1][j] > dp1[i-1][j-1] + good[i].ji){ dp1[i][j] = dp1[i-1][j]; dp2[i][j] = dp2[i-1][j]; } else if(dp1[i-1][j] == dp1[i-1][j-1] + good[i].ji){ dp1[i][j] = dp1[i-1][j]; dp2[i][j] = min(dp2[i-1][j], dp2[i-1][j-1]+good[i].pi); } else{ dp1[i][j] = dp1[i-1][j-1] + good[i].ji; dp2[i][j] = dp2[i-1][j-1] + good[i].pi; } } } printf("%d %d\n", sum-dp2 [(n+1)/2], dp1 [(n+1)/2]); } else { for(int i=1; i<n; i++){ for(int j=1; j<=(i+1)/2; j++){ if(dp1[i-1][j] > dp1[i-1][j-1] + good[i+1].ji){ dp1[i][j] = dp1[i-1][j]; dp2[i][j] = dp2[i-1][j]; } else if(dp1[i-1][j] == dp1[i-1][j-1] + good[i+1].ji){ dp1[i][j] = dp1[i-1][j]; dp2[i][j] = min(dp2[i-1][j], dp2[i-1][j-1]+good[i+1].pi); } else{ dp1[i][j] = dp1[i-1][j-1] + good[i+1].ji; dp2[i][j] = dp2[i-1][j-1] + good[i+1].pi; } } } printf("%d %d\n", sum-dp2[n-1][n/2], dp1[n-1][n/2]); } } return 0; }
相关文章推荐
- Winlogon通知包(Winlogon Notification Package)
- blast2go进行Nr注释
- google chrome开发者工具项(二)
- [转]Django实战(11):修改Model类
- google chrom 开发工具选项快捷操作
- 设计模式-代理模式(Go语言描述)
- Google谷歌在根据流量统计分析当年的2008年汶川大地震
- 瓜娃子?【Google Guava 学习笔记 一】
- dajngo 表单
- golang windows开发环境
- 多环境下的配置管理方案
- Rockmongo 查询条件总结
- 基于Ubuntu14.04+ROS indigo环境LSD-SLAM的数据集测试成功
- 优分享VR开源啦,优分享VR是基于Google VR开发的一款手机VR视频资源的聚合软件
- [翻译]PyMongo官方文档
- google删除后重新安装失败
- 深度解读 AlphaGo 算法原理
- GO的跨平台数扰类型
- 基因数据处理43之mango之503错误
- 【HDU】 1397 Goldbach's Conjecture