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;
}

最新文章

  1. 日志时间格式有s,ms,us,如何排序最大10行
  2. jQuery 循环问题
  3. css 伪类::after ::beftor 的使用方式
  4. js获取当前时间,js时间函数
  5. 什么是mata标签
  6. Google reCAPTCHA 人机身份验证
  7. Text Document Analysis CodeForces - 723B
  8. java体系结构与工作方式 《深入分析java web 技术内幕》第七章
  9. shell 获得后台进程返回值
  10. chrome 和IE 上传的文件,在net 后台取值Request.Form.Files[0].FileName 的不同
  11. 蜕变成蝶~Linux设备驱动之中断与定时器
  12. 5.Dubbo原理解析-代理之Javassist字节码技术生成代理 (转)
  13. Flex+BlazeDS+java通信详细笔记2-推送
  14. 20172306《Java程序设计与数据结构》第十周学习总结
  15. OC Foundation框架—结构体
  16. Vue2.5 Web App 项目搭建 (TypeScript版)
  17. 如何创建 SVN 服务器,并搭建自己的 SVN 仓库
  18. 把IP字符串转换为IPv4标准格式
  19. codeforces 549F Yura and Developers(分治、启发式合并)
  20. ruby 可枚举模块Enumerable

热门文章

  1. 关于jqeury中attr()和prop()方法
  2. check_mk 之 Configuration variables
  3. ansible使用8-Best Practices
  4. 来自SaberSama的HTML总结
  5. 实现vmare虚拟机系统随主机开机自动启动
  6. April 12 2017 Week 15 Wednesday
  7. SAP成都C4C小李探花:浅谈Fiori Design Guidelines
  8. 百度非会员满速下载利器(IDM)Internet Download Manager v6.30.8 中文特别版
  9. linq 和 lmabda 表达式 的用法 和优劣 转自 农码一生
  10. 【洛谷P1939】 矩阵加速模板