POJ:3040-Allowance(贪心好题)
Allowance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4903 Accepted: 1943
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.
解题新的:
- 题意就是你有很多张面额不同的纸币,你每个星期要给奶牛至少c元,问你用现在的钱最多给奶牛多少周。
- 这个题的感觉就是贪心,想了两三种方案感觉都不太对,后来发现这真的是很好的一个题,首先,将大于等于c的面额的钱直接每个星期给奶牛一张,将面额大于等于c的前去除,然后从大到小开始选择,要选择的金额尽可能的接近c,如果刚好能够凑足c就作为当前的一种方案,如果不能凑足c那就再从小的开始选,要选出一种的总额不少于c但尽量接近c作为当前的方案,然后计算如果按照如此方案最多可以给奶牛多少周,然后按照相同的方法再选方案,一直选到选择的金额不能凑足c为止。
- 为啥可以用这中方法,首先,这个题的数据量给你很大的提示,纸币的面额最多20种,然后排除起它比较简单的贪心思维,简单的从大到小,从小到大什么的,然后将从大到小,从小到大结合起来。
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 25;
struct MONEY {
ll va,num;
}m[maxn];
ll ans = 0,n,c;
ll use[maxn];
bool cmp(MONEY a,MONEY b) {
return a.va < b.va;
}
void init() {
scanf("%d%d",&n,&c);
for(int i=0;i<n;i++)
scanf("%d%d",&m[i].va,&m[i].num);
sort(m,m+n,cmp);
}
int main() {
init();
bool flag;
for(int i=n-1;i>=0;i--) {
if(m[i].va >= c) {
ans += m[i].num;
m[i].num = 0;
}
}
while(true) {
memset(use,0,sizeof(use));
flag = false;
ll temp_c = c,M;
for(int i=n-1;i>=0;i--) {
if(m[i].num) {
ll num = temp_c / m[i].va;
M = min(num,m[i].num);
temp_c -= M*m[i].va;
use[i] = M;
if(temp_c == 0) {
flag = true;
break;
}
}
}
if(temp_c > 0) {
for(int i=0;i<n;i++) {
if(m[i].num > use[i]) {
while(use[i] < m[i].num) {
temp_c -= m[i].va;
use[i]++;
if(temp_c < 0) {
flag = true;
break;
}
}
}
if(flag)
break;
}
}
if(!flag)
break;
ll cnt = 0x7f7f7f7f;
for(int i=0;i<n;i++) {
if(use[i])
cnt = min(cnt,m[i].num/use[i]);
}
ans += cnt;
for(int i=0;i<n;i++)
m[i].num -= use[i]*cnt;
}
printf("%d\n",ans);
return 0;
}
POJ:3040-Allowance(贪心好题)的更多相关文章
- POJ 3040 Allowance 贪心
这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...
- POJ 3040 Allowance【贪心】
POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...
- poj 3040 Allowance
Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1842 Accepted: 763 Descript ...
- POJ 3040 贪心
贪心好题 ---. 思路: 从大到小凑C 如果不够 再从小到大补满(超过)C //By SiriusRen #include <cstdio> #include <cstring&g ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 1488 Tex Quotes --- 水题
POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- 【贪心】Allowance POJ 3040
题目链接:http://poj.org/problem?id=3040 题目大意:你有n种不同面值的硬币,面值为vi的有bi个."硬币的面额均匀地分配下一个更大的面额",即下一个更 ...
- 【POJ - 3040】Allowance(贪心)
Allowance 原文是English,这里就放Chinese了 Descriptions: 作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1 ...
随机推荐
- SQLServer查询语句收集
常用的SQLServer查询语句,有空可以多练习一下,增加记忆,可以提高工作效率! 1.数据操作 Select --从数据库表中检索数据行和列Insert --向数据库表添加新数据 ...
- 从刷票了解如何获得客户端IP
我的个人博客,源地址:http://www.woniubi.cn/get_client_ip/ 前两个星期帮一个朋友的亲戚的孩子刷票,谁让咱们是程序员呢.这当中也遇到过重装系统,除灰尘,淘宝购物,盗Q ...
- lucene中Field简介
Lucene 6.1.0中存在的field种类如下(后缀是Field): 下面介绍几个常用的Field类型: TextField A field that is indexed and tokeniz ...
- selenium代理
selenium.KeyDown("id=ctaskName", "d"); selenium.KeyPress("id=cta ...
- easyUI 节点树选择
定义: <input id="treeFFatherId" name="treeFFatherId" value="" style=& ...
- centos命令行常用操作
1.查看某个端口占用 lsof -i tcp:80 2.查看是否安装了防火墙 service iptables status 查看是否启用防火墙/etc/init.d/iptables status ...
- 打开excl链接时总是出现问题
主要现象:1.提示"发生了意外错误":2.报错"由于本机限制无法打开链接" 原因: 这个是由于默认浏览器异常造成的,就是比如你下载了新的浏览器,然后为默认浏览器 ...
- 笨办法学Python(零)
py走起!!! 习题0:准备工作 Windows平台 1. 用浏览器打开 http://learnpythonthehardway.org/exercise0.html 下载并安装 gedit 文本编 ...
- Linux远程桌面(二)
上一篇远程桌面采用的独立服务配置不适用于过多用户,这一篇采用超级Internet服务器搭建vnc服务可以解决多用户问题. vnc之xinetd服务搭建配置 Linux远程桌面(一):vnc之独立服务 ...
- 腾讯云、CloudXNS域名服务器
腾讯云 f1g1ns1.dnspod.net f1g1ns2.dnspod.net CloudXNS lv3ns1.ffdns.net lv3ns2.ffdns.net lv3ns3.ffdns.ne ...