HDU 5527---Too Rich(贪心+搜索)

题目链接

Problem Description

You are a rich person, and you think your wallet is too heavy and full now. So you want to give me some money by buying a lovely pusheen sticker which costs pdollars from me. To make your wallet lighter, you decide to pay exactly p dollars by as many coins and/or banknotes as possible.

For example, if p=17 and you have two $10 coins, four $5 coins, and eight $1 coins, you will pay it by two $5 coins and seven $1 coins. But this task is incredibly hard since you are too rich and the sticker is too expensive and pusheen is too lovely, please write a program to calculate the best solution.

Input

The first line contains an integer T indicating the total number of test cases. Each test case is a line with 11 integers p,c1,c5,c10,c20,c50,c100,c200,c500,c1000,c2000, specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. The number ci means how many coins/banknotes in denominations of i dollars in your wallet.

1≤T≤20000
0≤p≤109
0≤ci≤100000

Output

For each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. If you cannot pay for exactly p dollars, please simply output ‘-1‘.

Sample Input

3

17 8 4 2 0 0 0 0 0 0 0

100 99 0 0 0 0 0 0 0 0 0

2015 9 8 7 6 5 4 3 2 1 0

Sample Output

9

-1

36

题意:给了 p 表示要付的钱数,一个数列v[10],分别表示  1 ,5,10,20,50,100,200,500,1000,2000 元的钱币数量,求用尽量多的钱币刚好付清 p 元,输出钱币数。

思路:贪心,尽量用面值小的钱币去筹,但是很可能面值小的钱币不够,所以从大面值开始考虑。初始化一个前缀和sum[12],sum[i]表示v[1]~v[i]面值的钱币和,tmp=rest-sum[i-1],表示当前面值的钱币应该付多少,cn=tmp/v[i] ,即表示当前面值的钱币应该拿出多少张,如果tmp%v[i]!=0 ,那么cn++,因为小于v[i]的钱币无法筹出足够的钱;另外要对于P=50 钱币为 20,20,20,50 时,按照贪心策略3张20为60,所以不会取50,但是用3张20 无法筹出50元,所以必须每张面值的钱币应该多考虑一张,比如对于这样的数据:

p=1020   0 0  0 49  1   0   0   0    1     0;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int v[12]={0,1,5,10,20,50,100,200,500,1000,2000};
int c[12],sum[12];
int p,ans;
void dfs(int i,int rest,int count)
{
    if(rest<0) return ;
    if(i==0) {
         if(rest==0) ans=max(ans,count);
         return ;
    }
    int tmp=max(0,rest-sum[i-1]);
    int cn=tmp/v[i]+(tmp%v[i]!=0);
    if(cn<=c[i])  dfs(i-1,rest-cn*v[i],count+cn);
    cn++;
    if(cn<=c[i])  dfs(i-1,rest-cn*v[i],count+cn);
}
int main()
{
    ///cout << "Hello world!" << endl;
    int T; cin>>T;
    while(T--)
    {
        scanf("%d",&p);
        for(int i=1;i<=10;i++) scanf("%d",&c[i]);
        sum[0]=0;
        for(int i=1;i<=10;i++) sum[i]=sum[i-1]+v[i]*c[i];
        ans=-1;
        dfs(10,p,0);
        printf("%d\n",ans);
    }
    return 0;
}
///1020 0 0  0 49  1   0   0   0    1     0
时间: 2024-09-28 16:11:36

HDU 5527---Too Rich(贪心+搜索)的相关文章

HDU 2391 Filthy Rich(贪心)

Filthy Rich Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description They say that in Phrygia, the streets are paved with gold. You’re currently on vacation in Phrygia, and to your astonishment you discover that this i

2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich

Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 245    Accepted Submission(s): 76 Problem Description You are a rich person, and you think your wallet is too heavy and full now. So yo

HDU 4004 The Frog&#39;s Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4970 Killing Monsters 【搜索】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 题目大意:给你一些防御塔的位置和其攻击力,以及一些怪物的血量和位置,问走到终点还有几个怪物活着. 题目思路:最开始看题目的时候就是区间更新的过程觉得会花很多时间,然后用的树状数组,后来发现用的一个机智方法可以过,简单了很多. 区间更新的主要时间是花在塔的伤害,(L,R)D伤害上面,我们用一个sttack数组记录伤害,在attack[L]+=D,在attack[R]-=D,然后从前往后扫一遍,可

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

hdu 2391 Filthy Rich

简单dp 水一个 处理点的时候,第一行和第一列特殊处理: <span style="font-size:24px;">其余的w[i][j]=show(w[i-1][j-1],w[i-1][j],w[i][j-1])+s[i][j];</span> <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<al

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大