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】常用的jquery获取表单对象的属性与值

    [jquery]常用的jquery获取表单对象的属性与值 1.JQuery的概念 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...

  2. 黑马程序员——JAVA基础之 == 和equals区别

    java中 == 和equals区别: java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolea ...

  3. java编程之:按位与运算,等运算规则

    按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0;   0&1=0;    1&0=0;     1&1=1; 即:两位 ...

  4. ES questions

    Be Careful of Cardinality Numeric and date fields are indexed in such a way that ranges are efficien ...

  5. JSBinding / Gen Bindings

    Classes in JSBindingSettings.classes array will be exported to JavaScript. There are already many cl ...

  6. sql compact 使用EF无法更新的问题?

    1.问题一是表中没有主键会报一些莫名其妙的错误. 2.数据库文件被默认复制到了Debug/Release目录,实际调试或运行时发现原有数据库没有被更新.

  7. JSP 相关试题(五)

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definiti ...

  8. 现在流行什么 JS库/框架?

    现在大家最感兴趣的 JS 库和框架是什么? jQuery 91.5% Underscore 38.6% AngularJS 28.5% Backbone 18.6% React 15.7% Knock ...

  9. JAVA包命名规范

    学习Java的童鞋们都知道,Java的包.类.接口.方法.变量.常量:JavaEE的三层模型等都有一套约定俗成的命名规则. 我学习每种语言都会关注相应的命名规则,一则体现自己比较专业:二来方便后检查, ...

  10. DW(六):polybase访问Azure Blob Storage

    目录: 连接hadoop配置语法 配置hadoop连接 Pushdown配置 Create external tables for Azure blob storage 连接hadoop配置语法: g ...