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. kuangbin_UnionFind C (HDU 1213)

    过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...

  2. 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a

    现在的网页应用越来越丰富,我们在网页中填写日期和时间已经再也不用手动输入了,而是使用各种各样的日期时间选择控件,大部分样式华丽的日期选择和日历控件都是基于jQuery和HTML5的,比如今天要分享的这 ...

  3. Java static block static constructor , static field

    http://stackoverflow.com/questions/7121213/singleton-instantiation http://docs.oracle.com/javase/spe ...

  4. Python-属性(property)

    在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]]) fget:获取属性 fset ...

  5. mysql学习之-三种安装方式与版本介绍

    MYSQL版本介绍 mysql分alpha,beta,rc,GA四个版本. alpha  暗示这是一个以展示新特性为目的的版本,存在比较多的不稳定因素,还会向代码中添加新新特性beta 以后的beta ...

  6. 开放平台-web实现人人网第三方登录

    应用场景     web应用通过人人网登录授权实现第三方登录.   操作步骤     1  注册成为人人网开放平台开发者         http://app.renren.com/developer ...

  7. wikioi 1430 素数判定

    /*====================================================================== 题目描述 Description 质数又称素数.指在一 ...

  8. Zookeeper工作原理一

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  9. JavaScript闭包演示

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. PIP安装Python的scipy,scrapy等包出现“failed building wheel for xxx”问题解决办法

    1.在这里下载对应的.whl文件,注意别改文件名! http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml Ctrl + F,输入lxml,找到下面这段 Lxm ...