你有10种面值的货币,每个有ci个,然后让你正好凑p元,并且货币个数最多
以前做过类似的贪心就是取最少的大的,然后用小的去凑大的
但是这题不一样,因为50和20不整除,200和500也是
怎么办呢。考虑到100被20整除,1000被500整除,所以可以考虑一下多选一个50和500
话虽这么说,但是直接在50和500那多取一个还是wa
改成了所有的都能多取一个,就AC了
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 1000005
#define MAXN 1000005
#define maxnode 105
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
int c[15]={0,1,5,10,20,50,100,200,500,1000,2000};
int d[15];
LL sum[15];
int ans;
void dfs(int p,int x,int num){
if(p<0) return ;
if(p==0){
ans=max(ans,num);
return;
}
if(x==0) return;
LL tmp=max(p-sum[x-1],0LL);
int y=tmp/c[x]+((tmp%c[x])?1:0);
if(y>d[x]) return;
dfs(p-(LL)y*c[x],x-1,num+y);
y++;
if(y>d[x]) return;
dfs(p-(LL)y*c[x],x-1,num+y);
}
int main(){
int t;
cin>>t;
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=10;i++) scanf("%d",&d[i]);
sum[0]=0;
for(int i=1;i<=10;i++){
sum[i]=sum[i-1]+(LL)d[i]*c[i];
}
ans=-1;
dfs(n,10,0);
printf("%d\n",ans);
}
return 0;
}