Ultraviolet A 562-Dividing coins

Source: Internet
Author: User

Divide the coin into two heaps to minimize the difference between the two heaps of coins.

0-1 backpack, which can calculate all the sum of coins, and then start to explore 0 from Sum/2 (sum is the sum of all coins, until you find the biggest coin and I that can be made up by the given coin, and the minimum difference is sum-2 * I.

The Code is as follows:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;const int Maxn = 102;int coin[Maxn], dp[Maxn*500];int main(){#ifdef test    freopen("input.txt", "r", stdin);#endif    int m, n;    scanf("%d", &n);    while(n--)    {        int sum = 0;        scanf("%d", &m);        for(int i=0; i<m; ++i)        {            scanf("%d", &coin[i]);            sum += coin[i];        }        memset(dp, 0, sizeof(dp));        dp[0] = 1;        for(int i=0; i<m; ++i)            for(int j=sum; j>=coin[i]; --j)            {                if(dp[j-coin[i]])                    dp[j] = 1;            }        int i;        for(i=sum/2; i>=0; --i)            if(dp[i])             break;        printf("%d\n", sum - 2 * i);    }    return 0;}

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.