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. IOs动画的那些事儿

    CoreAnimation介绍 1:Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Animatio ...

  2. c++函数学习-关于c++函数的林林总总

    本文是我在学习c++过程中的一些思考和总结,主要是c++中关于函数的林林总总.欢迎大家批评和指正,共同学习. os version: ubuntu 12.04 LTS gcc version: gcc ...

  3. 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)

    题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...

  4. 应用seajs 做了个向上滚动的demo

    目录结构式这样滴 sea sea-module jquery-1.10.2.min.js sea.js static css t.min.css img test test.min.js main.j ...

  5. jquery 同源跨域请求整理

    //同源ajax请求数据 function getData(url,paramjson,fn) { $.ajax({ type : "POST", //提交方式 url : url ...

  6. 26深入理解C指针之---不规则数组与指针

    一.不规则数组:每一行的列数不相等 1.复合字面量: 1).复合字面量是一种C构造 2).外形和数组声明差不多,写法与类型转换一样,(int[3]){10, 20, 30,} 3).将多个复合字面量可 ...

  7. configure: error: C++ preprocessor "/lib/cpp" fails sanity check

    configure: error: C++ preprocessor "/lib/cpp" fails sanity check 参考链接: error: C++ preproce ...

  8. hdu 5455(字符串处理)

    Fang Fang Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

  9. P1136 超车 归并排序 求逆序对个数

    这道题从看到它开始到做出来,已经过了快两周[因为第一次思路完全跑偏写的是暴力模拟想水过]: 题意是这样的:  jzabc除了对多米诺骨牌感兴趣外,对赛车也很感兴趣.上个周末他观看了一场赛车比赛.他总是 ...

  10. Linux环境下编译JDK

    环境准备 操作系统,ubuntu-14.04.6-desktop-amd64.iso,下载地址:http://59.80.44.100/releases.ubuntu.com/14.04/ubuntu ...