zoj3905 Cake
2015-10-12 16:36
267 查看
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.
题意:有n个球,两个人玩游戏,每次选出两个球,B会选出他认为球的价值比较大的,会挑走一个球,然后A会得到另一个球的价值,问怎样取能使A获得的价值最大。
思路:可以先按B所认为的价值从大到小排序,那么对于任意前m件物品,A可以得到m/2件,然后设dp[i][j]表示前i件物品A取出j件最多获得的价值,那么当循环到前第i件时有两种转移形式,一种是这一件不取,那么前i-1件就要取j件,否则就要取j-1件,即dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+c[i].a);
还有两种是贪心的解法,一个是从前往后贪心,还有一个是从后往前贪心,两者的本质是一样的。我们考虑从后往前贪心,因为不管A所选择的两个数是什么,都是B先选,所以我们可以先把n个数据按b为第一关键字从大到小排序,按a从大到小排序。那么不管我们取出哪两个数,B都是取排在前面的数,题目等价于有n个数,B按顺序取对其来说价值最大的数,A尽可能多的取得对它来说价值大的数。那么我们考虑B取法的规律,前1个数,B至少取1个,前3个数,B至少取2个,前5个数,B至少取3个....前n(n为奇数)个数,B至少取n/2+1个,所以我们可以推出前1个数必由B取,前3个数A至多取1个,前5个数,A至多取3个...那么我们便可进一步推出,后1个数,A至少取1个,后3个数,A至少取2个,后5个数,B至少取3个,那么我们可以从后往前每次把2个数放入set里,然后把set里最大的数取出,加上该值就行了。
代码一:(从后往前推)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000
struct node1{
int num;
}temp,temp1;
bool operator<(node1 c,node1 d){
return c.num>d.num;
}
set<node1>myset;
set<node1>::iterator it;
struct node{
int a,b;
}c[maxn];
bool cmp(node c,node d){
return c.b>d.b;
}
int main()
{
int n,m,i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d%d",&c[i].a,&c[i].b);
}
sort(c+1,c+1+n,cmp);
if(n==2){
printf("%d\n",c[2].a);
continue;
}
int sum=c
.a;
myset.clear();
for(i=n-1;i>=1;i--){
node1 temp;
temp.num=c[i].a;
myset.insert(temp);
if(i%2==0){
it=myset.begin();
sum+=(*it).num;
myset.erase(*it);
}
}
printf("%d\n",sum);
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14*/
代码二:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000
set<int>myset;
set<int>::iterator it;
struct node{
int a,b;
}c[maxn];
bool cmp(node c,node d){
return c.b>d.b;
}
int main()
{
int n,m,i,j,T,sum1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum1=0;
for(i=1;i<=n;i++){
scanf("%d%d",&c[i].a,&c[i].b);
sum1+=c[i].a;
}
sort(c+1,c+1+n,cmp);
if(n==2){
printf("%d\n",c[2].a);
continue;
}
int sum=0;
myset.clear();
for(i=1;i<=n;i++){
myset.insert(c[i].a);
if(i%2==1){
it=myset.begin();
sum+=*it;
myset.erase(it);
}
}
printf("%d\n",sum1-sum);
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14*/
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题意:有n个球,两个人玩游戏,每次选出两个球,B会选出他认为球的价值比较大的,会挑走一个球,然后A会得到另一个球的价值,问怎样取能使A获得的价值最大。
思路:可以先按B所认为的价值从大到小排序,那么对于任意前m件物品,A可以得到m/2件,然后设dp[i][j]表示前i件物品A取出j件最多获得的价值,那么当循环到前第i件时有两种转移形式,一种是这一件不取,那么前i-1件就要取j件,否则就要取j-1件,即dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+c[i].a);
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; #define ll long long #define inf 0x7fffffff #define maxn 810 struct node{ int a,b; }c[maxn]; bool cmp(node a,node b){ return a.b>b.b; } int dp[maxn][maxn]; int main() { int n,m,i,j,T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d%d",&c[i].a,&c[i].b); } sort(c+1,c+1+n,cmp); memset(dp,-1,sizeof(dp)); dp[1][0]=0; for(i=2;i<=n;i++){ dp[i][0]=0; for(j=1;j<=i/2;j++){ dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+c[i].a); } } printf("%d\n",dp [n/2]); } }
还有两种是贪心的解法,一个是从前往后贪心,还有一个是从后往前贪心,两者的本质是一样的。我们考虑从后往前贪心,因为不管A所选择的两个数是什么,都是B先选,所以我们可以先把n个数据按b为第一关键字从大到小排序,按a从大到小排序。那么不管我们取出哪两个数,B都是取排在前面的数,题目等价于有n个数,B按顺序取对其来说价值最大的数,A尽可能多的取得对它来说价值大的数。那么我们考虑B取法的规律,前1个数,B至少取1个,前3个数,B至少取2个,前5个数,B至少取3个....前n(n为奇数)个数,B至少取n/2+1个,所以我们可以推出前1个数必由B取,前3个数A至多取1个,前5个数,A至多取3个...那么我们便可进一步推出,后1个数,A至少取1个,后3个数,A至少取2个,后5个数,B至少取3个,那么我们可以从后往前每次把2个数放入set里,然后把set里最大的数取出,加上该值就行了。
代码一:(从后往前推)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000
struct node1{
int num;
}temp,temp1;
bool operator<(node1 c,node1 d){
return c.num>d.num;
}
set<node1>myset;
set<node1>::iterator it;
struct node{
int a,b;
}c[maxn];
bool cmp(node c,node d){
return c.b>d.b;
}
int main()
{
int n,m,i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d%d",&c[i].a,&c[i].b);
}
sort(c+1,c+1+n,cmp);
if(n==2){
printf("%d\n",c[2].a);
continue;
}
int sum=c
.a;
myset.clear();
for(i=n-1;i>=1;i--){
node1 temp;
temp.num=c[i].a;
myset.insert(temp);
if(i%2==0){
it=myset.begin();
sum+=(*it).num;
myset.erase(*it);
}
}
printf("%d\n",sum);
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14*/
代码二:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000
set<int>myset;
set<int>::iterator it;
struct node{
int a,b;
}c[maxn];
bool cmp(node c,node d){
return c.b>d.b;
}
int main()
{
int n,m,i,j,T,sum1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum1=0;
for(i=1;i<=n;i++){
scanf("%d%d",&c[i].a,&c[i].b);
sum1+=c[i].a;
}
sort(c+1,c+1+n,cmp);
if(n==2){
printf("%d\n",c[2].a);
continue;
}
int sum=0;
myset.clear();
for(i=1;i<=n;i++){
myset.insert(c[i].a);
if(i%2==1){
it=myset.begin();
sum+=*it;
myset.erase(it);
}
}
printf("%d\n",sum1-sum);
}
return 0;
}
/*
1 6 1 6 7 10 6 11 12 18 15 5 2 14*/
相关文章推荐
- 第三周。项目4--顺序表应用(2)
- 项目4 -- 链表算法库 程序的多文件组织形式
- 第六周 1,建立链队算法库
- YTKNetwork 使用基础教程
- Android的IPC机制Binder的详解汇总
- 软件测试管理要素
- 欢迎使用CSDN-markdown编辑器
- python udp编程实例
- 能挣钱的微信JSSDK+H5混合开发
- 大数据SPSS分析-均值比较和T检验
- 第七周项目三~~负数把正数赶出队列
- ContextLoaderListener(监听器)
- [回顾] 如何在Nexus 5(Android 4.4.4)上开启交换分区swap
- 第四周项目 - 建设“单链表”算法库
- 第七周项目一数据结构之自建算法库——顺序环形队列
- 第七周项目1 - 建立顺序环形队列算法库
- Android 通过配置相同的UUid访问不同进程小结
- Android页面间数据传递方式
- java,js的编码解码问题
- 设计模式之迭代器模式