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 ...
随机推荐
- 设置 text-align: center;line-height:height 居中无效
1.设置文字水平居中 内联元素(行内元素)使用: text-align: center: 使用后文字仍然没有居中 解决方法:设置width:100%: 块元素使用: margin: 0 auto; 2 ...
- Windows到Ubuntu免密登陆
Windows到Ubuntu免密登陆 首先检查C盘用户文件夹下是否有.ssh文件夹,同时检查该文件夹中是否有至少两个文件,一个是xxx_rsa和xxx_rsa.pub,一个是私钥文件一个是公钥文件. ...
- 跨平台图表控件TeeChart使用教程:将图表数据导出为XML格式
在开发者使用TeeChart进行开发的过程中,不管是在设计时或者运行时都可以使用的图表导出对话框将图表数据轻易地导出为XML格式: TeeChart最新版那下载地址 上图为TeeChart导出对话框的 ...
- selenium中Alter等弹出对话框的处理
昨天使用selenium做自动化测试,发现部分页面会弹出alert对话框,找了写资料,大概的意思就是要给弹出的对话框做出相应,不然,后续的处理会失败. _driver.SwitchTo().Alert ...
- oracle-插入到数据库中为日期
oracle中创建一个表,其中一个字段为date,当我们进行插入操作 create table xf_allsalestotal ( xf_txdate date not null, xf_store ...
- CFG的定义
最近在CMU上NLP,好吧 对于见了很多年的CFG(Context-Free Grammar)发现又搞不懂是什么了 教材上写的是: mathematical system for modeling c ...
- POJ-2002 Squares---绕点旋转+Hash
题目链接: https://vjudge.net/problem/POJ-2002 题目大意: 有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少. 相同的四个点,不同顺序构成的正方形视 ...
- 网格中的BFS,逆向(POJ2049)
题目链接:http://poj.org/problem?id=2049 解题报告: 网格中的BFS,最主要的是边界问题. 1.这里在左右,上下两个方向上,分别判断墙,和门,细节是,向上有t个墙,for ...
- 【[TJOI2018]碱基序列】
题目 为什么没人用\(SAM\)啊 我们先把原来的模式串建一遍\(SAM\),之后我们就可以求出\(SAM\)上每一个节点的\(|endpos|\)就可以知道每一个子串出现的次数了,也就是在模式串上的 ...
- 【转】startActivityForResult和setResult详解
startActivityForResult与startActivity的不同之处在于:1.startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startAct ...