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. Linux系统文件名字体不同的颜色都代表什么

    Linux系统文件名字体不同的颜色都代表什么   在Linux中,文件的颜色都是有含义的.   其中, Linux中文件名颜色不同,代表文件类型不一样.   如下所示: www.2cto.com   ...

  2. Mac parallels desktop安装windows,linux

    前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件    ...

  3. OpenCV:初试牛刀-带滚动条的视频播放-2

    视频播放时点击窗口关闭按钮(即小叉号)关闭窗口 隐藏console控制台 使用VideoCapture和createTrackbar实现滚动条控制视频播放 #include<iostream&g ...

  4. Hibernate 一对一

    一对一关系映射: 一对一关系映射即关系双方都只含有对方的一个引用(eg:人和身份证) 一对一关系映射又分为唯一外键关联映射和主键关联映射 数据库表结构: 创建实体类: public class Per ...

  5. HTML|CSS之HTML常用标签

    知识内容: 1.标签 2.head内标签 3.body内常用标签 注:本人使用的HTML为HTML5 一.标签 1.标签格式 标签的语法: <标签名 属性1=“属性值1” 属性2=“属性值2”… ...

  6. multiprocess模块---进程---进程队列

    首先明白几个概念: 同步:做完一件事情,再做另外一件事情 异步:做一件事情的时候,可以再做另外一件事情 阻塞:recv  sleep accept input recvfrom 非阻塞:没有遇见上面这 ...

  7. selenium+python自动化92-多线程启动多个不同浏览器

    前言 如果想用多个浏览器跑同一套测试代码,driver=webdriver.Firefox()这里的driver就不能写死了,可以把浏览器名称参数化. 后续如果想实现多线程同时启动浏览器执行用例,用前 ...

  8. Flutter main future mirotask 的执行顺序

    下面这段代码的输出是什么? import 'dart:async'; main() { print('main #1 of 2'); scheduleMicrotask(() => print( ...

  9. aix-syslog

    收集网络内路由器的日志信息,同时把本地日志信息与路由器信息分开. /etc/syslog.conf我写成: ## 本地日志处理 *.notice;*.err;*.warn<tab>;< ...

  10. quartz 的简单使用

    0.依赖: <!-- 引入quartz对应的依赖 --> <dependency> <groupId>org.quartz-scheduler</groupI ...