hdu1171 flexible use of knapsack problem ... And also!!!! Reasonable calculation of the range of the array!! WA has been so many times!

Source: Internet
Author: User

Problem Descriptionnowadays, we all know that computer College are the biggest department in HDU. But, maybe you don ' t know that computer College had ever been split into computer College and software College in 2002.
The splitting is absolutely a big event in hdu! At the same time, it's a trouble thing too. All facilities must go halves. First, all facilities is assessed, and the facilities is thought to being same if they has the same value. It is assumed this there is N (0<n<1000) kinds of facilities (different value, different kinds).

Inputinput contains multiple test cases. Each test case is starts with a number n (0 < n <= – The total number of different facilities). The next N lines contain an integer V (0<v<=50--value of facility) and an integer M (0<m<=100--corresponding Number of the facilities) each. You can assume this all V is different.
A test case starting with a negative an integer terminates input and this test case was not the be processed.

Outputfor, print one line containing, integers A and B which denote the value of computer College and Softwar e College'll get respectively. A and B should be as equal as possible. At the same-time, you should guarantee, which A is not less than B.

Sample Input210 120 1310 1 20 230 1-1

Sample OUTPUT20 1040 40 for this average distribution problem can be DP (in the case of the average distribution of a more than a few so B will have to be under aver) due to this topic with multiple backpacks easy hyper memory. I've done this at the time of typing. Repeat the number of one by one to write the number of people group and then the size of the array is calculated wrong .... (Stay a little more) #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace Std;
int max (int x,int y)
{
if (x>y) return x;
else return y;
}
int main ()
{
int n,a[5551],dp[250005],i,j,z,sum,aver,suma,sumb,k;/////The range of two arrays Ah, good calculation.
int Fun;//dui Geshu Dayu 1 D chuli
while (Cin>>n)
{
if (n<0) break;
sum=0;
for (i=1;i<=n;i++)
{
scanf ("%d%d", &a[i],&z);
if (z>=2)
{
for (j=1;j<=z;j++)
{
A[i+1]=a[i++];
n++;
}
}
Sum+=a[i]*z;
}
Memset (Dp,0,sizeof (DP));
AVER=SUM/2;
for (i=1;i<=n;i++)
{
for (j=aver;j>=a[i];j--)
{
Dp[j]=max (Dp[j],dp[j-a[i]]+a[i]);
}
}
Sumb=dp[aver];
Suma=sum-sumb;
cout<<suma<< ' <<sumb<<endl;
}
return 0;
}

hdu1171 flexible use of knapsack problem ... And also!!!! Reasonable calculation of the range of the array!! WA has been so many times!

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.