C. Om Nom and Candies

无线超大背包问题

 #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的更多相关文章

  1. Codeforces C - Om Nom and Candies

    C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 526C.Om Nom and Candies

    题目描述 一个只有两个物品的背包问题,但是范围都是1e9,需要考虑根号或者log的复杂度. 如果这两个物品中的某一个花费超过了根号C,那么我们可以直接枚举这件物品的数量,另一件物品的数量可以计算得出. ...

  5. 【Henu ACM Round#19 E】 Om Nom and Candies

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 紫书上的原题: 链接 [代码] #include <bits/stdc++.h> #define ll long lon ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 转】MyEclipse使用总结——MyEclipse文件查找技巧

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3781749.html 感谢! 一.查找文件 使用快捷键[ctrl+shift+R]弹出弹出文件查找框,如下图所示: ...

  2. ESP8266 TCP传输AT指令顺序

    ); //复位 ret = ESP8266_Cmd ( );//测试AT启动 ret = ESP8266_Cmd ( );//选择WIFI应用模式softAP+station //ret = ESP8 ...

  3. oracle存储过程返回数据集结果

    MSSQL的存储过程返回数据集好简单,直接SELECT 就可以. ORACLE的存储过程返回数据集必须通过游标. 创建ORACLE存储过程 create or replace procedure cx ...

  4. 使用aspose.word两句代码将word转换为pdf

    //Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...

  5. Linux下用arptables防arp攻击

    Linux下网络层防火墙iptables很强大,链路层也有类似的防火墙arptables,可针对arp地址进行限制,防止ARP网关欺骗攻击,再配合静态绑定MAC和向网关报告正确的本机MAC地址,有效解 ...

  6. hdu 5407

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题意:给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...,C(n,n)) 根 ...

  7. SecureCRT上传、下载文件(使用sz与rz命令)

    1.简述 借助securtCRT,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器. 2.理解记忆 其中,对于sz和rz的理解与记忆我用了如下的方法 ...

  8. sqlserver锁表、解锁、查看锁表

    sqlserver锁表.解锁.查看锁表 http://www.cnblogs.com/zfanlong1314/p/3698566.html http://www.cnblogs.com/chjf20 ...

  9. erlang: Programming Rules and Conventions。

    http://www.erlang.se/doc/programming_rules.shtml#HDR33 http://www.erlang.org/eeps/eep-0008.html

  10. android自定义相册 支持低端机不内存溢出

    1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...