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. 2、kvm基础常用命令操作

    KVM 虚拟机默认的配置文件在 /etc/libvirt/qemu 目录下,默认是以虚拟机名称命名的.xml文件,如下: root@xuedianhu:~# ls /etc/libvirt/qemu ...

  2. Eclipse(2019-03版本)汉化

    1.Eclipse IDE 版本 eclipse-jee-2019-03-R-win32-x86_64.zip 2.Eclipse 汉化包下载 babel-R0.16.0-photon.zip 3.解 ...

  3. Jmeter4.0----CSV Data Set Config_对输入参数进行参数化(14)

    1.说明 测试接口时,当请求参数不是固定的,每次请求对应的参数都是不同的,就需要对请求参数进行参数化. 2.步骤 第一步:准备数据 (以报告编制人员为例)首先创建.txt文本,自己编辑要参数化的内容 ...

  4. Spring Security方法级别授权使用介绍

    1.简介 简而言之,Spring Security支持方法级别的授权语义. 通常,我们可以通过限制哪些角色能够执行特定方法来保护我们的服务层 - 并使用专用的方法级安全测试支持对其进行测试. 在本文中 ...

  5. pat1062. Talent and Virtue (25)

    1062. Talent and Virtue (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Li Abou ...

  6. C# 几种数据类型转换方式

    1.(int)变量名[强制类型转换] 该转换方式主要用于数字类型之间的转换,从int类型向long,float,double,decimal 类型转换可以使用隐式转换,但从long型到int 就需要使 ...

  7. Java实例学习——企业进销存管理系统(1)

    Java实例学习——企业进销存管理系统(1) (本实例为书上实例,我所记录的是我的学习过程) 开始时间:2月12日 完成时间:暂未完成 2月12日—选择企业进销存管理系统 选择企业进销存管理系统这一实 ...

  8. 完美解决Android在listview添加checkbox实现单选多选操作问题

    在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上看上去只是改变checkbox那么简单,然而实际开发中,实现起来并不是那么得心应手.尤其当 ...

  9. nodejs的会话总结

    前言: http是一个无状态协议,所以客户端每次发出请求时,下一次请求就无法得知上一次请求所包含的状态数据,那么如何能把一个用户的状态数据关联起来?1.cookie 一开始,人们采用cookie这门技 ...

  10. 配置Ubuntu DNS

    首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址,这是简单使用本机的静态查询.要访问Ubuntu DNS 服务器来进行查询,需要设置/etc/resolv.conf文件 ...