Too Rich

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1850    Accepted Submission(s): 480

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
 
Source
 
题意:给你一些不同面值的硬币,每种硬币都有一定的数量,求用尽可能多的硬币凑出P元钱,有可能凑不出
思路:首先按贪心的思想,用尽可能多的小面值钱币,前提是小面值钱币可以凑出当前需要的钱数,所以从大面值的开始决策,比如现在到第idx个面值的钱币,要凑x元,又用1~idx-1的钱币可以凑出的总金额为y元,那么当前面值我需要用(x - y) / c[i]个,当然如果这个值小于0,就不用当前面值的钱币,注意如果c[i]不能整除(x- y),则需要多用一个idx的钱币,因为剩下的钱不够,比如有 10 20 20 50 50,现在要凑110,110 - 10 - 20 - 20 = 60,50不能整除60,则就需要两个50的,因为只用一个50的话,剩下的凑不出60,还有一点要注意的是20不能整除50,200不能整除500,因此我们算个数的时候有时需要多加一个,比如20 20 20 50,现在要凑50,因为剩下3个20,一共可以凑60,按照贪心策略那个单独的50就不会被选了,因此这里需要强制选一个50,200和500同理
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = 1e5+;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int p,ans,c[];
int val[]={,,,,,,,,,,};
ll sum[]; void dfs(int rest,int idx,int cnt)
{
if(rest<)
return;
if(idx==)
{
if(rest==)
ans=max(ans,cnt);
return;
}
ll cur = max(rest-sum[idx-],(ll));
int curnum=cur/val[idx];
if(cur % val[idx])
curnum++;
if(curnum<=c[idx])
dfs(rest-curnum*val[idx],idx-,cnt+curnum);
curnum++;
if(curnum<=c[idx])
dfs(rest-curnum*val[idx],idx-,cnt+curnum);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(sum,,sizeof(sum));
ans=-;
scanf("%d",&p);
for(int i=;i<=;i++)
scanf("%d",&c[i]);
for(int i=;i<=;i++)
sum[i]=sum[i-]+(ll)(val[i]*c[i]);
dfs(p,,);
printf("%d\n",ans);
}
return ;
}

Too Rich HDU - 5527 (贪心+dfs)的更多相关文章

  1. HDU 5527 贪心

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)

    题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...

  3. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  4. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  5. hdu6060[贪心+dfs] 2017多校3

    /* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...

  6. HDU 5527:Too Rich(DFS+贪心)***

    题目链接 题意 给出p块钱,现在要用十种硬币凑出,每种硬币有c[i]个,问最多能用多少个硬币. 思路 首先确定,对于每个硬币就是能用小的替换就不用大的. 所以,可以先把硬币尽量用小的替换,如果小的不够 ...

  7. HDU 5527 Too Rich ( 15长春区域赛 A 、可贪心的凑硬币问题 )

    题目链接 题意 : 给出一些固定面值的硬币的数量.再给你一个总金额.问你最多能用多少硬币来刚好凑够这个金额.硬币数量和总金额都很大   分析 : 长春赛区的金牌题目 一开始认为除了做类似背包DP那样子 ...

  8. 【算法系列学习】HDU 5527 Too Rich贪心

    http://www.cnblogs.com/AOQNRMGYXLMV/p/4934747.html #include<iostream> #include<cstdio> # ...

  9. HDU 5527 Too Rich 贪心

    题意: 有\(10\)种面值为\(1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000\)的纸币,现在你要选最多的数量凑成\(p\)块钱. 分析: 同样分析问题的反面 ...

随机推荐

  1. Python之PIP安装

    Python有两个著名的包管理工具easy_install.py和pip.Python2.7的安装包中自带了easy_install.py,而pip需要手动安装.而在Python3.5之后都是默认安装 ...

  2. Django 03 模板路径、模板变量、常用的过滤器

    Django 03 模板路径.模板变量.常用的过滤器 一.模板路径 #1.在每个app下面添加一个templates文件 #2.在项目views.py里面第33行INSTALLED_APPS里面添加上 ...

  3. POJ 1556 E - The Doors

    题意:给定n堵墙,现在要你从(0,5)走去(10,5)的最短距离 思路:刚开始还想模拟,就是从(0,5)走,每次x向右一格,然后判断有没和线段相交就可以.但是它的们有可能是小数形式给出的,这样就GG了 ...

  4. Unity Destroy和DestroyImmediate

    Destroy(Object obj, float t = 0.0F); 删除一个游戏对象,组件或者资源. 物体obj现在被销毁或在指定了t时间过后销毁.如果obj是组件,它将从GameObject销 ...

  5. Mac开启自带的Apache服务器

    OSX版本10.13.6 1.开启 sudo apachectl start 2.关闭 sudo apachectl stop 3.重启 sudo apachectl restart 默认的Apach ...

  6. static修饰的类属性

    我看书上说:static成员总是唯一存在的,并且在多个对象之间互享. 因此想到,如果我在a.php中实例化了Person.class.php这个类,并给static $name赋值,那么在b.php中 ...

  7. serv-U 7以上版本pasv端口的设置及中文乱码问题

    利用serv-u架设ftp服务器已经是再常见不过了事情了,近日一朋友为图新鲜,弄了个7.4版本的新玩意儿,结果架设上去后,仅开了21端口,用LeapFtp在port模式下连接没问题,但是另一常见的cu ...

  8. WPF根据数据项获取条目控件的方法-ItemContainerGenerator

    一.方法: ContainerFromIndex:返回 ItemCollection 中指定索引处的项的容器. ContainerFromItem:返回与制定的项对应的容器(ComboxItem等条目 ...

  9. 菜鸟 学注册机编写之 “sha1”

    1. 首先运行程序随便输入用户与注册码如下图所示: 2.将程序载入OD, 下MessageBoxA函数断点, F9运行程序, 程序运行后随便输入用户名与注册码,点"OK"后断下,F ...

  10. Spring MVC中注解的简介

    参考网址:  https://blog.csdn.net/a67474506/article/details/46361195 @RequestMapping映射请求 SpringMVC 使用 @Re ...