Sdut OJ I-sample (0-1 knapsack problem "template")

Source: Internet
Author: User

I-sample Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ The question describes what is the problem? DP, greedy, data structure, graph theory, number theory or computational geometry? Tube him, anyway fat giant metropolis, although fat giant walk early. Now there are N number XI, and now you want to divide these numbers into two groups, a, a, so that abs (SUM (A)-sum (B)) is as small as possible, and each XI must and can only be divided
To a group, each group contains at least one number. sum () is calculated as cumulative and ABS () represents the absolute value of the calculation. The input input has more than one set. For each set of data: The first line enters an n (1 <= n <= 100), and the next n rows are an integer Xi (1 <= Xi <= 50) per line. Output for each set of data, if you can complete the task output an integer representing the answer, otherwise output-1. Sample input
33122210
Sample output
08
Algorithm Analysis: This topic began to look like a greedy algorithm to achieve, not actually. For some comparison pit point data, the result is dead!

For example, seniors gave me the data:

5

10 8 9 5 4 The correct result should be 0, the greedy result is dead!

The correct algorithm is: If you want to make two discrete sets of ABS () the difference between the smallest, that is, two sets of the individual and as close as possible

(SUM (set a) +sum (set B))/2. Don't care if you don't evenly divide it, because we're going to do it with the next backpack, this backpack.

It's not necessarily full!

Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < Ctype.h> #include <algorithm>using namespace Std;int f[2600];int main () {    int n;    int p[150];    int I, J;    while (scanf ("%d", &n)!=eof)    {        int sum=0;        for (i=0; i<n; i++)            scanf ("%d", &p[i]), sum+=p[i];        if (n==1)        {            printf (" -1\n");            Continue;        }        memset (f, 0, sizeof (f));        int dd=sum;        SUM=SUM/2;        The backpack does not have to fill for        (i=0; i<n; i++)        {for            (j=sum; j>=p[i]; j--)                f[j] = max (F[j], f[j-p[i]]+p[i]); c21/>}        dd=dd-f[sum];        printf ("%d\n", ABS (F[SUM]-DD));    }    return 0;}

Sdut OJ I-sample (0-1 knapsack problem "template")

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.