zoukankan      html  css  js  c++  java
  • POJ 3040 Allowance

    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.

    解题报告:
    很无聊的题,贪心一下就好,原则是选择能超过C的最小搭配,那么显然对于本身价值就大于C的直接用掉就好,如果小于的话我们选择最小搭配
    考虑实现:
    我们要先用掉面额比较大的,拿它去搭配较小面额,所以我们能用多少就用多少,对于剩余的我们就用小面额的补上,然后找到循环节,一次性减掉即可,这样一次至少用掉一种面额,所以复杂度(O(N^2))

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=25;
    struct node{
    	int v,w;
    	bool operator <(const node &pp)const{
    		return v<pp.v;
    	}
    }a[N];
    ll ans=0;int r[N];
    void work()
    {
    	int n,c,lim=1e6;
    	scanf("%d%d",&n,&c);
    	for(int i=1;i<=n;i++){
    		scanf("%d%d",&a[i].v,&a[i].w);
    		if(a[i].v>=c){
    			ans+=a[i].w;
    			a[i].w=0;
    		}
    	}
    	sort(a+1,a+n+1);
    	int tot=0,tmp;
    	while(tot<=0){
    		tot=c;
    		for(int i=n;i>=1;i--){
    			tmp=Min(a[i].w,tot/a[i].v);
    			tot-=tmp*a[i].v;
    			r[i]=tmp;
    		}
    		if(tot){
    			for(int i=1;i<=n;i++){
    				if(a[i].w>r[i])tot-=a[i].v,r[i]++;
    				if(tot<=0)break;
    			}
    		}
    		if(tot>0)break;
    		tmp=lim;
    		for(int i=1;i<=n;i++)
    			if(r[i]>0)tmp=Min(tmp,a[i].w/r[i]);
    		for(int i=1;i<=n;i++){
    			if(r[i]>0)a[i].w-=tmp*r[i];
    			r[i]=0;
    		}
    		ans+=tmp;
    	}
    	printf("%lld
    ",ans);
    }
    
    int main()
    {
    	work();
    	return 0;
    }
    
    
  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7499208.html
Copyright © 2011-2022 走看看