Uva 562-dividing coins (DP)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: n coins, given the value of each coin, require that these coins be divided into two groups, making the value of the two groups as small as possible.

It can be found that if you can split equally, then the value difference must be 0, then from SUM/2 to 0 enumeration I, if the value combination of the above coin can be composed of I then sum-i-i is the answer. How to determine whether for a number I with these coins can be gathered out, with a backpack sentence can be, note is 01 backpack.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 55005#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1<<40+10#define pp pair<int,int> #define ull unsigned long longusin G namespace Std;int v[115],dp[maxn],n,s;void solve () {memset (dp,0,sizeof (DP));DP [0]=1;for (int i=1;i<=n;i++) for ( int j=s/2;j>=0;j--) dp[j+v[i]]+=dp[j];for (int i=s/2;i>=0;i--) if (Dp[i]) {printf ("%d\n", s-i*2); return;}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), s=0;for (int i=1;i<=n;i++) {scanf ("%d", v+i); s+ =v[i];} Solve ();} return 0;}


Uva 562-dividing coins (DP)

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.