UVa11264 - Coin Collector(贪心)

Our dear Sultan is visiting acountry where there are n different types of coin. He wants to collectas many different types of coin as you can. Now if he wants to withdraw Xamount of money from a Bank, the Bank will give him this money using followingalgorithm.

 

withdraw(X){

if( X == 0) return;

Let Y be the highest valued coin that doesnot exceed X.

Give the customer Y valued coin.

withdraw(X-Y);

}

 

Now Sultan can withdraw any amountof money from the Bank. He should maximize the number of different coins thathe can collect in a single withdrawal.

 

Input:

 

First line of the input containsT 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, ... , Cnthe value of each coin type. C1<C2<C3< … <Cn<1000000000.C1 equals to 1.

 

Output:

 

For each test case output oneline denoting the maximum number of coins that Sultan can collect in a singlewithdrawal. He can withdraw infinite amount of money from the Bank.

 

Sample Input

Sample Output

2

6

1 2 4 8 16 32

6

1 3 6 8 15 20

 

6

4

 

 题意:给出一系列的货币,求出最大的兑换数

思路:s(i)表示coin[0],coin[1]...coin[i]的和,并且s[i]<coin[i + 1],如果s[i] >=coin[i+1],那么coin[i+1]也应该选上,所有相当于找到这样的一个序列s[i-1] < coin[i],并且s[i] = s[i-1]+coin[i] < coin[i + 1]

#include <cstdio>

using namespace std;

const int MAXN = 1010;

int coin[MAXN];
int n;

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &coin[i]);
    }
}

void solve()
{
    if (n <= 2) {
        printf("%d\n", 2);
    } else {
        int sum = coin[0];
        int ans = 2;
        for (int i = 1; i < n - 1; i++) {
            if (sum < coin[i] && sum + coin[i] < coin[i + 1]) {
                sum += coin[i];
                ans++;
            }
        }
        printf("%d\n", ans);
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("d:\\OJ\\uva_in.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int cas;
    scanf("%d", &cas);
    while (cas--) {
        input();
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值