hdu 3348 coins(贪心)

coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1080    Accepted Submission(s): 309

Problem Description

"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)

"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn‘t like to get the change,
that is, he will give the bookseller exactly P Jiao.

Input

T(T<=100) in the first line, indicating the case number.

T lines with 6 integers each:

P a1 a5 a10 a50 a100

ai means number of i-Jiao banknotes.

All integers are smaller than 1000000.

Output

Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can‘t buy the book with no change, output "-1 -1".

Sample Input

3
33 6 6 6 6 6
10 10 10 10 10 10
11 0 1 20 20 20

Sample Output

6 9
1 10
-1 -1

Author

madfrog

Source

HDU2010省赛集训队选拔赛(校内赛)

Recommend

lcy   |   We have carefully selected several similar problems for you:  3349 3350 3347 3346 3345

题意:给你P a1 a5 a10 a50 a100 ,ai代表i角的硬币有ai个,问凑成P最少,最多的硬币个数。

题解:

最少直接从大到小取,最大就是不断的把面值大的转换成小的。渣渣只会暴力。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

#define N 1010
#define ll long long

using namespace std;

int s;
int a[7];///面值为c[i]的硬币剩余的个数为a[i]个
int b[7];///面值为c[i]的硬币用了b[i]个
int c[7]= {0,1,5,10,50,100};///面值

int main() {
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        scanf("%d",&s);
        for(int i=1; i<=5; i++) {
            scanf("%d",&a[i]);
        }
        int x=s;
        int Min=0,Max=0;
        memset(b,0,sizeof b);
        for(int i=5; i>=1; i--) {
            if(x>=c[i]) {
                int num=x/c[i];
                if(num>=a[i])
                    num=a[i];
                Min+=num;
                a[i]-=num;
                b[i]=num;
                x-=num*c[i];
            }
        }
        if(x!=0)Min=-1;
        if(Min==-1) {
            printf("-1 -1\n");
            continue;
        }
        Max=Min;
        ///求最大值就是大的尽量转换成小的
        while(1) {
            int flag=1; ///标记是否有转换
            for(int i=5; i>1; i--) {
                if(b[i]) {///用的个数
                    for(int j=i-1; j>=1; j--) {
                        if(b[i]==0)
                            break;
                            //转换
                        if(a[j]*c[j]>=c[i]) {
                            int x=a[j]*c[j]/c[i];
                            if(x>=b[i]) {
                                Max=Max-b[i]+c[i]*b[i]/c[j];
                                b[j]+=c[i]*b[i]/c[j];
                                a[i]+=b[i];
                                a[j]=a[j]-c[i]*b[i]/c[j];
                                b[i]=0;
                                flag=0;
                            } else {
                                Max=Max-x+c[i]*x/c[j];
                                b[j]+=c[i]*x/c[j];
                                a[i]+=x;
                                a[j]=a[j]-c[i]*x/c[j];
                                b[i]-=x;
                                flag=0;
                            }
                        }
                    }
                }
            }
            if(flag)
               break;
        }
        printf("%d %d\n",Min,Max);
    }
    return 0;
}
时间: 2024-10-13 16:14:40

hdu 3348 coins(贪心)的相关文章

hdu 3348 coins

这道题算是一道很经典的题,很好的诠释了贪心和动态规划的不同功能.求最少钱的数量用贪心就够了,但是求最多钱的数量要用到动态规划的思想,每步都尽量保留最大 数量.具体看程序注解: #include"iostream" #include"stdio.h" #include"algorithm" #include"string.h" #include"cmath" using namespace std; int

hdu 4869 Task(贪心)

题目链接:hdu 4869 Task 题目大意:有n台机器,m个任务,每个机器和任务都有有xi和yi,要求机器的xi,yi均大于等于任务的xi和yi才能执行任务.每台机器一天只能执行一个任务.要求完成的任务数尽量多,并且说金额尽量大.完成每个任务的金额为xi?500+yi?2 解题思路:贪心,mach[i][j]表示等级为i,时间为j的机器数量,task[i][j]表示等级为i,时间为j的机器数量.每次优先减少i,因为对应等级减少100,对应的金额代价也不会减少超过500(即时间减少1). 每次

HDU 4864 Task(贪心)

HDU 4864 Task 题目链接 题意:有一些机器和一些任务,都有时间和等级,机器能做任务的条件为时间等级都大于等于任务,并且一个任务只能被一个机器做,现在求最大能完成任务,并且保证金钱尽量多 思路:贪心,对于每个任务,时间大的优先去匹配,时间相同的,等级大的优先去匹配,因为时间占得多,时间多1就多500,而等级最多才差200.然后匹配的时候,尽量使用等级小的去匹配,而时间只要大于它的都可以用,因为是按时间优先,所以如果该时间能匹配大的,其他肯定也能匹配,那么肯定优先匹配大的,所以只要在等级

HDU 4811 Ball(贪心)

2014-05-15 22:02 by Jeff Li 前言 系列文章:[传送门] 马上快要期末考试了,为了学点什么.就准备这系列的博客,记录复习的成果. 正文-计数  概率 概率论研究随机事件.它源于赌徒的研究.即使是今天,概率论也常用于赌博.随机事件的结果是否只凭运气呢?高明的赌徒发现了赌博中的规律.尽管我无法预知事件的具体结果,但我可以了解每种结果出现的可能性.这是概率论的核心. "概率"到底是什么?这在数学上还有争议."频率派"认为概率是重复尝试多次,某种结

HDU 4912 LCA+贪心

Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 531    Accepted Submission(s): 182 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n. Th

HDU 2844 Coins (动规)

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6904    Accepted Submission(s): 2809 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

HDU 2844 Coins

Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(witho

HDU 1661 Assigments 贪心法题解

Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each type has N tasks. Each task of type A needs xi time to finish, and each task of type B needs yj time to finish, now, you, as the boss of the factory, n

2014多校第一场D题 || HDU 4864 Task (贪心)

题目链接 题意 : 用N台机器,M个任务,每台机器都有一个最大工作时间和等级,每个任务有一个需要工作时间和一个等级.如果机器完成一个任务要求是:机器的工作时间要大于等于任务的时间,机器的等级要大于等于任务的等级.一台机器只能完成一个任务,一个任务只能被一台机器完成.每个机器完成一个任务公司能够获得500*xi+2*yi (此处xy都是指被完成的任务的).输出所有机器能完成的最多任务数,和最大盈利. 思路 :贪心,自己做的时候想了各种排序都不对,没有考虑到500*xi+2*yi 这个公式的重要性.