Too Rich

http://acm.hdu.edu.cn/showproblem.php?pid=5527

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

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元,我们可以反着想,用最少的硬币凑成剩下的钱,这样就转换成一道经典的贪心问题
但是要注意的是,大的硬币不一定是小的硬币的整数倍,所以需要用DFS搜索出最优解,具体看代码
 #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define maxn 50010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const long long INF=0x3f3f3f3f3f3f3f3f;
using namespace std;
long long coin[] = {,, , , , , , , , , };
long long a[];
long long ans; void dfs(int pos,long long sum,long long num){
if(sum==){
ans=min(ans,num);
return;
}
if(pos<){
return;
}
long long tmp=min(a[pos],sum/coin[pos]);
dfs(pos-,sum-tmp*coin[pos],num+tmp);
if(tmp>){//去掉一个硬币的情况
tmp--;
dfs(pos-,sum-tmp*coin[pos],num+tmp);
}
} int main(){
int t;
scanf("%d",&t);
while(t--){
int total=;
long long sum=;
for(int i=;i<;i++){
scanf("%lld",&a[i]);
sum+=coin[i]*a[i];
total+=a[i];
}
total-=a[];//a[0]表示p
if(sum<a[]){
puts("-1");
continue;
}
sum-=a[];
ans=INF;
dfs(,sum,);
if(ans==INF){
puts("-1");
}
else{
printf("%lld\n",total-ans);
}
}
}

Too Rich(贪心+DFS)的更多相关文章

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

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

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

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

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

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

  4. Too Rich HDU - 5527 (贪心+dfs)

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

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

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

  6. UVALive3902 Network[贪心 DFS&&BFS]

    UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...

  7. 【NOIP2003】传染病控制(-贪心/dfs)

    我自己yy了个贪心算法,在某oj 0msAC~.然后去wikioi提交,呵呵,原来是之前oj的数据太弱给我水过了,我晕. 我之前的想法是在这棵树上维护sum,然后按时间来割边,每一时刻割已经感染的人所 ...

  8. HDU 5802 Windows 10 (贪心+dfs)

    Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...

  9. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

随机推荐

  1. [UE4]为UStaticMeshComponent添加点击事件

    BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...

  2. storm项目架构分析

    storm是一条一条数据处理,spark是一批数据处理的,storm才是真正意义的实时数据处理. 1.fileBeat类似flume用来采集日志的,fileBeat是轻量级的,对性能消化不大,而flu ...

  3. 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力

    作者:Tony Peng 去年,OpenAI 的 1v1 AI 击败了世界顶尖选手 Dendi,OpenAI CTO Greg Brockman 承诺:明年,我们会带着 5v5 的 AI bot 重回 ...

  4. 分割List为指定size

    背景 老项目,用的原生的JDBC,获取连接,预编译...然后业务需要要更新很多条数据,我就写了条件为 ——IN()... 根据传入的 list 的 size 循环的给sql语句拼接上“ ? ”为了之后 ...

  5. Java调用Bat

    import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Input ...

  6. linux教程

    linux视频教程:尚观 http://www.uplinux.com/shipin/linuxyong-hu-guan-li-zhi-yong-hu-guan-li-01 一,linux开机(cen ...

  7. 1.获取服务器IP、端口等

    比如,页面内部有一个连接,完整的路径应该是 http://192.168.0.1:8080/myblog/authen/login.do 其中http://server/是服务器的基本路径,myblo ...

  8. 3. 修改myeclipse工作区间默认编码和jsp的默认编码

    1.windows - preferences - General - Workspace 2.windows - preferences - MyEclipse - Files and Editor ...

  9. springboot ssl http转Https

    参考:https://www.cnblogs.com/imfjj/p/9058443.html   (里面有坑) https://blog.csdn.net/l4642247/article/deta ...

  10. 11-11SQLserver基础--数据库之触发器

    触发器 意义:本质上就是一个特殊的存储过程,只不过不是通过exec来调用执行,而是通过增删改数据库中的操作来执行. 作用:1.将关联的表之间的数据增删改        2.触发器可以操作视图,在视图上 ...