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. 解决 Maven 项目中找不到 jdk 的 tools.jar 文件的办法(多数情况下适用)

    <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifact ...

  2. jQuery实现焦点图[兼容ie7+]

    HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...

  3. c语言函数指针的几种使用方式

    1.直接定义函数指针赋值并使用. #include <stdio.h> int max(int x, int y) { if (x > y) return x; else retur ...

  4. Arm启动流程解析

    谈到arm的启动流程不得不说的是bootloader,但是我这篇文章主要来谈谈arm启动流程的,所以bootloader只是跟大家简介一下就ok.这篇文章我会谈到以下内容: 1.bootloader简 ...

  5. 关于android项目的习惯

    编码使用UTF-8. 布局中多写style,常用字号颜色尺寸写进values对应文件 如中号 小号 大号 下部按钮颜色 上标题颜色 左边距,右边距,等. 任何文件类型通用名放在最前 如item_a;i ...

  6. Mvc过滤器的使用【转载】

    前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MVC(特指:Asp.Net MVC,以下皆同)项目中不想让MVC开发人员去关心和写类似身份验证,日志,异常,行为截取等这部分重复的代码 ...

  7. 安装office提示Office 16 Click-to-Run Extensibility Component

    今天安装office时,提示Office 16 Click-to-Run Extensibility Component或者Office 15 Click-to-Run Extensibility C ...

  8. Azure资源模板化部署,伦家不懒都不好意思了

    如果老板让你在云平台上部署一套系统,你准备怎么做? 嗯,估计得根据具体需求开通或创建一大堆东西:虚拟机.存储.数据库.虚拟网络……别急还没完,接着还要对这些东西的规模.配置等各方面调整和优化.一系列环 ...

  9. 高精度水题(POJ2109)

    题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...

  10. 前端JavaScript之DOM使用案例

    1.弹出框点击关闭 (这个例子关键自己创建标签以及属性,不是太好想啊,而且作用相对来数也不是太大) <!DOCTYPE html> <html> <head> &l ...