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. (七)Mybatis总结之注解开发

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

  2. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  3. git忽略文件权限的检查

    在linux上配置了一个samba服务器,方便在linux上通过ide修改代码,然后发现一个很烦人的问题,就是没有修改权限,在使用命令 chmod 777 filename后可以修改了,然而使用git ...

  4. nodejs idea 创建项目 (一)

    1.在工作空间创建module file->new module next next 项目的目录结构: bin:跟业务无关的公共部分 node_modules :默认的模块 public:公共模 ...

  5. myeclipse 跟踪struts 源码失败

    解决办法: 找到工程jar包所在的位置,点击右键:properties 点击external folder 找到  这个包下的src文件夹 导入之后, 源码会变色

  6. 【PostgreSQL-9.6.3】启动,登录,退出,关闭

    当我们费尽千辛万苦安装完数据库后,一定会迫不及待的想使用它.骚年,不要着急,且看我为您解析PostgreSQL的启动,登录,退出,关闭过程. 一 启动数据库服务器 1. 没有设置环境变量的情况下 po ...

  7. 在sql server 如何创建一个只读账户

    设置步骤 进入Sqlserver Management Studio(MSSQL客户端) 选择安全性->登录名->右键新建登录名 在常规里输入用户名和密码 在"用户映射" ...

  8. rem手机端页面自适应布局(待修正下一篇完美布局)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. JS对json中某字段进行排序

    var data =[ { "cid":1, "name":"aaa", "price":1000 },{ " ...

  10. MFC_简易文件管理器

    练习_简易文件管理器 Edit1编辑框绑定变量,初始化内容 m_EditCtrl = L"D:\"; 添加List控件,属性设置report,OnInitDialog()函数里添加 ...