ACM Changchun 2015 A. Too Rich
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 p 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 = 1 and you have two 10 coins, four 5 coins, and eight 1 coins, you will pay it by two 5coins 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 Format
The first line contains an integer Tindicating the total number of test cases.
Each test case is a line with 1 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 ii dollars in your wallet.
1≤T≤20000
0≤p≤109
0≤ci≤100000
Output Format
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'.
样例输入复制
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
样例输出复制
9
-1
36
题目来源
/*
某些面值的钱分别有若干个,用这些钱来恰好组成某一金额的钱,问最多需要的钱的数目
逆向思维 用总价减去需求=X
那么贪心来用最少的数目凑成X即可(也可能凑不出来)
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 1009
const ll inf =9e18;
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
ll val[]={,,,,,,,,,,};
ll num[],a[];
ll ans,p;
ll sum,ret;
int t;
void dfs(int x,ll sum,ll cnt)
{
if(!sum)
{
ans=min(ans,cnt);
return ;
}
if(x<) return ;
a[x]=min(sum/val[x],num[x]);
dfs(x-,sum-val[x]*a[x],cnt+a[x]);
if(a[x]>=)
{
a[x]--;
dfs(x-,sum-val[x]*a[x],cnt+a[x]);
}
/*
如 : 1
150 0 0 0 3 1 1 0 0 0 0
如果没有上面的三行就是无解 -1
但是 dfs(4,60,0) 因为 用了50,后面的就无法凑成10
但是后面的20*3可以凑成60,因此a[x]可一每次都减1,再去dfs
这样才能考虑到所有的情况!
*/
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld",&p);
mem(a,);
sum=;
ret=;
gep(i,,)
{
scanf("%lld",&num[i]);
ret+=num[i];
sum+=val[i]*num[i];
}
sum-=p;
if(sum<)
{
printf("-1\n");
continue;
}
ans=inf;
dfs(,sum,);
if(ans!=inf)
{
printf("%lld\n",ret-ans);
}
else{
printf("-1\n");
}
}
return ;
}
ACM Changchun 2015 A. Too Rich的更多相关文章
- ACM Changchun 2015 L . House Building
Have you ever played the video game Minecraft? This game has been one of the world's most popular ga ...
- ACM Changchun 2015 J. Chip Factory
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage larg ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering
Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time
Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?
I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing
H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 C. Colder-Hotter
C. Colder-Hotter time limit per test 1 second memory limit per test 512 megabytes input standard inp ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams
A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...
随机推荐
- 洛谷 P1121 环状最大两段子段和
https://www.luogu.org/problemnew/show/P1121 不会做啊... 看题解讲的: 答案的两段可能有两种情况:一是同时包含第1和第n个,2是不同时包含第1和第n个 对 ...
- morphia(3)-查询
1.查询所有 @Test public void query() throws Exception { final Query<Employee> query = datastore.cr ...
- eclipse导入mavn工程报Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 的解决办法
详细报错: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from http://10.74. ...
- Perl的Notepad++环境配置
Notepad++打开pl文件F5录入命令分别保存. Run_Perl(F9): cmd /k F:\Strawberry\perl\bin\perl.exe -w "$(FULL_CURR ...
- 用java代码写一个简单的网上购物车程序
需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...
- zookeeper系列 (第一章 :ubuntu 下安装zookeeper)
1.zookeeper是分布式一致性管理服务.解决了分布式中死锁,不一致,原子性操作等问题. 2.环境:系统ubuntu,zookeeper 下载地址:http://archive.apache.or ...
- 双飞翼布局介绍-始于淘宝UED-2011年淘宝玉伯写的
仔细分析各种布局的技术实现,可以发现下面三种技术被经常使用: 浮动 float 负边距 negative margin 相对定位 relative position 这是实现布局的三个最基本的原子技术 ...
- [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法 --转
初用Yii的srbac模块.出现 Cannot read property ‘msie’ of undefined 错误.上网查询,找到如下的文章.使用文末的打补丁的方法,成功搞定.感谢. ===== ...
- Azure 项目构建 - 构建 WordPress 网站
本课程主要介绍了如何基于 Azure Web 应用和 WordPress 快速构建网站,实践讲解如何使用 Azure Web 应用,创建并连接 MySQL Database on Azure, 使用 ...
- 面试题--JAVA中静态块、静态变量加载顺序
最后给大家一道面试题练练手,要求写出其结果(笔试) public class StaticTest { public static int k = 0; public static StaticTes ...