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? 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 ≤ 1000): 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 ≤ 1000) 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 Input 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 Sample Output 170 130 14 16 9 10

两个人选东西,一个是贪心的策略,选当前对自己价值尽量大的,然后让对别人的价值尽量小

第二个人的策略是让自己最终的价值尽量大,同时让自己选的东西对别人的价值尽量小

对物品按第一个人的价值进行排序,(其实这就是第一个人对物品的选择序),然后在这个序列上进行动态规划。

DP[i][j]表示在前i个物品中选择j个第二个人获得的价值

cost[i][j[表示在前I个物品中选择j个第一个人损失的价值

dp[i][j] = dp[i-1][j-1] + a[i].val 注意j不能超过(i+1)/2

如果是贪心的先选就从序列中第二个元素开始DP

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; #define MAXN 1008
#define INF 0x3f3f3f3f struct node
{
int pi, ji;
bool operator < (const node& rhs)const
{
if (pi == rhs.pi)
return ji < rhs.ji;
return pi > rhs.pi;
}
};
int dp[MAXN][MAXN], n, cost[MAXN][MAXN];
//dp[i][j] 表示前i个物品中选取j个
// cost[i][j]表示---的时候P损失的价值
node a[MAXN];
char op[];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
memset(dp, , sizeof(dp));
memset(cost, , sizeof(cost));
int beg = , sum = ;
scanf("%d%s", &n, op);
if (op[] == 'P')
beg = ;
for (int i = ; i <= n; i++)
scanf("%d%d", &a[i].pi, &a[i].ji), sum += a[i].pi;
sort(a + , a + n + );
for (int i = ; i <= n - beg; i++)//如果是P开始就dp处理n-1个物品,如果是j开始就dp n 个
{
for (int j = ; j <= (i + ) / ; j++)
{
if (dp[i - ][j] > dp[i - ][j - ] + a[i + beg].ji)
{
dp[i][j] = dp[i - ][j];
cost[i][j] = cost[i - ][j];
}
else if (dp[i - ][j] == dp[i - ][j - ] + a[i + beg].ji)
{
dp[i][j] = dp[i - ][j];
cost[i][j] = min(cost[i - ][j], cost[i - ][j - ] + a[i + beg].pi);
}
else
{
dp[i][j] = dp[i - ][j - ] + a[i + beg].ji;
cost[i][j] = cost[i - ][j - ] + a[i + beg].pi;
}
}
}
printf("%d %d\n", sum - cost[n - beg][(n - beg + ) / ], dp[n - beg][(n - beg + ) / ]);
}
}

Free Goodies UVA - 12260的更多相关文章

  1. Free Goodies UVA - 12260 贪心

      Free Goodies Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit ...

  2. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  3. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  4. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  5. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  6. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  7. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  8. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  9. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. web+ admin template,spa管理应用后台,easyui后台正式发布

    演示地址:http://admintemplate.webplus.org.cn/ v1.0 (2016/7/27) 扁平化风格 全屏支持 后台管理不使用iframe,全ajax开发 权限管理 商品管 ...

  2. (七)Mybatis总结之注解开发

    请移步到 https://www.cnblogs.com/lxnlxn/p/5996707.html

  3. ES5之变量

    什么是变量:存放物体的一个容器,以便后续利用该容器存放的物体. 变量的声明及赋值: 声明变量关键字var; 变量名的规范:变量名由英文字母.数字.下划线.美元符号组成,但是首字母只能是英文字母.下划线 ...

  4. 新奇:(nodejs兄弟)用HTML + FLASH +JS 也可以写桌面EXE。

    首先看下面这张图片,下面的所有界面都是用html代码实现的. 编程IDE:vb6.0 使用控件:WEBBROWSER 原理:使用olelib 让程序继承:IDocHostUIHandler 和 ICu ...

  5. 洛谷 P1339 [USACO09OCT]热浪Heat Wave (堆优化dijkstra)

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...

  6. 原生 js 整理

    常见的事件 window.event     代表着,事件的状态,只有在事件的过程中才有效.

  7. OpenCV2:应用篇 QT+OpenCV实现图片编辑器

    一.简介 做完会放在Github上  

  8. javascript中常见undefined与defined的区别

    在JavaScript中相信“undefined”与“defined”对大家来说都肯定不陌生,但是又不是很清楚它们的区别,先看两个demo我们再说, 例1. console.log(parms); / ...

  9. 中南大学2019年ACM寒假集训前期训练题集(入门题)

    A: 漫无止境的八月 Description 又双叒叕开始漫无止境的八月了,阿虚突然问起长门在这些循环中团长哪几次扎起了马尾,他有多少次抓住了蝉等等问题,长门一共回复n个自然数,每个数均不超过1500 ...

  10. LUA-点号和冒号

    由于LUA中是模拟类,没有class, 所以这里是使用.号来访问实例的成员 re.SetActive(re, re.activeSelf == false); 而冒号:  则是种语法糖,省略了上面代码 ...