poj3040(双向贪心)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1540 | Accepted: 637 |
Description
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
* 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
Sample Input
3 6
10 1
1 100
5 120
Sample Output
111
Hint
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(双向贪心)的更多相关文章
- poj-3040 Allowance (贪心)
http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie c元,问最多能 ...
- hdu 6860 Fluctuation Limit 双向贪心
题意: 给你n个区间[li,ri],和一个整数k,你从每一个区间内选出来一个数,把从第i个区间内选出来数放在第i个位置,这样会构成一个长度为n的序列,你需要保证序列中任意两个相邻的数之差的绝对值要小于 ...
- 【贪心算法】POJ-3040 局部最优到全局最优
一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...
- POJ3040贪心
题意:作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1≤N≤20)不同的面额,每枚硬币是所有比他小的硬币面值的倍数,例如1美分硬币.5美分硬币. ...
- poj3040 发工资(贪心)
题目传送门 题目大意:给一个人发工资,给出不同数量不同面额,(大面额一定是小面额的倍数),问最多能发几天,(每天实发工资>=应发工资). 思路:首先,将大于等于c的面额的钱直接每个星期给奶牛一张 ...
- hdu4915 Parenthese sequence 贪心O(n)解法(new)
hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 2480 贪心+简单并查集
Steal the Treasure Time Limit: 10000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- NOIP2012疫情控制(二分答案+倍增+贪心)
Description H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境 ...
随机推荐
- CountDownLatch和CyclicBarrier 的用法
CountDownLatch是减计数方式,计数==0时释放所有等待的线程:CyclicBarrier是加计数方式,计数达到构造方法中参数指定的值时释放所有等待的线程.CountDownLatch当计数 ...
- C/C++程序编译运行过程分析(转载)
为了减轻使用机器语言编程的痛苦,人们进行了一种有益的改进:用一些简洁的英文字母.符号串来替代一个特定的指令的二进制串,比如,用“A D D”代表加法,“M O V”代表数据传递等等,这样一来,人们很容 ...
- AC日记——图灵机游戏 codevs 2292
2292 图灵机游戏 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description [Shadow 1]第二题 ...
- AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- 51 Nod 1238 最小公倍数之和 V3 杜教筛
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...
- Java 实现随机验证码
许多系统的注册.登录或者发布信息模块都添加的随机码功能,就是为了避免自动注册程序或者自动发布程序的使用. 验证码实际上就是随机选择一些字符以图片的形式展现在页面上,如果进行提交操作的同时需要将图片上的 ...
- free如何知道释放内存长度:vs与glibc分配内存时编译器内部处理
鉴于网上这个资料实在太少,将以前整理过却未完全的一篇文章贴出来,希望大牛指正vs下内存管理方式.可联系gaoshiqiang1987@163.com vs分配内存 vs没有源码,编译器在分配内存时,分 ...
- luogu P1510 精卫填海
题目描述 [版权说明] 本题为改编题. [问题描述] 发鸠之山,其上多柘木.有鸟焉,其状如乌,文首,白喙,赤足,名曰精卫,其名自詨.是炎帝之少女,名曰女娃.女娃游于东海,溺而不返,故为精卫.常衔西山之 ...
- 【APIO2016】Gap
题目描述 有 $N$ 个严格递增的非负整数 $a_1, a_2, \dots, a_N$($0 \leq a_1 < a_2 < \cdots < a_N \leq 10^{18}$ ...
- ORACLE MOS 翻译
http://blog.csdn.net/msdnchina/article/details/53174196