个人心得: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

  1. 3
  2. 17 8 4 2 0 0 0 0 0 0 0
  3. 100 99 0 0 0 0 0 0 0 0 0
  4. 2015 9 8 7 6 5 4 3 2 1 0

Sample Output

  1. 9
  2. -1
  3. 36
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<iomanip>
  6. #include<algorithm>
  7. using namespace std;
  8. const int maxn=;
  9. long long ans=maxn;
  10. int number[];
  11. int next[]={,,,,,,,,,};
  12. void dfs(int i,long long sum,long long t){
  13. int next[]={,,,,,,,,,};
  14. if(sum==){
  15. ans=min(ans,t);
  16. return ;
  17. }
  18. if(i<) return;
  19. long long c=min((long long )number[i],sum/next[i]);
  20. dfs(i-,sum-c*next[i],t+c);
  21. if(c>)
  22. {
  23. c--;
  24. dfs(i-,sum-c*next[i],t+c);
  25. }
  26. }
  27. int main()
  28. {
  29.  
  30. int t;
  31. cin>>t;
  32. int next[]={,,,,,,,,,};
  33. while(t--){
  34. long long p,q=;
  35. ans=maxn;
  36. int sum=;
  37. cin>>p;
  38. for(int i=;i<;i++){
  39. cin>>number[i];
  40. q+=number[i]*next[i];
  41. sum+=number[i];
  42. }
  43. long long s=q-p;
  44. if(s<)
  45. {
  46. cout<<-<<endl;
  47. continue;
  48. }
  49. dfs(,s,);
  50. if(ans==maxn)
  51. cout<<-<<endl;
  52. else cout<<sum-ans<<endl;
  53. }
  54. return ;
  55. }
  1.  

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. 实现对第三方应用任意SO注入

    实现对第三方应用任意SO注入 0x01 应用在Android中运行,从外部对该进程可以进行任意SO文件动态注入,就是应用动态运行我们的SO文件 0x02 基本的逻辑是: 1.    获取目标进程的pi ...

  2. iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期

    1. 属性 在开发过程中经常要用到定义属性,@property和@synthesize是经常用到的属性, property在.h文件中作声明,@synthesize在.m文件中用于实现 // Stud ...

  3. samtools的基本用法

    1.sam,bam的格式转换: $samtools view -sb file.sam >file.bam $samtools view -sb file.sam -o file.bam #sa ...

  4. mini2440移植uboot 2014.04(七)

    上一篇博文:<mini2440移植uboot 2014.04(六)> 代码已经上传到github上: https://github.com/qiaoyuguo/u-boot-2014.04 ...

  5. 记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)

    1) 方案一,  使用Web Service  基础功能没问题, 只是在连接https (ssh) 网站时, 需要针对https进行开发 (即http 和https 生成两套接口, 不太容易统一 ). ...

  6. 增强织梦DedeCMS“更新系统缓存”清理沉余缓存的功能

    我们使用织梦DedeCMS系统有很长一段时间后,不间断的在后台更新系统缓存的时候,有些缓存文件夹及缓存文件没有被清理,导致日积月累的垃圾缓存文件越来越多,可以以百千万计算,现在增强更新系统缓存功能清理 ...

  7. 将本地jar包制作成maven依赖

    首先,需要配置maven环境,如果没配置,传送门:http://www.cnblogs.com/hyyq/p/6557683.html 然后,需要一个ben地的jar包,这里以阿里云支付宝sdk为例, ...

  8. phpPgAdmin (win)配置安装及远程访问

    phpPgAdmin (win)配置安装 [1]     通过PostgreSQL的Application Stack Builder配置安装phpPgAdmin 1.确保PostgreSQL安装并正 ...

  9. Android框架之路——GreenDao3.2.2的使用

    一.简介 GreenDAO是一个开源的安卓ORM框架,能够使SQLite数据库的开发再次变得有趣.它减轻开发人员处理低级数据库需求,同时节省开发时间. SQLite是一个令人敬畏的内嵌的关系数据库,编 ...

  10. DOM初体验(绑定事件,监听事件)

    JavaScript的组成: ECMAScript(js的基本语法).DOM(文档对象模型).BOM(浏览器对象模型) DOM的作用: 1. 找到页面上的元素 2. 增添.删除.修改页面上的元素 3. ...