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

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
 #include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct TT
{
int v,w;
}a[];
int need[];
bool cmp(TT m, TT n)
{
if(m.v>n.v) return true;
return false;
}
int main()
{
int n,i,k;
int value,aa,bb,ans;
while(~scanf("%d %d",&n,&value))
{
ans = ;
k = ;
for( i=;i<n;i++)
{
scanf("%d %d",&aa,&bb);//排除大数;
if( aa>= value)
{
ans = ans+bb;
}
else
{
a[k].v = aa;
a[k].w = bb;
k++;
}
}
//k = n;
//printf("sdfgsdf\n");
sort(a,a+k,cmp);
while()
{
memset(need,,sizeof(need));
int sum = value;
for(int i=; i<k; i++) // Õý×ÅÕÒ
{
int tmp = sum/a[i].v;
need[i] = min(a[i].w,tmp);
sum = sum - a[i].v*need[i];
}
if(sum>)
{
for(int i=k-;i>=;i--)
{
if(a[i].w && a[i].v>=sum)
{
need[i]++;
sum = ;
break;
}
}
}
if(sum>) break;
int s = 0x3f3f3f3f;
for(int i=;i<k;i++)
{
if(need[i])
s = min(s,a[i].w/need[i]);
}
ans = ans+s;
for(int i=;i<k;i++)
{
if(need[i])
a[i].w -= need[i]*s;
}
}
printf("%d\n",ans);
}
return ;
}

poj 3040 Allowance的更多相关文章

  1. POJ 3040 Allowance【贪心】

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

  2. POJ 3040 Allowance 贪心

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

  3. 【贪心】Allowance POJ 3040

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

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

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

  5. Greedy:Allowance(POJ 3040)

    零用钱大作战 题目大意:农夫和牛又搞新花样了,现在农夫想给Bessie每个星期都给一点零用钱,农夫有一堆面值的钱币,并且这个钱币都能被上一个钱币整除(1,5,10,50),并且钱币有一定数量,要你求最 ...

  6. POJ 3040 贪心

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

  7. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  8. [SinGuLaRiTy] 贪心题目复习

    [SinGuLaRiTy-1024] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 2709] 颜料 (Painter) 题目描述 ...

  9. poj-3040 Allowance (贪心)

    http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie   c元,问最多能 ...

随机推荐

  1. jQuery使用伪递归重复执行动画

    使用setInterval()来重复执行动画,会因为动画执行过程的时候,setInterval()的时间依然是在走的,所以会导致动画的调用时间不理想,因此只能使用递归来重复执行动画. // 首页LOG ...

  2. Linux驱动设计—— 中断与时钟

    中断和时钟技术可以提升驱动程序的效率 中断 中断在Linux中的实现 通常情况下,一个驱动程序只需要申请中断,并添加中断处理函数就可以了,中断的到达和中断函数的调用都是内核实现框架完成的.所以程序员只 ...

  3. c语言学习笔记

    为什么需要输出控制符: 1:  01组成的代码可以表示数据亦可以表示指令: 2:如果01组成的代码表示的是数据的话,那么同样的01代码组合以不同的输出格式输出就会有不同的输出结果.. %d   --- ...

  4. tar学习使用心得

    tar如何解压文件到指定的目录?#tar zxvf mysql.tar.gz -C /home/aaa 暂时用到这,所以学到这,日后再更新 近日用tar解压一个文件时发现这样的错误: # tar -z ...

  5. how to change the AlexNet into FCNs ?

    How to change the AlexNet into FCNs ? FCNs is a network that only contain convolution layers and no ...

  6. 使用四种框架分别实现百万websocket常连接的服务器

    著名的 C10K 问题提出的时候, 正是 2001 年.这篇文章可以说是高性能服务器开发的一个标志性文档,它讨论的就是单机为1万个连接提供服务这个问题,当时因为硬件和软件的**,单机1万还是一个非常值 ...

  7. 方正S4101g笔记本电脑搜不到无线网络

    方正S4101g这款笔记本的无线网卡有问题.不能识别高版本的WIFI信号.有时候链接上之后,就再也找不到无线网络信号了.有时候根本就找不到.   解决方法:把无线路由的发送模式和频率设置到最低.重启路 ...

  8. 021. asp.net两个DataSet数据集的合并

    protected void Page_Load(object sender, EventArgs e) { DataSet dsSource = new DataSet(); //创建源数据集 Da ...

  9. GridView 控制分页页码间距

    来源:http://auv2009.blog.163.com/blog/static/68858712200992793853431/ 技巧1:在分页区中改变当前页码的样式或高亮显示页码 一个简单的办 ...

  10. Pop Sequence

    题目来源:PTA02-线性结构3 Pop Sequence   (25分) Question:Given a stack which can keep M numbers at most. Push ...