Coin Collector
Our dear Sultan is visiting a country where there are n different types of coin. He wants to collect
as many different types of coin as you can. Now if he wants to withdraw X amount of money from a
Bank, the Bank will give him this money using following algorithm.
withdraw(X){
if( X == 0) return;
Let Y be the highest valued coin that does not exceed X.
Give the customer Y valued coin.
withdraw(X-Y);
}
Now Sultan can withdraw any amount of money from the Bank. He should maximize the number
of different coins that he can collect in a single withdrawal.
Input
First line of the input contains T the number of test cases. Each of the test cases starts with n
(1 ≤ n ≤ 1000), the number of different types of coin. Next line contains n integers C1, C2, . . . , Cn
the value of each coin type. C1 < C2 < C3 < . . . < Cn < 1000000000. C1 equals to 1.
Output
For each test case output one line denoting the maximum number of coins that Sultan can collect in a
single withdrawal. He can withdraw infinite amount of money from the Bank.
Sample Input
2
6
1 2 4 8 16 32
6
1 3 6 8 15 20
Sample Output
6
4
题目简述:
苏丹要用钱换硬币,硬币有非常多种,苏丹想要尽可能多种类的硬币。规则是:换硬币时优先给你小于等于总金额的面额最大的硬币。
题目分析:
找好贪心策略很关键。
这位博主讲解的非常清楚:https://blog.csdn.net/synapse7/article/details/12262029
代码实现:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
#define pf printf
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define ms(i,j) memset(i,j,sizeof(i))
int a[1005];
int main()
{
int T,n;
sf(T);
while(T--)
{
int sum=0,s=0;
sf(n);
for(int i=1;i<=n;i++)
sf(a[i]);
for(int i=1;i<n;i++)
{
if(sum<a[i]&&sum+a[i]<a[i+1])
{
sum+=a[i];
s++;
}
}
pf("%d\n",s+1);
}
}