1420 of 9-degree notes: Jobdu MM fruit

Source: Internet
Author: User

Question 1420: Jobdu MM fruit

 

Description:
The Jobdu team has two PPMM, both of which share the same idea. One day, Fuqiang company sponsored a batch of fruit to the team, and Hu sent the fruit pie to the two MM, which were allocated by them. Each fruit has a weight. Can you tell them how to minimize the weight difference?

Input:
Multiple groups of data are input. The number of fruits input in the first row of each group is n (1 <= n <= 100 ), enter n weight wi (0 <= wi <= 10 ^ 5) in the next line ).

Output:
Output a row of input values in each group. The minimum difference value is obtained.

Sample input:
510 20 30 10 10

 

 

Algorithm Analysis
Question and question 1358: Cheng Bo's egalitarianism can be considered as the same question.

We use dynamic planning. Assume that the total weight of all fruits is sum, then the weight of the fruit that one person can obtain must be in the range of 0-sum/2,

If sum is an odd number, the w value range should be between 0 and (sum/2 + 1). When a person receives sum/2 + 1, the other person must be assigned sum/2, which is the same as the test range from 2 to sqrt (n) When we calculate the quantity.

Our problem is to convert the weight w assigned by a person from sum/2 to 0.

When we give a person no fruit, we can achieve 0. That is, dp [0] = true;

When we select the I fruit, the weight of the fruit is num [I], and we determine whether the dp [w] is true before the I fruit is assigned to this person, or whether dp [w-num [j] is true;

Dp [w] = true if you pick out a few fruits from the previous I-1 to make up w;

If you pick out a few fruit from the previous I-1 that can make up the weight w-num [I], then the weight of the I fruit can make up w, so

 

 

dp[j] = dp[j]||dp[j-num[i]];         dp[j] = dp[j]||dp[j-num[i]];

When updating the dp, you must note that the value ranges from sum/2 To num [I] to ensure that each fruit appears 0 or 1 time.

If dp is updated from num [I] To sum/2, dp [j-num [I] may be the result of the I-th fruit. Similar

Question 1455: cherish the present and be grateful for Life (each element has a limited number of elements and can be converted into multiple independent elements)

Question 1434: Do not AC this summer

Topic 1499: project arrangement

For an infinite number of elements, dp is updated progressively from num [I] to the maximum

Question 1455: cherish the present and be grateful for your life

Source program

//============================================================================// Name        : judo1358.cpp// Author      : wdy// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================//similar to   judo1358#include <iostream>int num[101];using namespace std; void allocate(int n){    int sum=0;    for(int i = 0;i<n;i++){        std::cin>>num[i];        sum+=num[i];    }         int maxPercent = sum>>1;    bool *dp = new bool[maxPercent+1];    for(int i = 1;i<maxPercent+1;i++)        dp[i] = false;    dp[0] = true;    for(int i = 0;i<n; i++){        for(int j = maxPercent; j>=num[i]; j--)            dp[j] = dp[j]||dp[j-num[i]];    }    for(int i = maxPercent;i>0;i--){        if(dp[i]){            std::cout<< sum-2*i<<std::endl;            break;        }    }} void judo(){    int n;    while(std::cin>>n){        allocate(n);    }}int main() {    judo();    return 0;}/**************************************************************    Problem: 1420    User: KES    Language: C++    Result: Accepted    Time:770 ms    Memory:3888 kb****************************************************************/

 

Related Article

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.