ZOJ 3905 Cake
2015-10-11 18:22
375 查看
CakeTime 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.
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
are n distinct integers and b[1], b[2]..., b
are n distinct integers.
Author: HUA, Yiwei
解题:$动态规划,dp[i][j]表示有i个cake,选了j个给Bob,剩下的给Alice,由于预先按每块cake的b属性由高到低排序$
$所以,可以保证每次加入的Cake 如果是给Alice的一定可以成功的给Alice,因i里面有j个的cake的b属性比当前cake的b属性大$
View Code
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
are n distinct integers and b[1], b[2]..., b
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 = 810; typedef long long LL; struct Cake{ int a,b; bool operator<(const Cake &rhs)const{ return b > rhs.b; } }cake[maxn]; LL dp[2][maxn]; int main(){ int kase,n; scanf("%d",&kase); while(kase--){ scanf("%d",&n); for(int i = 1; i <= n; ++i) scanf("%d%d",&cake[i].a,&cake[i].b); sort(cake + 1,cake + n + 1); memset(dp,0,sizeof dp); int cur = 0; for(int i = 1; i <= n; ++i){ for(int j = ((i-1)>>1); j >= 0; --j){ dp[cur^1][i-j] = max(dp[cur^1][i-j],dp[cur][i-1-j]); dp[cur^1][i-j-1] = max(dp[cur^1][i-j-1],dp[cur][i-1-j] + cake[i].a); } memset(dp[cur],0,sizeof dp[cur]); cur ^= 1; } printf("%lld\n",dp[cur][n>>1]); } return 0; }
View Code
相关文章推荐
- MAC电脑DNS劫持解决方法
- 【重要更新】Senparc.Weixin.Open v1.5.1
- 清除浮动1-使用:after 伪元素
- nginx文件类型错误解析漏洞
- requestFeature() must be called before adding content
- 最长公共子序列
- android【基础】 style标签应用与activity间的淡入淡出切换
- 挑战程序设计竞赛:三角形
- 开发小公司的一个程序模块的感触
- 浅谈HTTP中Get与Post的区别
- R12 查询EBS用户相关SQL
- Web存储
- BufferReader 的使用
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #8
- 黑马程序员-java基础(四)-面向对象(封装、继承、多态)
- hdu 5500 Reorder the Books
- Number类为数字到字符串的类型转换场景定义三种方法
- maven3.3.3 mac 下 Unsupported major.minor version 51.0
- python中魔法属性和魔法方法
- poj2761(treap入门)