Cake


Time Limit: 4 Seconds      Memory Limit: 65536 KB

Alice and Bob like eating cake very much. One day, Alice and Bob went to a bakery and bought many cakes.

Now we know that they have bought n cakes in the bakery. Both of them like delicious cakes, but they evaluate the cakes as different values. So they decided to divide those cakes by following method.

Alice and Bob do n / 2 steps, at each step, Alice choose 2 cakes, and Bob takes the cake that he evaluates it greater, and Alice take the rest cake.

Now Alice want to know the maximum sum of the value that she can get.

Input

The first line is an integer T which is the number of test cases.

For each test case, the first line is an integer n (1<=n<=800). Note that n is always an even integer.

In following n lines, each line contains two integers a[i] and b[i], where a[i] is the value of ith cake that Alice evaluates, and b[i] is the value of ith cake that Bob evaluates. (1<=a[i]b[i]<=1000000)

Note that a[1]a[2]..., a[n] are n distinct integers and b[1]b[2]..., b[n] are n distinct integers.

Output

For each test case, you need to output the maximum sum of the value that Alice can get in a line.

Sample Input

1
6
1 6
7 10
6 11
12 18
15 5
2 14

Sample Output

28

Author: HUA, Yiwei

解题:$动态规划,dp[i][j]表示有i个cake,选了j个给Bob,剩下的给Alice,由于预先按每块cake的b属性由高到低排序$

$所以,可以保证每次加入的Cake 如果是给Alice的一定可以成功的给Alice,因i里面有j个的cake的b属性比当前cake的b属性大$

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
typedef long long LL;
struct Cake{
int a,b;
bool operator<(const Cake &rhs)const{
return b > rhs.b;
}
}cake[maxn];
LL dp[][maxn]; int main(){
int kase,n;
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
for(int i = ; i <= n; ++i)
scanf("%d%d",&cake[i].a,&cake[i].b);
sort(cake + ,cake + n + );
memset(dp,,sizeof dp);
int cur = ;
for(int i = ; i <= n; ++i){
for(int j = ((i-)>>); j >= ; --j){
dp[cur^][i-j] = max(dp[cur^][i-j],dp[cur][i--j]);
dp[cur^][i-j-] = max(dp[cur^][i-j-],dp[cur][i--j] + cake[i].a);
}
memset(dp[cur],,sizeof dp[cur]);
cur ^= ;
}
printf("%lld\n",dp[cur][n>>]);
}
return ;
}

ZOJ 3905 Cake的更多相关文章

  1. ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

    Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...

  2. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  3. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  4. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  5. zoj 3537 Cake (凸包确定+间隔dp)

    Cake Time Limit: 1 Second      Memory Limit: 32768 KB You want to hold a party. Here's a polygon-sha ...

  6. ZOJ 3537 Cake(凸包判定+区间DP)

    Cake Time Limit: 1 Second Memory Limit: 32768 KB You want to hold a party. Here's a polygon-shaped c ...

  7. ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)

    Description You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut t ...

  8. zoj 3537 Cake(区间dp)

    这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三 ...

  9. ZOJ 3537 Cake

    区间DP. 首先求凸包判断是否为凸多边形. 如果是凸多边形:假设现在要切割连续的一段点,最外面两个一定是要切一刀的,内部怎么切达到最优解就是求子区间最优解,因此可以区间DP. #include< ...

随机推荐

  1. [POI2008]POD Subdivision of Kingdom

    Description 给出一个具有N个结点的无向图,将其分成两个集合S1,S2. 这两个集合的点的个数一样多,但连接它们的边最少. Input 第一行给出数字N,M,代表有N个点,M条边. 下面M行 ...

  2. “玲珑杯”第七届郑州轻工业学院ACM程序设计大赛 ------- D:社交网络

    题目链接: http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=3 题目大意: 国语题目,题意显而易见, 解题思路: 只需要对每一个节点进行假设, ...

  3. LoadRunner12学习之路(6-8)

    六.创建负载测试场景 如何启动控制器? 要开始开发场景,请打开LoadRunner Controller. 打开HPE LoadRunner Controller. 在LoadRunner机器上,单击 ...

  4. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  5. Appium教程---Client/Server Architecture

    appium的核心其实是一个暴露了一系列REST API的server. 这个server的功能其实很简单:监听一个端口,然后接收由client发送来的command.翻译这些command,把这些c ...

  6. magento 添加事件

    首先是配置文件config.xml里的配置 <checkout_cart_save_after> /*事件名字*/ <observers> <deal> /*模块名 ...

  7. 安装11g 数据库

    出现问题解决: 1.首先确认下载的安装包完整性.2解压包的时候,按顺序解压,解压第一个包后,解压第二个包的时候,要把解压地址与解压第二包的地址要一样. 安装的时候,需要把两个压缩包都解压,并将目录wi ...

  8. pandas 选择某几列

    转自:https://blog.csdn.net/aaa_aaa1sdf/article/details/77414387 col_n = ['名称','收盘价','日期'] a = pd.DataF ...

  9. iOS-UI控件之UITableView(二)- 自定义不等高的cell

    不等高的cell 给模型增加frame数据 所有子控件的frame cell的高度 @interface XMGStatus : NSObject /**** 文字\图片数据 ****/ // ... ...

  10. KVM环境下vCPU绑定到物理CPU

    在KVM环境中测试虚拟系统性能时,如果宿主机是有两个CPU socket的硬件,会碰到由于vCPU在不同物理CPU上浮动导致测试RFC2544时出现少量丢包的现象,测试结果非常不稳定.可以将vCPU绑 ...