ZOJ 3905 Cake(dp)
2015-10-12 10:18
183 查看
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.
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.
For each test case, you need to output the maximum sum of the value that Alice can get in a line.
Author: HUA, Yiwei
Submit Status
/*
思路: 按 b排序,那么转化为了 在n个物品中取n/2个,同时这n/2个取完前面有n/2个物品被取了
我们算你一个人可以取的最小值,dp[i][j] 代表取到i这个位置,对方已经取了j 个物品
那么剩下来的物品应该满足 n-i>= (need-j)*2+1,
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
const int INF=0x3f3f3f3f;
typedef long long LL;
#define INF 0x3f3f3f3f
#define N 805
int dp
;
int n;
int va
;
struct stud{
int a,b;
bool operator<(const stud s)const
{
return b>s.b;
}
}f
;
void solve()
{
int i,j;
memset(dp,INF,sizeof(dp));
dp[0][0]=0;
int all=0;
for(i=1;i<=n;i++)
all+=va[i];
int need=n/2;
for(i=1;i<=need;i++)
for(j=i;j<=n;j++)
for(int k=i-1;k<j;k++)
{
if(n-j<=(need-i)*2) continue;
if(dp[k][i-1]>=INF) continue;
dp[j][i]=min(dp[j][i],dp[k][i-1]+va[j]);
}
int ans=INF;
for(int i=1;i<n;i++)
ans=min(ans,dp[i][need]);
printf("%d\n",all-ans);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&f[i].a,&f[i].b);
sort(f+1,f+n+1);
for(i=1;i<=n;i++)
va[i]=f[i].a;
solve();
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14
*/
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
Submit Status
/*
思路: 按 b排序,那么转化为了 在n个物品中取n/2个,同时这n/2个取完前面有n/2个物品被取了
我们算你一个人可以取的最小值,dp[i][j] 代表取到i这个位置,对方已经取了j 个物品
那么剩下来的物品应该满足 n-i>= (need-j)*2+1,
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
const int INF=0x3f3f3f3f;
typedef long long LL;
#define INF 0x3f3f3f3f
#define N 805
int dp
;
int n;
int va
;
struct stud{
int a,b;
bool operator<(const stud s)const
{
return b>s.b;
}
}f
;
void solve()
{
int i,j;
memset(dp,INF,sizeof(dp));
dp[0][0]=0;
int all=0;
for(i=1;i<=n;i++)
all+=va[i];
int need=n/2;
for(i=1;i<=need;i++)
for(j=i;j<=n;j++)
for(int k=i-1;k<j;k++)
{
if(n-j<=(need-i)*2) continue;
if(dp[k][i-1]>=INF) continue;
dp[j][i]=min(dp[j][i],dp[k][i-1]+va[j]);
}
int ans=INF;
for(int i=1;i<n;i++)
ans=min(ans,dp[i][need]);
printf("%d\n",all-ans);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&f[i].a,&f[i].b);
sort(f+1,f+n+1);
for(i=1;i<=n;i++)
va[i]=f[i].a;
solve();
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14
*/
相关文章推荐
- 华为机试:输入法查找
- 【软考4】编程语言的发展和特点
- 黑马程序员——API中的小总结
- 项目管理小知识
- windows如何查看某个端口被谁占用
- Nginx 反向代理的正确配置
- 关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别
- 书籍推荐
- Timer和TimerTask
- 下次老板让你重构系统,让他看看这篇文章
- 第一章 Mahout 简介
- Linux学习记录(14)IP别名及多网卡绑定
- [Git] 关于refs/for/ 和refs/heads/
- linux文件操作常用命令
- ASP.Net MVC(3)
- ios隐藏顶部状态和修改顶部状态栏字体颜色
- linux学习札记
- UVA-12545 Bits Equalizer (贪心)
- 动态规划[入门]2-循环数组最大子段和
- 12家人均创收最高的科技公司