zoj3905
2015-11-12 15:27
330 查看
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.
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
Source: ZOJ Monthly, October 2015
题解:先按照b值从大到小金星排序,排序的原因是使得从前往后计算过程中,只要是想将该值给Alice就一定可以通过改变之前的取得方式把该值给Alice.
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
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
Source: ZOJ Monthly, October 2015
题解:先按照b值从大到小金星排序,排序的原因是使得从前往后计算过程中,只要是想将该值给Alice就一定可以通过改变之前的取得方式把该值给Alice.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define N 810 struct Cake{ int a, b; bool operator < (const Cake & t)const{ return b > t.b; } }C ; int n, _T; int dp[2] ; int main(){ scanf("%d", &_T); while(_T--){ scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d%d", &C[i].a, &C[i].b); } memset(dp, 0, sizeof dp); sort(C+1, C+1+n); int cur = 0; for(int i = 1; i <= n; i++){ cur ^= 1; memset(dp[cur], 0, sizeof dp[cur]); for(int j = 1; 2*j <= i; j++){ dp[cur][j] = max(dp[cur^1][j], dp[cur^1][j-1] + C[i].a); } } printf("%d\n", dp[cur][n>>1]); } return 0; }
相关文章推荐
- 1112语法树博客评论
- 评论
- 1112对其他人的评论与建议
- Masonry介绍与使用
- 在C++中怎么判断一个double型数据的小数点部分是否为零
- 引用类型(下)
- 文章标题
- php表单提交实例讲解
- 讲解Java编程中finally语句的基本使用方法
- AUGTEK成为广域物联网国际联盟董事会唯一亚洲成员
- Python 登录系统
- [小木虫]推荐几个机器学习算法及应用领域相关的中国大牛
- 关于在jsp中使用图片相对路径的问题
- call()和apply()
- 冒泡算法程序分析树的评论
- 11.12 评论汇总
- 词法分析评论
- 腾讯优测自动化测试场景丨iBeacon走进微信“豪门”
- Python游戏服务器开发日记(九)近期问题总结。(打酱油中……)
- C++ primer 第五版 答案