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国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境 ...
随机推荐
- [luoguP1963] [NOI2009]变换序列(二分图最大匹配)
传送门 根据公式xjb推一下,然后就可以连边. 考虑到字典序最小,和匈牙利算法的实现过程,要倒序匹配. #include <cmath> #include <cstdio> # ...
- docker的通俗理解
自己买了个服务器,前不久搭建好的一个网站,想要再搞一个站点,无奈只能修改端口后,再部署另外一个站点.繁琐的配置运行环境,迁移网站,是否让你感觉到很繁琐?服务器不想用了,想搬迁到另外一台服务器去部署,先 ...
- net6:创建Membership对象数据源的代码
原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] 添加了一个db的类作为了对象数据源: using System;using System.Data;using S ...
- 标准C程序设计七---107
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Mac下Android SDK更新不了的解决办法
在hosts文件中加入: 203.208.46.146 dl.google.com 203.208.46.146 dl-ssl.google.com
- js-百度地图多个标注点
以成都华誉口腔3个门诊部为例,锦江区-青羊区-高新区 html: <div class = "demo_main"> <fieldset class = &quo ...
- 从壹开始 [ Ids4实战 ] 之三║ 详解授权持久化 & 用户数据迁移
回顾 哈喽大家周三好,今天终于又重新开启 IdentityServer4 的落地教程了,不多说,既然开始了,就要努力做好
- 域名缓存侦测(DNS Cache Snooping)技术
域名缓存侦测(DNS Cache Snooping)技术 在企业网络中,通常都会配置DNS服务器,为网络内的主机提供域名解析服务.这些DNS不仅解析自己的私有域名,还会用递归方式,请求公网的DNS解析 ...
- ruti
也许我可以说除了我把+号写成了-号这个程序几乎是完美的
- arch 安装
xfce参考 http://my.oschina.net/u/1408707/blog/182581#OSC_h2_6 chm阅读 – chmsee作为一个苦逼的码农,要忍受各种chm文件的蹂躏,这个 ...