HDU 5527 贪心
Too Rich
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1666 Accepted Submission(s): 422
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.
1≤T≤20000
0≤p≤109
0≤ci≤100000
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
-1
36
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int c[];
int v[] = {,,,,,,,,,};
int num[];
int sum,mon,p,ans; int solve(int x) {
int ret = ,need; for(int i = ; i >=; i--) { if(v[i]==) {
need = x/;
need = min(need,num[i]/);
ret +=*need;
x-=need*;
}
else if(v[i]==) {
need = x/;
need = min(need,num[i]/);
ret +=*need;
x-=need*;
}
else {
need = x/v[i];
need = min(need,num[i]);
ret +=need;
x -=need*v[i];
}
}
if(x) return inf;
return ret; } int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--) { sum = mon = ;
scanf("%d",&p);
for(int i = ; i < ; i++) {
scanf("%d",&num[i]);
sum +=num[i];
mon +=num[i]*v[i];
} p = mon - p; //最少的硬币去兑换 if(p<) {
puts("-1");
continue;
} ans = inf;
ans = min(ans,solve(p)); if(num[]) {
p-=;num[]--;
if(p>=)
ans = min(ans,solve(p)+);
p+=;num[]++;
}
if(num[]) {
p-=;num[]--;
if(p>=)
ans = min(ans,solve(p)+);
p+=;
num[]++;
}
if(num[]&&num[]) {
p-=;num[]--;num[]--;
if(p>=)
ans = min(ans,solve(p)+);
}
if(ans==inf) puts("-1");
else printf("%d\n",sum-ans);
} return ;
}
HDU 5527 贪心的更多相关文章
- 【算法系列学习】HDU 5527 Too Rich贪心
http://www.cnblogs.com/AOQNRMGYXLMV/p/4934747.html #include<iostream> #include<cstdio> # ...
- Too Rich HDU - 5527 (贪心+dfs)
Too Rich Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5527 Too Rich 贪心
题意: 有\(10\)种面值为\(1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000\)的纸币,现在你要选最多的数量凑成\(p\)块钱. 分析: 同样分析问题的反面 ...
- HDU 5527 Too Rich ( 15长春区域赛 A 、可贪心的凑硬币问题 )
题目链接 题意 : 给出一些固定面值的硬币的数量.再给你一个总金额.问你最多能用多少硬币来刚好凑够这个金额.硬币数量和总金额都很大 分析 : 长春赛区的金牌题目 一开始认为除了做类似背包DP那样子 ...
- HDU 5527:Too Rich(DFS+贪心)***
题目链接 题意 给出p块钱,现在要用十种硬币凑出,每种硬币有c[i]个,问最多能用多少个硬币. 思路 首先确定,对于每个硬币就是能用小的替换就不用大的. 所以,可以先把硬币尽量用小的替换,如果小的不够 ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
随机推荐
- mysql PXC集群方案总结
同时写集群内的所有机器 写性能依赖最慢的那个机器 读性能提高X倍
- 二维vector容器读取txt坐标
template <class vector> struct HeadLocation{ vector x; vector y; }; vector<HeadLocation< ...
- ibaits数组形式批量入库
一.xml层 <!--批量入库--> <insert id="addSecCodeList" parameterClass="java.util.Has ...
- Hadoop Ecosytem
There are a lot of Hadoop related projects which are open sourced and widely used by many componies. ...
- 最近准备把安卓和java的知识再回顾一遍,顺便会写博客上!千变万化还都是源于基础,打扎实基础
最近准备把安卓和java的知识再回顾一遍,顺便会写博客上!千变万化还都是源于基础,打扎实基础,加油吧 距离去北京还有23天
- 阿里云Centos 7上面安装mysql教程
1 软件的基本安装过程 1 卸载已有的mysql 1.查看系统是否安装了mysql软件 rpm -qa|grep -i mysql 2.将已经安装过的软件卸载掉.注意:这样的卸载是不彻底,不过这里够用 ...
- PHP中break及continue两个流程控制指令解析
<?php $arr = array( 'a' => '0a0', 'b' => '0b0', 'c' => '0c0', 'd' => '0d0', 'e' => ...
- matlab安装过程的被要求的配置程序
顺序是这样的: 网址的顺序是这样的: 1. http://cn.mathworks.com/support/compilers/R2015b/index.html?sec=win64&s_ci ...
- SVM 之 MATLAB 实现代码
MATLAB 中 SVM 实现 直接上代码 main.m %% Initialize data clear, clc, close all; load('data.mat'); y(y == 0) = ...
- vim创建新的命令
转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...