Allowance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1540   Accepted: 637

Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly
divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute
the maximum number of weeks he can pay Bessie.

Input

* Line 1: Two space-separated integers: N and C



* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.

Output

* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance

Sample Input

3 6
10 1
1 100
5 120

Sample Output

111

Hint

INPUT DETAILS:

FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin.



OUTPUT DETAILS:

FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.

Source

题目得意思是,john要发硬币工资给他的奶牛。工资不低于c,他有n个硬币,币值和数目。

问你最多发多少个星期。

人云亦云啊,我也用双向贪心。哎。

从大到小排好序,从头到尾做一轮,再反过来做一轮。没有严谨证明,奇奇怪怪的感觉。

(可是我看到硬币就想到dp...)

/***********************************************************
> OS : Linux 3.13.0-24-generic (Mint-17)
> Author : yaolong
> Mail : dengyaolong@yeah.net
> Time : 2014年10月14日 星期二 09时59分40秒
**********************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
pair<int, int> a[25];
int use[25];
int n, m; int main()
{
while ( scanf ( "%d%d", &n, &m ) != EOF )
{
int i;
for ( i = 1; i <= n; i++ )
{
scanf ( "%d%d", &a[i].first, &a[i].second );
}
sort ( a+1, a + n+1 );
int res = 0;
while ( 1 )
{
memset ( use, 0, sizeof ( use ) );
int rest = m;
for ( i = n; i >= 1; i-- )
{
int tmp = min ( rest / a[i].first, a[i].second );
rest -= tmp * a[i].first;
use[i] = tmp;
}
if ( rest )
{
for ( i = 1; i <= n; i++ )
{
if ( a[i].second && a[i].first >= rest )
{
use[i]++;
rest = 0;
break;
}
}
}
if ( rest )
{
break;
}
int mmin = 0x7f7f7f7f;
for ( i = 1; i <= n; i++ )
{
if ( use[i] )
{
mmin = min ( mmin, a[i].second / use[i] );
}
}
res += mmin;
for ( i = 1; i <= n; i++ )
{
if ( use[i] )
{
a[i].second -= use[i] * mmin;
}
}
}
cout << res << endl;
}
}

poj3040(双向贪心)的更多相关文章

  1. poj-3040 Allowance (贪心)

    http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie   c元,问最多能 ...

  2. hdu 6860 Fluctuation Limit 双向贪心

    题意: 给你n个区间[li,ri],和一个整数k,你从每一个区间内选出来一个数,把从第i个区间内选出来数放在第i个位置,这样会构成一个长度为n的序列,你需要保证序列中任意两个相邻的数之差的绝对值要小于 ...

  3. 【贪心算法】POJ-3040 局部最优到全局最优

    一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...

  4. POJ3040贪心

    题意:作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1≤N≤20)不同的面额,每枚硬币是所有比他小的硬币面值的倍数,例如1美分硬币.5美分硬币. ...

  5. poj3040 发工资(贪心)

    题目传送门 题目大意:给一个人发工资,给出不同数量不同面额,(大面额一定是小面额的倍数),问最多能发几天,(每天实发工资>=应发工资). 思路:首先,将大于等于c的面额的钱直接每个星期给奶牛一张 ...

  6. hdu4915 Parenthese sequence 贪心O(n)解法(new)

    hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  7. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  8. hdu 2480 贪心+简单并查集

    Steal the Treasure Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. NOIP2012疫情控制(二分答案+倍增+贪心)

    Description H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境 ...

随机推荐

  1. 数据库操作之——key与index的区别

    mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的. 1 key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查 ...

  2. 解决v-for产生的警告的办法

    当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用 “就地复用” 策略.如果数据项的顺序被改变,Vue将不是移动 DOM 元素来匹配数据项的顺序, 而是简单复用此处每个元素,并且 ...

  3. 马士兵hadoop第五课:java开发Map/Reduce(转)

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  4. Linux内核解析之标准I/O库

    当Linux创建一个进程时,会自动创建3个文件描述符0,1,2,分别对应标准输入,标准输出,错误输出.C库中与文件描述符对应的是文件指针.查看C库头文件stdio.h中的源码 typedef stru ...

  5. 模块化开发(seajs)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. SYN攻击SYN Attack

    SYN攻击SYN Attack   SYN Attack是一种DOS攻击方式.它利用的是TCP协议的漏洞,攻击目标,使其不在响应网络请求.在TCP协议中,需要三次握手,才能建立TCP连接.在握手过程中 ...

  7. spring mvc 编写处理带参数的Controller

    在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeControl ...

  8. Markdown编辑器的使用

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/LoveJavaYDJ/article/details/73692917 一.Markdown和edi ...

  9. BT原理分析(转)

    BT种子文件结构分析,参考:http://www.cnblogs.com/EasonJim/p/6601047.html BT下载,参考:http://baike.baidu.com/item/BT下 ...

  10. nginx 自签名https

    繁杂的命令,以下准备写好的sh,拷贝https.sh文件,设置执行权限:chmod u+x https.sh #!/bin/sh # create self-signed server certifi ...