个人心得: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. Linux 关机命令详解 转自脚本之家

    在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的. Linux centos重启命令: 1. ...

  2. 【leetcode刷题笔记】Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  3. Android 平台电容式触摸屏的驱动基本原理

    Android 平台电容式触摸屏的驱动基本原理 Android 平台电容式触摸屏硬件基本原理 Linux 与 Android 的多点触摸协议 Linux输入子系统:事件的编码

  4. this对象解析

    this在js中有着非常广泛的应用,但其所指的对象也常常让人摸不着头脑,简而言之: this指的就是调用函数的对象,最常见的莫过以下几种 1.直接使用函数,则为window对象 function a( ...

  5. POJ 1144 无向图求割点

     学长写的: #include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#in ...

  6. CodeWars上的JavaScript技巧积累

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice The sli ...

  7. java应用线上CPU过高问题排查

    1.top 命令,查看占用CPU最高的PID.ps aux|grep PID 进一步确定tomcat进程出现问题.2.ps -mp pid -o THREAD,tid,time显示线程列表3.prin ...

  8. UvaLive 5811 概率DP

    题意 : 有54张牌 问抽多少张牌能使每种花色都至少是给定的数字 两张王牌可以被选择为任何花色 高放学长真是太腻害辣! 设置dp[][][][][x][y] 前四维代表四种真的花色的数量 后两维xy代 ...

  9. python正则表达式同时匹配多个关键字(多关键字匹配)

    网上翻了很多文章...居然没有一个有用的..倒是找到一篇java的,但java的正则表达式和python的还有点不同. 那篇java的文章是用"[keywd1]|[keywod2]|[key ...

  10. 读写properties文件方法

    按key读取properties文件中的value public static String readSystemConfig(String key){ Properties prop = new P ...