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. 目前用到最全的datagrid(easyui)

    包含checkbox.复合表头.多行可编辑单元格.combobox单元格,就差上次做的table中每行中的关联检索combobox单元格了.目前已修改为单行编辑,多行编辑时的check有问题 $(&q ...

  2. 使用seajs来引入js代码

    注意的是:引入jquery的代码最好放在html文件中,本文是为了说明seajs中require如何使用的,才将jquery放入seajs中的. html中对应的代码: <script type ...

  3. 利用KMeans聚类进行航空公司客户价值分析

    准确的客户分类的结果是企业优化营销资源的重要依据,本文利用了航空公司的部分数据,利用Kmeans聚类方法,对航空公司的客户进行了分类,来识别出不同的客户群体,从来发现有用的客户,从而对不同价值的客户类 ...

  4. httpd-2.2

    http://httpd.apache.org/docs/2.2/logs.html httpd.conf文件 Configuration and logfile names: If the file ...

  5. php编程安全指南

    php编程安全指南1.一般 1)lamp系统安全设置 2)php.ini安全设置 3)使用MVC框架 2.数据传输 1)在$_GET的$_POST,$_COOKIE,和$_REQUEST中,消毒和验证 ...

  6. 【转】windows7的桌面右键菜单的“新建”子菜单,在注册表哪个位置,如何在“新建"里面添加一个新项

    点击桌面,就会弹出菜单,然后在“新建”中就又弹出可以新建的子菜单栏.office与txt 的新建都是在这里面的.我想做的事情是:在右键菜单的“新建” 中添加一个“TQ文本”的新建项,然后点击它之后,桌 ...

  7. java.lang.NoSuchMethodError: 属于jar包冲突

    今天测试接口,出错:java.lang.NoSuchMethodError: org.apache.http.entity.mime.content.StringBody.<init>(L ...

  8. myBatis抛出异常Result Maps collection already contains value ...

    BaseResultMap 是自动生成的,非说已经包含了,NM! 删除tomcat下的 D:\apache-tomcat-7.0.52\webapps\chp-approve\WEB-INF\clas ...

  9. 图片上传,图片剪切jquery.imgareaselect

    ---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  10. Android SDK Manager 更新失败的解决方法

    Android SDK Manager 更新失败的解决方法 原文地址 最近使用Android SDK Manager 更新Android SDK tools 发现经常更新失败,获取不到更新信息: Fe ...