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.


解题新的:

  1. 题意就是你有很多张面额不同的纸币,你每个星期要给奶牛至少c元,问你用现在的钱最多给奶牛多少周。
  2. 这个题的感觉就是贪心,想了两三种方案感觉都不太对,后来发现这真的是很好的一个题,首先,将大于等于c的面额的钱直接每个星期给奶牛一张,将面额大于等于c的前去除,然后从大到小开始选择,要选择的金额尽可能的接近c,如果刚好能够凑足c就作为当前的一种方案,如果不能凑足c那就再从小的开始选,要选出一种的总额不少于c但尽量接近c作为当前的方案,然后计算如果按照如此方案最多可以给奶牛多少周,然后按照相同的方法再选方案,一直选到选择的金额不能凑足c为止。
  3. 为啥可以用这中方法,首先,这个题的数据量给你很大的提示,纸币的面额最多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(贪心好题)的更多相关文章

  1. POJ 3040 Allowance 贪心

    这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...

  2. POJ 3040 Allowance【贪心】

    POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...

  3. poj 3040 Allowance

    Allowance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1842   Accepted: 763 Descript ...

  4. POJ 3040 贪心

    贪心好题 ---. 思路: 从大到小凑C 如果不够 再从小到大补满(超过)C //By SiriusRen #include <cstdio> #include <cstring&g ...

  5. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  6. POJ 1488 Tex Quotes --- 水题

    POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...

  7. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  8. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  9. 【贪心】Allowance POJ 3040

    题目链接:http://poj.org/problem?id=3040 题目大意:你有n种不同面值的硬币,面值为vi的有bi个."硬币的面额均匀地分配下一个更大的面额",即下一个更 ...

  10. 【POJ - 3040】Allowance(贪心)

    Allowance 原文是English,这里就放Chinese了 Descriptions: 作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1 ...

随机推荐

  1. pymongo模块

    import pymongo # 创建与MongoDB服务器的连接 mongoclient = pymongo.MongoClient(host='127.0.0.1', port=27017) # ...

  2. (开发)ESLint - 代码规范

    参考文档:http://eslint.cn/ ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 J ...

  3. 关于css实现单行、多行省略标记

    实现单行: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 实现多行: display: -webkit-box; -we ...

  4. DXperience Winforms新版本13.2功能预览

    据界面控件厂商 DevExpress 官方最新消息,大家期盼已久的DXperience 13.2终于要面世了.今天在这里提前跟大家提前披露一下DXperience Winforms 13.2的一些精彩 ...

  5. arm寄存器解析

    寒假闲来无事准备将自己的走过的arm之路总结一下,今天就先从arm的寄存器说起吧,欢迎各位拍砖. 要介绍arm寄存器之前我们要先了解一下arm处理器的工作模式: Arm处理器有七种工作模式,为的是形成 ...

  6. Spark job执行流程消息图

    Spark job执行流程消息图 1.介绍

  7. 单步调试理解webpack里通过require加载nodejs原生模块实现原理

    在webpack和nodejs里,我们经常使用require函数加载原生模块或者开发人员自定义的模块. 原生模块的加载,比如: const path = require("path" ...

  8. mongodb在C#的连接以及curd写法

    连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...

  9. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  10. TCP与虚连接

    http://bbs.csdn.net/topics/390262738 在TCP通信时,会建立一个从源端到目的端的虚拟连接.感觉这种连接类似电路交换,只是这种连接是虚拟存在的.发送的报文都应该是沿着 ...