[2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)
Pick apples
Time Limit: 1000MS Memory limit: 165536K
题目描写叙述
Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each kind
of apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.
输入
In the first line there is an integer T (T <= 50), indicates the number of test cases.
In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S<= 100) and the price (1 <= P <= 10000) of this kind of apple.
In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.
输出
For each case, first output the case number then follow the most profits she can gain.
演示样例输入
1 1
1 2
1 3
1 6
演示样例输出
Case 1: 6
提示
来源
解题思路:
从昨天赛完就開始弄这道题,当时做的时候第一感觉就是全然背包,可是数据量太大了,直接全然背包肯定会超时。赛后看解题报告才知道这题用的是大范围贪心,小范围全然背包,这个也好懂,看了网上AC的代码小范围用的是1000,但这个今天被老师证实是错误的,这题后台測试数据不完好,比方老师给的这组測试数据:
98 99
99 100
100 101
2000 这组測试数据用小范围为1000的代码測试结果为2000,可是正确答案应该是2020,尽管第一种是最优的东西,可是这里我们要所有选择第三种100 101的才是正确答案。所以小范围为1000是错误的,这里的小范围应该是三种物品容量的最小公倍数,即100*100*100,用1000000就能够了。做这个题花了真不少功夫,整个上午差点儿相同就是在runtime error 和 wrong answer中度过的。找错找了非常久。注意的是:限定的容量V要用long long类型,dp[]数组也要用
long long类型。dp[]数组不能仅仅开1000000个,要尽量大点,由于在贪心部分仅仅选择最优的那种物品,可是大于1000000的那部分不一定能整除该物品的容量,也就是说可能会剩余一部分容量加到1000000里面一起用全然背包来做。这个就是无数次runtime error的原因所在。后来又看到一种解法,特别巧妙,没实用到贪心和背包,就是枚举除了最优物品之外的两外两种背包的选择的个数,这里有一个限定条件,那就是不是最优物品的物品被选择的个数一定比最优物品的容量要小,
比方这组測试数据;
最优物品 3 6
其它 2 3 选一个 2 3 选两个 4 6 选三个6 9 但这时候我们选2个最优物品的情况是 6 12,显然比选三个其它物品要好,也就是说其它物品可能选的个数的情况有0 个,1个,2个。这些都是有可能的,本题背包的容量都小于100,所以两层循环枚举其它两种物品被选择的个数,在计算中取最大值就能够了,复杂度为0(100*100)。
第一种方法(排序做的贪心+全然背包):
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int lcm=1000000;//三种类size的最小公倍数
long long dp[lcm*2+2],V;//dp数组要开的足够大,不能仅仅开lcm个,原因见后面凝视,还有V要用long long struct N
{
int s,p;
double pri;
}node[3]; bool cmp(N a ,N b)
{
return a.pri<b.pri;
}
long long maxll(long long a,long long b)
{
return a>b?a:b;
} void compack()//全然背包
{
memset(dp,0,sizeof(dp));
for(int i=0;i<3;i++)
for(int j=node[i].s;j<=V;j++)
dp[j]=maxll(dp[j],dp[j-node[i].s]+node[i].p);
} int main()
{
int t;cin>>t;int c=1;
while(t--)
{
for(int i=0;i<3;i++)
{
cin>>node[i].s>>node[i].p;
node[i].pri=node[i].s*1.0/node[i].p;
}
cin>>V;
if(V<=lcm)//小范围直接全然背包
{
compack();
cout<<"Case "<<c++<<": "<<dp[V]<<endl;
}
else
{
sort(node,node+3,cmp);//排序,最优的在第一个
V-=lcm;//超出1000000的部分
long long ans=0;
ans=ans+(V/node[0].s*node[0].p);//贪心部分选择第一个所获得的价值
V=lcm+(V-V/node[0].s*node[0].s);//这里解释了为什么dp要开的足够大,不能仅仅开lcm个,由于在贪心部分不一定能整除,有余下的部分
compack();//全然背包
cout<<"Case "<<c++<<": "<<ans+dp[V]<<endl;
}
}
return 0;
}
另外一种方法(未排序的贪心+全然背包):
#include <iostream>
#include <string.h>
using namespace std;
const int lcm=1000000;
long long dp[lcm*2+2],V;
int s[5],p[5]; void compack()
{
memset(dp,0,sizeof(dp));
for(int i=0;i<3;i++)
for(int j=s[i];j<=V;j++)
{
if(dp[j]<dp[j-s[i]]+p[i])
dp[j]=dp[j-s[i]]+p[i];
}
}
int main()
{
int t;cin>>t;int c=1;
while(t--)
{
double pr,temp=0;
int id;
for(int i=0;i<3;i++)
{
cin>>s[i]>>p[i];
pr=1.0*p[i]/s[i];
if(temp<pr)
{
temp=pr;
id=i;
}
}
cin>>V; if(V<=lcm)
{
compack();
cout<<"Case "<<c++<<": "<<dp[V]<<endl;
}
else
{
long long ans=(V-lcm)/s[id]*p[id];
V=V-(V-lcm)/s[id]*s[id];
compack();
cout<<"Case "<<c++<<": "<<ans+dp[V]<<endl;
}
}
}
第三种方法(枚举未排序):
#include <iostream>
#include <stdio.h>
using namespace std;
int s[4],p[4]; long long llmax(long long a,long long b)
{
return a>b?a:b;
}
int main()
{
int t;
cin>>t;
int c=1;
while(t--)
{
for(int i=1; i<=3; i++)
cin>>s[i]>>p[i];
int V;
cin>>V;
int k1=1,k2,k3;
for(int i=2; i<=3; i++)
{
if(p[i]*s[k1]>p[k1]*s[i])//推断优先级
k1=i;
}
if(k1==1){k2=2;k3=3;};//k1是最优的物品,k2,k3是谁没有关系
if(k1==2){k2=1,k3=3;};
if(k1==3){k2=1,k3=2;};
long long ans=0;
for(int i=0; i<s[k1]; i++)//枚举
{
for(int j=0; j<s[k1]; j++)
{
long long temp=i*s[k2]+j*s[k3];
if(temp>V)
break;
else
{
long long v=V-temp;
ans=llmax(ans,v/s[k1]*p[k1]+i*p[k2]+j*p[k3]);//选最大值
}
}
}
cout<<"Case "<<c++<<": "<<ans<<endl;
}
return 0;
}
第四种方法(排序枚举):
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std; struct N
{
int s,p;
double pri;
}node[4]; bool cmp(N a,N b)
{
if(a.pri<b.pri)
return true;
return false;
} long long llmax(long long a,long long b)
{
return a>b?a:b;
} int main()
{
int t;
cin>>t;
int c=1;
while(t--)
{
for(int i=0; i<3; i++)
{
cin>>node[i].s>>node[i].p;
node[i].pri=1.0*node[i].s/(1.0*node[i].p);
}
int V;
cin>>V;
sort(node,node+3,cmp);
long long ans=0;
for(int i=0; i<node[0].s; i++)
{
for(int j=0; j<node[0].s; j++)
{
long long temp=i*node[1].s+j*node[2].s;
if(temp>V)
break;
else
{
long long v=V-temp;
ans=llmax(ans,v/node[0].s*node[0].p+i*node[1].p+j*node[2].p);
}
}
}
cout<<"Case "<<c++<<": "<<ans<<endl;
}
return 0;
}
[2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)的更多相关文章
- [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...
- 山东ACM省赛历届入口
山东省第一届ACM大学生程序设计竞赛 山东省第二届ACM大学生程序设计竞赛 山东省第三届ACM大学生程序设计竞赛 山东省第四届ACM大学生程序设计竞赛 山东省第五届ACM大学生程序设计竞赛 山东省第六 ...
- [2011山东ACM省赛] Sequence (动态规划)
Sequence Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Given an integer number sequence ...
- [2011山东ACM省赛] Identifiers(模拟)
Identifiers Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Identifier is an important ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- [2011山东ACM省赛] Mathman Bank(模拟题)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sr19930829/article/details/24187925 Mathman Bank ni ...
- [2011山东ACM省赛] Binomial Coeffcients(求组合数)
Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
随机推荐
- php写入文件fwrite() 函数用法
在php中,php fwrite() 函数是用于写入文件(可安全用于二进制文件).说的简单点,就是在一个文件中,添加新的内容,本篇文章收集总结了几篇关于php写入文件fwrite() 函数用法的总结, ...
- Elixir木蚂蚁支付服务器验签名方法
官方范例为java public boolean verify(String sign , String appKey , String orderId) throws UnsupportedEnco ...
- Portal的安全代理(反向代理出口)配置架构
对于正式运行的环境,一般需要设置网络安全控制区DMZ,通过代理,把仅需要的端口向客户端暴露,其他内部端口应该是在防火墙包含之内的. 下文将针对ArcGIS 的Portal软件,讲述在DMZ中如何架构的 ...
- TensorFlow分布式部署【单机多卡】
让TensorFlow飞一会儿 面对大型的深度神经网络训练工程,训练的时间非常重要.训练的时间长短依赖于计算处理器也就是GPU,然而单个GPU的计算能力有限,利用多个GPU进行分布式部署,同时完成一个 ...
- Pig store用法举例
store:将数据存储到HDFS等文件系统里 将数据保存到/data目录 store data into '/data'; 以逗号为分隔符 store data into '/data' usin ...
- armel和armhf区别
出于低功耗.封装限制等种种原因,之前的一些ARM架构处理器因为内部资源宝贵,加入浮点运算单元是十分奢侈的,因为需要额外的软件实现.之前的ARM处理器架构是什么样的?(http://www.cnblog ...
- Coroutine及其实现
线程是内核对外提供的服务,应用程序可以通过系统调用让内核启动线程,由内核来负责线程调度和切换.线程在等待IO操作时线程变为unrunnable状态会触发上下文切换.现代操作系统一般都采用抢占式调度,上 ...
- 一些简单的SQL Server服务器监控
1:磁盘活动的一些监控 指标 吞吐量:IOPS,存储子系统每秒能提供多少次I/O 吞吐量,MB/S,I/O子系统每秒能提供多少MB 延时:每个I/O请求占用多少时间 队列长度:队列中有多少IO请求在等 ...
- django中的字段类型
from http://www.cnblogs.com/lhj588/archive/2012/05/24/2516040.html Django 通过 models 实现数据库的创建.修改.删除等操 ...
- 在虚拟机里安装linux(centos 6.5)系统
菜鸟一枚,也是接触linux系统没多长时间,前一阵子网上说有一个高级数据库工程师,因为rm -rf / 命令干掉了数据库-(nb),居然还跑路了!厉害了我的哥!也是闲的我蛋疼,在虚拟机里试了一批,本来 ...