C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6
无线超大背包问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=;
const LL SIZE=; int main()
{
LL c,hr,hb,wr,wb;
cin>>c>>hr>>hb>>wr>>wb;
if(wb>wr)
{
swap(wb,wr);
swap(hb,hr);
} if(wr>SIZE) // 巧妙将 复杂度控制在 10e6
{
LL ans=0LL;
for(LL r=;r*wr<=c;r++)
{
LL b=(c-r*wr)/wb;
LL cur=r*hr+b*hb;
if(cur>ans)
ans=cur;
}
cout<<ans<<endl;
return ;
}
LL ans=;
LL h_big=max(wb*hr,wr*hb);
for(LL r=;r<wb;r++) // 如果r>=wb,可以组成一个公倍数重量的big
{
for(LL b=;b<wr;b++) // 同理
{
LL w=r*wr+b*wb;
if(w>c)
break;
LL big=(c-w)/(wr*wb);
LL cur=r*hr+b*hb+big*h_big;
if(cur>ans)
ans=cur;
}
}
cout<<ans<<endl;
return ;
}
HDU 换了一个背景的同题 Zombie’s Treasure Chest
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=;
const LL SIZE=; int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
LL c,hr,hb,wr,wb;
//cin>>c>>hr>>hb>>wr>>wb;
cin>>c>>wr>>hr>>wb>>hb;
if(wb>wr)
{
swap(wb,wr);
swap(hb,hr);
} if(wr>SIZE) // 巧妙将 复杂度控制在 10e6
{
LL ans=0LL;
for(LL r=;r*wr<=c;r++)
{
LL b=(c-r*wr)/wb;
LL cur=r*hr+b*hb;
if(cur>ans)
ans=cur;
}
cout<<"Case #"<<kase++<<": "<<ans<<endl;
continue;
}
LL ans=;
LL h_big=max(wb*hr,wr*hb);
for(LL r=;r<wb;r++) // 如果r>=wb,可以组成一个公倍数重量的big
{
for(LL b=;b<wr;b++) // 同理
{
LL w=r*wr+b*wb;
if(w>c)
break;
LL big=(c-w)/(wr*wb);
LL cur=r*hr+b*hb+big*h_big;
if(cur>ans)
ans=cur;
}
}
cout<<"Case #"<<kase++<<": "<<ans<<endl;
}
return ;
}
C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6的更多相关文章
- Codeforces C - Om Nom and Candies
C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
C. Om Nom and Candies Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces 526C.Om Nom and Candies
题目描述 一个只有两个物品的背包问题,但是范围都是1e9,需要考虑根号或者log的复杂度. 如果这两个物品中的某一个花费超过了根号C,那么我们可以直接枚举这件物品的数量,另一件物品的数量可以计算得出. ...
- 【Henu ACM Round#19 E】 Om Nom and Candies
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 紫书上的原题: 链接 [代码] #include <bits/stdc++.h> #define ll long lon ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 526.D Om Nom and Necklace
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codefoces 436 B. Om Nom and Spiders
纯属练习JAVA.... B. Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes ...
随机推荐
- 第二百零九天 how can I 坚持
上班感觉好空虚啊. 今天感觉也没有什么,只是感觉上班的时候闲了一天,下班的时候就来事了. 确实没什么,只是上班的时候突然感觉好失落. 不该胡扯,朱镕基,言必行. 还有中国高铁谈判的新闻,中国确实是个比 ...
- 如何判断ios设备是否是高清屏幕
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2f) { CGRect winRect = [[UIScreen m ...
- CMake编译linux C++
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...
- (转载)ETL利器Kettle实战应用解析系列一【Kettle使用介绍】
http://www.cnblogs.com/limengqiang/archive/2013/01/16/kettleapply1.html ETL利器Kettle实战应用解析系列一[Kettle使 ...
- labview 中的一些简写全称
MAX:Measurement & Automation Explorer 测量自动化管理器 :可用于配置DAQ通道名称,VISA资源名称和IVI逻辑名称. DAQ: Device Data ...
- HDU1712简单的分组背包
HDU1712http://acm.hdu.edu.cn/showproblem.php?pid=1712 简单的分组背包 #include <map> #include <set& ...
- UVaLive 6805 Pantun Grader (恶心的模拟)
题意:给定 n 首诗,然后让你给他打分,有四种规则,第一种是按音节打,如果音节在8-12之间,就加10,第二种就是按押韵打,如果13,24一个押韵就加20分. 第三种按音节数打,13,24有一个一样的 ...
- C# 浅拷贝与深拷贝
浅拷贝:给对象拷贝一份新的对象引用地址:(只是给一个对象多起了个名字,所以,当改变拷贝的某个属性的时候,原对象的对应属性亦会改变).浅拷贝的定义—— 只对值类型(或string)类型分配新的内存地址: ...
- android Intent的startActivityForResult()方法
startActivityForResult() 之前学习了利用Intent跳转页面的同时传值,但有的时候需要从跳转到的页面返回所需要的值(如修改了用户信息,需要返回修改的信息),通俗的意思就是A.A ...
- ActionBar PopuMenu
PopupMenu popupmenu可以非常方便得实现在指定view下弹出一个菜单,实现类似ActionBar中的效果. public void showPopupMenu(View view){ ...