UVa 11264 Coin Collector (coin & greedy Good question)

Source: Internet
Author: User
Tags printf time limit

11264-coin Collector

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=2231

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 it wants to withdraw X amount of a bank, the bank would give him this money using following algorithm.

Withdraw (X) {

if (X = = 0) return;

Let Y is 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 the Bank. He should maximize the number of different coins and can collect in a single withdrawal.

Input:

The number of the 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, Sultan can collect in a single withdrawal. He can withdraw infinite amount of the Bank.

Sample Input Sample Output
2
6
1 2 4 8 16 32
6
1 3 6 8 15 20
6
4

Ideas:

1. You must have noticed that the coin with the biggest face value c[n-1] has to be chosen. (Disprove: If spent sum yuan but did not select it, know sum<c[n-1], so use m+c[n-1] yuan to exchange can get a better solution. )

2. The key to greed: suppose S (i) is the par value of those selected currencies in C[1]...c[i]. Then there must be S (i) < c[i+1].

3. So you can construct a sequence like this. As stated in 2, if there are S (i-1) < C[i] and S (i) =s (i-1) +c[i]<c[i+1], then C[i will be selected.

Complete code:

/*0.015s*/
    
#include <cstdio>  
    
int c[1005];  
    
int main ()  
{  
    int t, n, I, Sum, count;  
    scanf ("%d", &t);  
    while (t--)  
    {  
        scanf ("%d", &n);  
        for (i = 0; i < n; ++i)  
            scanf ("%d", &c[i]);  
        if (n <= 2) printf ("%d\n", n);  
        else
        {  
            sum = c[0], count = 2;///The last one for  
            (i = 1; i < n-1 ++i)  
                if (Sum < c[i] && s Um + C[i] < C[i + 1])  
                    sum + + c[i], ++count;  
            printf ("%d\n", count);  
        }  
    return 0;  
}

Author Signature: CSDN blog Synapse7

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.