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. 数据结构 - 静态单链表的实行(C语言)

    静态单链表的实现 1 静态链表定义 静态链表存储结构的定义如下: /* 线性表的静态链表存储结构 */ #define MAXSIZE 1000 /* 假设链表的最大长度是1000 */ typede ...

  2. hdu 1044 Collect More Jewels

    题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...

  3. [hdu4089] Activation【概率dp 数学期望】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4089 本来可以一遍过的,结果mle了一发...注意要用滚动数组. 令f(i, j)表示队列剩余i个人,这 ...

  4. Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数

    Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...

  5. 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

    题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...

  6. 题解报告:hdu 1171 Big Event in HDU(多重背包)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  7. NSSet转成NSArray 以及NSSortDescriptor的使用

    //如果想排序以后再取,可以这样:NSSet *users = [groupUser users]; //如果是存的字典,则key后面写的是想按照哪个关键字进行排序 NSSortDescriptor ...

  8. 468 Validate IP Address 验证IP地址

    详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...

  9. (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5

    1.浏览器处理图像的过程: 1.服务器获取文件,显示出文本结构,以及预留默认的大小给<img>(如果该<img>有width-1值和height-1值,则根据这个值提前设好页面 ...

  10. HttpWebRequest 以及WebRequest的使用

    1.WebRequest的发送数据以及接收数据 class Program { static void Main(string[] args) { //创建一个实例并发送请求 HttpWebReque ...