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. JVM内存的那些事

    前言 对于C语言开发的程序员来说,在内存管理方面,必须负责每一个对象的生命周期,从有到无. 对于Java程序员你来说,在虚拟机内存管理的帮助下,不需要为每个new对象都匹配free操作,内存泄露和内存 ...

  2. 转】MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3544208.html 感谢! 一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制 ...

  3. c# 邮件发送代码分享

    /// <summary> /// 发送邮件方法 /// </summary> /// <param name="sendMail">发送人&l ...

  4. Controlling GameObjects Using Components

    [Accessing Components] The most common case is where a script needs access to other Components attac ...

  5. int* V.S. int[]

    [int* V.S. int[]] 在C++中,int[]有2种形态,一种是指针形态,即使用方法和int*一样,另一种是符号形态,即只是一个编译期的符号(意味着在runtime期,所定义的int[]根 ...

  6. 【Android框架进阶〖0〗】ThinkAndroid注解机制

    由于项目需要,开始研究ThinkAndroid. 个人认为该框架的注解机制十分新颖,所以先研究这个,顺便学习下 Java 的annotation. 粗略的看了看,该机制在BaseActivity中初始 ...

  7. WEB数据挖掘(十六)——Aperture数据抽取(9):数据源

    One of the central concepts of Aperture is the notion of a DataSource. A DataSource contains all inf ...

  8. Uncaught TypeError: Object #<Object> has no method 'fancybox'

    Uncaught TypeError: Object #<Object> has no method 'fancybox' 2011-10-24 16:51:19|  分类: html|举 ...

  9. if condition volist

    <table class="table table-hover table-striped"> <if condition="$order_list e ...

  10. Android ImageView scaleType属性

    scaleType属性 文章来源:http://blog.csdn.net/xilibi2003/article/details/6628668 使用ImageView时经常会用到scaleType属 ...