Free Goodies UVA - 12260
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的更多相关文章
- Free Goodies UVA - 12260 贪心
Free Goodies Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Submit ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- 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 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- Zclip点击复制内容到剪贴板兼容各浏览器
WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮,只需要轻轻一点这个复制按钮,内容将会被复制, ...
- leetcode790 Domino and Tromino Tiling
思路: dp.没有像discuss中的那样优化递推式. 实现: class Solution { public: ; int numTilings(int N) { vector<vector& ...
- poj3050 Hopscotch
思路: 水题. 实现: #include <iostream> #include <cstdio> #include <set> using namespace s ...
- Android开发中实现桌面小部件
详细信息请参考原文:Android开发中实现桌面小部件 在Android开发中,有时候我们的App设计的功能比较多的时候,需要根据需要更简洁的为用户提供清晰已用的某些功能的时候,用桌面小部件就是一个很 ...
- Farseer.net轻量级开源框架 中级篇:探究ORM(Mapping)
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: SQL执行报告 下一篇:Farseer.net轻量级开源框架 中级篇: Cooki ...
- 转:谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲: 第一个分享的主题是 ...
- app dcloud 打包公用证书
Android平台云端打包使用的DCloud公用证书 分类:HTML5+ 5+App开发 HBuilder|HBuilderX应用云端打包Android平台默认使用的DCloud公用证书,其信息如下: ...
- CAD参数绘制块引用对象(com接口)
主要用到函数说明: _DMxDrawX::DrawBlockReference 绘制块引用对象.详细说明如下: 参数 说明 DOUBLE dPosX 插入点的X坐标 DOUBLE dPosY 插入点的 ...
- April Fools Day Contest 2019: editorial回顾补题
A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- php腾讯云短信验证码
腾讯云短信控制台:https://console.cloud.tencent.com/sms 腾讯云短信 PHP SDK:https://github.com/qcloudsms/qcloudsms_ ...