个人心得:10月份月赛题目,很low,就过了一道水题而且是把所有猜测都提交才过的。这段时间不知道忙什么去了,

也没怎么刷题感觉自己越来越差,还不如新来的大一学弟呢,别人起码天天刷代码到半夜,比起刚在区域赛拿银的学长们,

溜了溜了。慢慢磨练吧。

题目心得:这题意思很简单,就是用最多的硬币去负款,我比赛时是从正面开始的,发现自己low一是这样麻烦而且漏洞多,

后面看了题解他是从后面开始的。

要用尽量多的硬币凑P元钱,假设我现在硬币总共的面值和为M,那么换个思路,我现在要从总共的硬币中拿走尽量少的硬币,使剩下的硬币为P元。

那么问题转化成了,用尽量少的硬币去凑出M−P元,这个题目就有点熟悉了,不过这里有两个特殊的地方,一个是每种硬币都有数量限制,一个是面值大的硬币并不是面值小的硬币的整数倍。

假设没有数量限制,那么就是简单的贪心,尽量用面值大的硬币去凑出M−P元就好了。但是这个题用这个思路的话,会有这种情况。

假设现在有20,20,20,50这四个硬币,要去凑60元,如果以之前的思路贪心的话,那么是无解的,但是这里可以不用50元这个硬币,而用3个20元的去凑出60元。

那么对于这种情况,就要尝试一下,对于每种硬币,去掉一个这种硬币后的解。

题目:

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 pp dollars from me. To make your wallet lighter, you decide to pay exactly pp dollars by as many coins and/or banknotes as possible.

For example, if p=17p=17 and you have two $10$10 coins, four $5$5 coins, and eight $1$1coins, you will pay it by two $5$5 coins and seven $1$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.

InputThe first line contains an integer TT 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,c2000p,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 cici means how many coins/banknotes in denominations of ii dollars in your wallet.

1≤T≤200001≤T≤20000 
0≤p≤1090≤p≤109 
0≤ci≤1000000≤ci≤100000OutputFor each test case, please output the maximum number of coins and/or banknotes he can pay for exactly pp dollars in a line. If you cannot pay for exactly pp 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
const int maxn=;
long long ans=maxn;
int number[];
int next[]={,,,,,,,,,};
void dfs(int i,long long sum,long long t){
int next[]={,,,,,,,,,};
if(sum==){
ans=min(ans,t);
return ;
}
if(i<) return;
long long c=min((long long )number[i],sum/next[i]);
dfs(i-,sum-c*next[i],t+c);
if(c>)
{
c--;
dfs(i-,sum-c*next[i],t+c);
}
}
int main()
{ int t;
cin>>t;
int next[]={,,,,,,,,,};
while(t--){
long long p,q=;
ans=maxn;
int sum=;
cin>>p;
for(int i=;i<;i++){
cin>>number[i];
q+=number[i]*next[i];
sum+=number[i];
}
long long s=q-p;
if(s<)
{
cout<<-<<endl;
continue;
}
dfs(,s,);
if(ans==maxn)
cout<<-<<endl;
else cout<<sum-ans<<endl;
}
return ;
}
												

Too Rich(贪心加搜索)的更多相关文章

  1. codeforces733-C. Epidemic in Monstropolis 贪心加链表

    题意 现在有一个怪兽序列a[i],权值大的怪兽可以吃权值小的怪兽,吃完之后权值大的怪兽的权值会变成两者权值的和,相邻的怪兽才能吃 吃完之后,位置合并,队列前移,从左到右重新编号,重复这一过程, 然后给 ...

  2. 【HDOJ6351】Beautiful Now(贪心,搜索)

    题意:给定一个数字n,最多可以交换其两个数位k次,求交换后的最大值与最小值,最小值不能有前导0 n,k<=1e9 思路: 当k>=n的位数时只需要无脑排序 k<n时有一个显然的贪心是 ...

  3. 5.12 省选模拟赛 T2 贪心 dp 搜索 差分

    LINK:T2 这题感觉很套路 但是不会写. 区间操作 显然直接使用dp不太行 直接爆搜也不太行复杂度太高. 容易想到差分 由于使得整个序列都为0 那么第一个数也要i差分前一个数 强行加一个0 然后 ...

  4. 【代码笔记】iOS-scrollerView里多个tableView加搜索框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "customCell.h&qu ...

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

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

  6. easyUI分页实现加搜索功能

    前台页面: js代码: ps:pagination为true时会在table下面加上easyUI的分页. load函数会将查询值传给datagrid并传给后台重新加载. DAO.xml为: 后台代码实 ...

  7. web实现下拉列表多选加搜索

    实现如图所示的下拉多选还能带有搜索功能. <!DOCTYPE html> <html> <head> <title></title> < ...

  8. F. Gourmet and Banquet(贪心加二分求值)

    题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...

  9. HDU 5527 Too Rich 贪心

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

随机推荐

  1. 【LeetCode】【矩阵旋转】Rotate Image

    描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  2. Python学习进程(8)字符串內建函数

        Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持.     (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...

  3. OpenGL学习进程(5)第三课:视口与裁剪区域

    本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域:     (1)知识点引入:     1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...

  4. vue(组件、路由)懒加载

    const Login = resolve => require(['@/components/Login'], resolve) //就不用import了 Vue.use(Router) le ...

  5. mysql sql的执行顺序

    转:http://blog.csdn.net/u014044812/article/details/51004754 关于sql和MySQL的语句执行顺序(必看!!!) 原创 2016年03月29日 ...

  6. java 程序的使用

    Java程序可以在任何安装有Java平台的系统上运行,运行的时候语法如下: java -jar <program.jar>   -jar这个参数是必须有的,后面跟你的java程序,例如我们 ...

  7. R中的运算符,条件语句,控制语句

    1.运算符 算术运算符:+,-,*,/ 关系运算符:==,!=,>,>=,<,<= 逻辑运算符:&,|,&&,||,! &和|称为短逻辑符,&a ...

  8. Yii技巧大全(摘录)

    Yii技巧大全(摘录) db组件 'schemaCachingDuration'=>3600, 为什么不起做用? 需要开缓存 如何在页面下边显示sql的查询时间 在log组件的routes中加入 ...

  9. Eclipse Validating缓慢的优化

    使用Eclipse的人基本都有这种情况,如图: 各种等待有木有,各种崩溃啊有木有,反正我是觉得挺烦的,但是也不知道是干嘛的,如果取消了,造成程序出问题,就是给自己找麻烦,我知道这个事情肯定是可以关的, ...

  10. 【bzoj4401】块的计数(水dfs)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4401 假设把树划分为x个节点作一块,那么显然只有当x|n的时候才可能存在划分方案,并且这 ...