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. Oracle用户角色权限相关视图

    常用相关视图概述 DBA_SYS_PRIVS: 查询某个用户所拥有的系统权限 USER_SYS_PRIVS: 当前用户所拥有的系统权限 SESSION_PRIVS: 当前用户所拥有的全部权限 ROLE ...

  2. html添加css——样式选择器

    如何给html添加样式.两种方法: 一.新建立一个css样式表,与原html同目录,然后通过link标签链接.如:<link type="text/css" rel=&quo ...

  3. Vuex/Vue 练手项目 在线汇率转换器

    详情请点击: https://zhuanlan.zhihu.com/p/33362758

  4. OpenGL VAO, VBO 使用简介

    参照代码样例: // This function takes in a vertex, color, index and type array // And does the initializati ...

  5. jQuery 小实例 关于按字母排序

    jQuery的强大再次不再赘述 一般情况下操作表格式数据的一种最常见的任务就是排序,在一个大型的表格中,能够对要寻找的信息进行重新排列是非常重要的,一般情况用来完成排序的方式有两种 :一种是服务器端排 ...

  6. [转载]迅为4418开发板Qt移植移动4G模块第一部分

        本文转自迅为论坛:http://topeetboard.com   平台:iTOP-4418开发板   1.首先要配置内核,这个一步和Android系统移植3G或者4G模块是一样的.一般模块的 ...

  7. AIX 10201 ASM RAC安装+升级到10204

    1:查看系统版本 [rac1:root:/hacmp/hacmp5.4/ha5.4/installp/ppc] oslevel -s 6100-06-06-1140 lslpp -al bos.adt ...

  8. 集成新版(5.17+)Activiti Modeler与Rest服务

    声明: 此教程适合Activiti 5.17+版本. 本博客所涉及的内容均可在kft-activiti-demo中找到. 在线demo可以访问 http://demo.kafeitu.me:8080/ ...

  9. Spring Boot(15)——自动配置Validation

    自动配置Validation当应用中的Classpath下存在javax.validation的实现时,Spring Boot的org.springframework.boot.autoconfigu ...

  10. vsphere中的vcenter创建esxi模板虚拟机新建无法连接网络

    1.删除网卡配置文件下的uuid和hwaddr  这是因为虚拟机模板创建网卡mac没改变 2.删除规则文件 rm -f /etc/udev/rules.d/-prtsistent-net.rules ...