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. 实体框架 (EF) 入门 => 二、在全新的数据库中使用 Code First

    学习资料:http://msdn.microsoft.com/zh-cn/data/jj193542 视频建立的控制台应用程序,我在这里使用MVC. 一.非常有磁性的,非常优雅 很喜欢看这个人的视频, ...

  2. 安装CiscoWorks LMS

    安装CiscoWorks LMS,需要安装5张光盘,CiscoWorks LMS的安装步骤比较简单,在这里只介绍需要的重要步骤. 1.安装CD One 将CiscoWorks 2000 LMS CD ...

  3. easyui问题小记

    在easyui1.4.3版本中,(其他版本不知道是不是也是这样的),绑定在datagridview上面的数据最好不要是带有特殊符号的字段,不然会导致部分的属性不好用,比如这样的字段  START_DA ...

  4. hdoj 5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ----------- ...

  5. angular实践第一弹:选项卡开发

    在学习angular的过程中,实践是最好的方法. 在开发选项卡的过程中,不需要再像jquery一样以DOM操作为核心,那什么样的情况是以DOM操作为核心的Jquery的思想呢? 一想到改变什么,就想设 ...

  6. Mysql用户密码设置修改和权限分配

    我的mysql安装在c:\mysql 一.更改密码 第一种方式: 1.更改之前root没有密码的情况 c:\mysql\bin>mysqladmin -u root password " ...

  7. js 如何验证字符串里是否包含汉字?

    1.用正则表达式判断<input  type="text" id="name" placeholder="请输入用户名" value= ...

  8. Android开发之SQLite的使用方法

    前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...

  9. The Aggregate Magic Algorithms

    http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...

  10. NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused

    NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve:                          ...