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. Parquet列式存储格式

    Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的版本是1.8.0. 列式存储 列式存 ...

  2. vSphere client 登陆ESXI主机“您无权登录次服务器”

    vCenter安装在虚拟机上,安装好后想调整下内存,直接把虚拟机关闭了电源,突然一想服务器都被我关了,还拿什么修改内存,完蛋! 突然想起,在使用vCenter之前,都是用vsphere client ...

  3. Linux命令详解-Apache网站服务器配置和管理

    1.Apache网站服务器配置和管理 1.源码包安装 2.rpm包安装 rpm –a | grep httpd 3.启动服务 service httpd start 4.配置文件: /etc/http ...

  4. 小朋友学Java(2):Win 7安装JDK

    1 打开命令行窗口,输入java -version. 若提示不认识java命令,说明没有java环境.   1.png 2 从甲骨文网站(http://www.oracle.com/technetwo ...

  5. 关于oracle数据库压力测试

    今天接到需求,需要对oracle数据库进行压力测试,就这几个字的需求. 然后查看了以下软件: 1.Benchmark Factory是一款专业的服务器性能测试工具,专为数据库测试和可扩展性测量而设计, ...

  6. 开发组件:Supervisor

    Supervisor安装与配置(Linux/Unix进程管理工具) https://blog.csdn.net/xyang81/article/details/51555473 https://www ...

  7. extends和implements区别

    extends与implements的不同 1.在类的声明中,通过关键字extends来创建一个类的子类. 一个类通过关键字implements声明自己使用一个或者多个接口. extends 是继承某 ...

  8. Linux命令行下如何终止当前程序

    Linux命令行下如何终止当前程序 快捷键: Ctrl+c 在命令行下起着终止当前执行程序的作用, Ctrl+d 相当于exit命令,退出当前shell Ctrl+s 挂起当前shell(保护作用很明 ...

  9. selenium+python自动化95-弹出框死活定位不到

    前言 部分小伙伴学了selenium的alert后,就不管啥弹出框都去用alert,这是行不通的,看到弹出框,先要确定是不是alert,是才能用,不是的话肯定不能用. 有些弹出框是div层,这种跟平常 ...

  10. python实现列表页数据的批量抓取练手练手的

    python实现列表页数据的批量抓取,练手的,下回带分页的 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import B ...