Too Rich

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1410    Accepted Submission(s): 362

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枚举纸币张数爆搜肯定会tle
  • 所以往贪心的思想考虑
  • 朴素的想法是对于p从小面额的凑到大面额的纸币
  • 但是这道题中纸币的面额如果以倍数为关系建图可以发现(20,50)和(200,500)会分别从10和100的节点分叉,但是其他节点均可表示比当前节点大的所有面额的纸币
  • 这个特性意味着用小面额20或200有凑不出50和500以及其奇数倍数值的情况
  • 反思之前提到的朴素的贪心想法
  • 如果凑到最后出现凑的数额不等于p
  • 则一定是最后加的当前最大面额导致的
  • 补救的办法应是用之前使用的小面额纸币凑当前导致超范围的纸币的面额
  • 那么如之前所述
  • 10种纸币的面额不是一种很好的结构
  • 不能保证小面额纸币一定可以凑成任意大面额纸币
  • 就存在不可补救的情况存在
  • 而且我们没法保证当前凑数的结果是正确的凑数结果
  • 那有没有补救的办法呢?
  • 还是有滴呀
  • 只要把纸币面额更改成最优结构即可
  • 就是每次使用两张50或500,就把这种面额的纸币算是取消了
  • 之后就剩下8种面额的纸币,而且都满足可以凑成任意比当前面额大的纸币的条件
  • 剩下的就是讨论50和500两种纸币使用张数奇偶性的问题了
  • 4种情况,在贪心前加上就行
  • 高中数学老师讲得好啊!“正难则反”
  • 这个题就可以反向思维,只要凑出纸币剩余面额的最小张数,减一下就是结果啊!
  • 如果正向考虑,应该是贪心凑数,最后对于多出来的数值用小面额先代替大面额之后打补丁
  • 好吧,很麻烦
  • 但是反向来做,不用代替和打补丁,就是直接用最大面值来凑,不存在打补丁的问题
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int c[]={,,,,,,,,,,};
int a[], b[], T, ans, tot;
LL p, sum;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
while(T--){
sum=;
tot=;
ans=inf;
scanf("%lld",&p);
for(int i=;i<;i++){
scanf("%d",&a[i]);
tot+=a[i];
sum+=a[i]*c[i];
}
if(sum<p){
printf("-1\n");
continue;
}
p=sum-p;
memcpy(b,a,sizeof(a));
for(int i=;i<;i++)
for(int j=;j<;j++)
if(i<=a[] && j<=a[]){
b[]=a[]-i;
b[]=a[]-j;
LL t=p-i*c[]-j*c[];
int cnt=i+j;
for(int k=;k>&&t>;k--){
int del=min(b[k],(int)t/c[k]);
if((k==||k==)&&(del&))
del--;
t-=del*c[k];
cnt+=del;
}
if(t==)
ans=min(ans,cnt);
}
if(ans==inf)
ans=-;
else
ans=tot-ans;
printf("%d\n",ans);
}
}
return ;
}

HDU_5527_Too Rich的更多相关文章

  1. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  2. PeopleSoft Rich Text Boxes上定制Tool Bars

      在使用PT8.50或在8.51时,你可能遇到过Rich-text编辑框.该插件使你能够格式化文本,添加颜色.链接.图片等等.下面是效果图: 如果页面中只有这么一个字段,该文本框就会有足够的空间来容 ...

  3. selenium处理rich text(富文本框)

    WordPress 的 rich  text 采用js,先让selenium切换到iframe中 driver.switchTo().frame("content_ifr"); 然 ...

  4. 8 ways rich people view the world differently than the average person

    Self-made millionaire Steve Siebold spent 26 years interviewing some of the wealthiest people in the ...

  5. 2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. 【转】深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格

    深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格 作者: 字体:[增加 减小] 类型:转载   本篇文章是对Android中在同一个TextView中设置不同 ...

  7. 目标检测--Rich feature hierarchies for accurate object detection and semantic segmentation(CVPR 2014)

    Rich feature hierarchies for accurate object detection and semantic segmentation 作者: Ross Girshick J ...

  8. Rich Text Editor for MVC

    在网站开发中难免会用到富文本编辑器,本文将介绍一款富文本编辑器(在线HTML编辑器) Rich Text Editor ,简要说明一下其在MVC中的使用. 具体使用情况和下载地址请参考:http:// ...

  9. WWDC 2016: Rich Notifications in iOS 10

    Notifications have gotten more than a visual refresh in iOS 10. As part of the new UserNotifications ...

随机推荐

  1. LPC(Low Pin Count) 与SIO(Super IO)

    记录bios学习的点点滴滴,虽然已经学了很长时间才发出来,但就当是温故而知新吧,由于水平有限,难免存在错误,望指正,同时感谢CSDN提供的平台. 1.LPC 定义:​ Intel所定义的PC接口,将以 ...

  2. 3、Cocos2dx 3.0游戏开发找小三之搭建开发环境

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27107295 搭建开发环境 使用 Cocos2d- ...

  3. basic_string

    // Components for manipulating sequences of characters -*- C++ -*- // Copyright (C) 1997, 1998, 1999 ...

  4. 【转】搞清楚LzoCodec和LzopCodec

    使用LZO过程会发现它有两种压缩编码可以使用,即LzoCodec和LzopCodec,下面说说它们区别: LzoCodec比LzopCodec更快, LzopCodec为了兼容LZOP程序添加了如 b ...

  5. SQL查询优化联合索引 与 单一列的索引

    目前WEB的普及太快,在实际的开发中,一旦遇到大数据量的时候就需要做到优化,让查询的更快,才能给客户更好的体验,也能够在程序上避免timeout. 部分转载自:https://www.cnblogs. ...

  6. [转]VC传递消息sendmessage

    SendMessage的基本结构如下: SendMessage( HWND hWnd,  //消息传递的目标窗口或线程的句柄. UINT Msg, //消息类别(这里可以是一些系统消息,也可以是自己定 ...

  7. kettle两表内链接的查询结果与sql语句的查询结果不符合?

    1.教师表输入 2.学生表 查 3.学生表中查出的教师id进行排序 5.教师表中查出的同样也对教师的id进行排序 6.进行左连接 总结: 进行连接的时候的关键是同样对教师的id进行先排序

  8. datagrid columns

    columns: [[ { field: 'Source_Id', title: 'Source_Id', hidden: true }, //{ field: 'Current_Value', hi ...

  9. jQuery && jEasyUI 扩展功能集合

    jquery-extensions:jQuery && jEasyUI 扩展功能集合 该扩展功能基于 jQuery 1.9.x / 1.10.x / 1.11.x 和 jQuery E ...

  10. 字符串类为JAVA中的特殊类

    字符串类为JAVA中的特殊类,String中为final类,一个字符串的值不可重复.因此在JAVA VM(虚拟机)中有一个字符串池,专门用来存储字符串.如果遇到String a=”hello”时(注意 ...