题目传送门

题目大意:给一个人发工资,给出不同数量不同面额,(大面额一定是小面额的倍数),问最多能发几天,(每天实发工资>=应发工资)。

思路:首先,将大于等于c的面额的钱直接每个星期给奶牛一张,将面额大于等于c的前去除。然后从大到小开始选择,要选择的金额尽可能的接近c,如果刚好能够凑足c就作为当前的一种方案,如果不能凑足c那就再从小的开始选,要选出一种的总额不少于c但尽量接近c作为当前的方案,然后计算如果按照如此方案最多可以给奶牛多少周,然后按照相同的方法再选方案,一直选到选择的金额不能凑足c为止。

为什么这样做是对的呢?

题目中说大面值一定是小面值的倍数,所以,如果一个数字可以用大面值凑,那必定可以用小面值凑,也就是说,小面值的功能比大面值强。我们在能选择大面值的时候,尽量选择大面值。在这个过程中我们一直保证m<=c,但如果我们不能使m<=c,即即便我们用了最小的面额也会超值,那就只能浪费超过的这一部分了,并且此时浪费的是最少的。(如果先选小的再选大的,浪费的肯定大于等于此时的情况)

代码。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
struct dian {
int val,to;
} a[30];
bool cmp(dian a,dian b) {
return a.val<b.val;
}
int make[30];
int main() {
int n,c;
cin>>n>>c;
for(int i=1; i<=n; i++) {
cin>>a[i].val>>a[i].to;
}
sort(a+1,a+1+n,cmp);
int ans=0;
for(int i=n; i>0; i--) {
if(a[i].val>=c) {
ans+=a[i].to;
n--;
} else break;
}
bool flag;
while(1) {
flag=false;
memset(make,0,sizeof(make));//make表示 某一种货币在一种工资组合中的使用次数
int tepc=c;
int i;
for(i=n; i>0; i--) {
if(a[i].to) {
int m,num;
num=tepc/a[i].val;
m=min(num,a[i].to);//尽可能的多用,但不能超过总数
tepc-=m*a[i].val;
make[i]=m;
if(tepc==0) { //此处只会>=0
flag=true;
break;
}
}
}
if(tepc>0) {
for(i=1; i<=n; i++) {
if(a[i].to>make[i]) {//make里面存的是当前这一轮用过的 除了用过的 还有剩下的 才可以用这种货币
while(make[i]<a[i].to) {
tepc-=a[i].val;
make[i]++;
if(tepc<0) {//此处不会=0 因为=0的情况上面已经排除掉了
flag=true;
break;
}
}
}
if(flag)break;
}
}
if (!flag) break;
int cnt=0x3f3f3f3f;
for(int i=1;i<=n;i++){
if(make[i])
cnt=min(cnt,a[i].to/make[i]);//最小的组合
}
ans+=cnt;
for(int i=1;i<=n;i++){
a[i].to-=make[i]*cnt;
}
}
cout<<ans<<endl;
}
Allowance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5167   Accepted: 2032

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.

poj3040 发工资(贪心)的更多相关文章

  1. (简单贪心) 发工资咯:) hdu2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  3. 贪心--HDU 2021 发工资咯

    Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵,但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就 ...

  4. POJ3040给奶牛发工资

    题意:       有n种硬币,每种硬币有mi个,然后让你给奶牛发工资,每周发至少c元(就是不找零钱的意思)然后问你能发几周?(硬币之间都是倍数关系) 思路:       这个题目做了两天,丢脸,看完 ...

  5. HDU 2021 发工资咯:)(最水贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2021 发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDOJ2021发工资咯:)

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. HDOJ 2021 发工资咯:)(利用了一种取余的思想)

    Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处 ...

  8. python实现发工资脚本

    好开心啊,在旁边大神的帮助下,终于实现了发工资的python脚本,之前用shell写的老出错,刚才测试,发80个人工资详情,妥妥的,代码如下: from email.mime.multipart im ...

  9. HDU 2021 发工资咯:)

    http://acm.hdu.edu.cn/showproblem.php?pid=2021 Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的 ...

随机推荐

  1. http协议基础教程

    引言 HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/ ...

  2. 向vsftp服务器上传文件报“550 Permission denied”错误的解决办法

    上传文件: ftp> mput db.iso 550 Permission denied 原因:vsftp默认配置不允许上传文件. 解决:修改/etc/vsftpd.conf 将“write_e ...

  3. Android ScrollView 内部控件 layout_margin失效的解决方法

    在<ScrollView> 的<LinearLayout  >属性里面加入android:layout_gravity="top" <LinearLa ...

  4. sql中IN的用法

    1.和where配合使用 IN操作符允许我们在where的子句中规定多个值 SELECT * FROM Persons  WHERE LastName IN ('Adams','Carter') 这句 ...

  5. 常见地图服务(WMS、WFS、WCS、TMS、WMTS

    1.网络地图服务(WMS) 网络地图服务(WMS)利用具有地理空间位置信息的数据制作地图.其中将地图定义为地理数据可视的表现.能够根据用户的请求返回相应的地图(包括PNG,GIF,JPEG等栅格形式或 ...

  6. GUI编程02

    1 编写一个导航栏 from tkinter import * root = Tk() root.title("测试") root.geometry("400x400+4 ...

  7. 业务逻辑:shiro框架的功能实现

    思路:分别在web.xml配置过滤器以及在applicationContext.xml去配置 实现步骤:1.在pom.xml里引入shiro的坐标 2.在web.xml里配置shiro过滤器 3.在a ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...

  9. GCD 学习(一)简介

    文章摘抄至:http://zhuyanfeng.com/archives/3015 并有一些改动 GCD(Grand Central Dispatch)是从OS X Snow Leopard和iOS ...

  10. loj10131 暗的连锁

    传送门 分析 首先我们知道如果在一棵树上加一条边一定会构成一个环,而删掉环上任意一条边都不改变连通性.我们把这一性质扩展到这个题上不难发现如果一条树边不在任意一个新边构成的环里则删掉这条边之后可以删掉 ...