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. colormap画出的图不是彩色问题

    针对matlab2017渲染出的彩色图是黑白的问题. t=labels; t(tstSet(:,end-))=Relabels; t=reshape(t,,); t=t'; figure imshow ...

  2. 安卓(Android)关于 RecyclerView 不能填充满宽度

    RecyclerView 不能填充满屏幕宽度 RecyclerView 的 Adapter 在使用是,一定要 @Overridepublic RecyclerView.ViewHolder onCre ...

  3. K2 blackpearl 安装向导

    最近我在Windows Server 2012 R2上面安装K2 blackpearl遇到了不小的麻烦,于是乎写了这篇向导,把自己遇到的问题记录下来,留给自己和需要帮助的人参考. 首先要解压缩blac ...

  4. mysql 插入多条记录,重复值不插入

    只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...

  5. 田字格布局html div

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. How to Configure YUM to Install Packages From Installation ISO (RHEL)

    1. Mount RHEL Installation ISO mkdir /media/dvd mount /dev/cdrom /media/dvd 2. Get Media ID with the ...

  7. ZooKeeper系列(三)

    前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...

  8. 数组,寻找第K大的数

    时间复杂度 O(n) def partition(data,left,right): if (len(data)<=0 or left<0 or right>=len(data)): ...

  9. 【转】c语言中的定义和声明

    1. 变量的定义.声明 变量的声明有两种情况: 一种是需要建立存储空间的.例如:int  a.在声明的时候就已经建立了存储空间.这种声明是"定义性声明(defining declaratio ...

  10. 关于Maven项目的pom.xml中的依赖或插件失效的解决方法

    1.请将<dependency>标签包含的依赖从<dependencyManagement>中拿出来,单独放在<dependencies>标签里面.2.请将< ...