题目传送门

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

思路:首先,将大于等于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. 记录一次从txt文件导入数据的python下的MySQL实现

    环境: python2.7 ComsenzXP自带MySQL 安装python-MySQL模块 数据格式:txt格式的账号信息. 数据一行一条数据. 难点:有的行只有账号,没有密码:有的为空行:有的行 ...

  2. eclipse 中使用 GreenUML 和 AmasterasUML 自动生成类图

    Green UML和AmaterasUML 两种 一.安装方法: 1.都是先安装GEF 通过eclipse-> install new software安装GEF的网址: http://down ...

  3. checked多选,取消,反选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. MyBatis使用动态代理报 invalid bound statement (not found) 错

    这个问题网上大部分都说xml文件中的路径不对 或者是resources之类的问题,如果那些文章的解决方案解决不了你的问题的话,可以看一下我遇到的这种情况: 前提: mybatis-config.xml ...

  5. 猪羊——HTML解析

    HTML标签和属性大全见:http://www.cnblogs.com/Mr-liyang/p/5797976.html CSS样式大全:http://www.cnblogs.com/Mr-liyan ...

  6. iOS 添加Empty Application模板

    在Apple最新的XCode6.x中没有了Empty Application模板,好在XCode可以添加模板,而且可以自定义模板. 首先可以到XCode5.x中复制 Empty Application ...

  7. php学习笔记-include

    这个和C语言中的include是同样的用法,如果一个php文件中有一句include a.php,那么执行的时候就会把a.php的内容插入到这个php文件中. 举个例子,我现在有一个名为hellowo ...

  8. python3-打印一个进度条

    # Auther: Aaron Fan import sys,time for i in range(30): #打印一个#号,这种方法打印不会自动换行 sys.stdout.write('#') # ...

  9. JAVA and JAVA WEB with TOMCAT and ECLIPSE 学习过程中遇到的字符乱码问题及解决方法汇总(随时补充)

    JAVA语言具有跨平台,unicode字符集编码的特点. 但是在开发过程中处理数据时涉及到的字符编码问题零零散散,尤其是处理中文字符时一不留神就可能出现一堆奇奇怪怪的符号,俗称乱码. 对于乱码,究其原 ...

  10. ubuntu nvidia驱动+cuda9.0

    https://blog.csdn.net/fdqw_sph/article/details/78745375