HDU 1171 0-1 Backpack

Source: Internet
Author: User

Recently I feel the DP has completely forgotten. All kinds of explosions, ready to review the first, 0-1 backpack began

Big Event in Hdu
Problem DescriptionNowadays, we know that Computer College are the biggest department in HDU. But, maybe you don ' t know this Computer College had ever been split into Computer College and Software in 2002.
The splitting is absolutely a big event in hdu! At the same time, it is a trouble thing too. All facilities must go halves. Facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed this there is N (0<n<1000) kinds of facilities (different value, different kinds).


Input
Input contains multiple test cases. Each test case 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 so all V are different.
A test case starting with a negative integer terminates the input and this test case are not. Processed.


Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software Ege'll get respectively. A and B should be as equal as possible. At the same time, your should guarantee that's not less than B.


Sample Input

2

10 1

20 1

3

10 1

20 2

30 1

-1



Sample Output

20 10

40 40

To sum up, put all the equipment into a array, so that you do not have to consider the number of problems, direct use of 0-1 backpacks can be solved.
In addition, how to ensure that a is not less than B? You only need to calculate half the maximum DP value of the total, that's the value of B, Sum-b is the value of a.

#include <iostream> #include <vector> #include <algorithm> #include <map> #include <vector > #include <queue> #include <string> #include <set> #include <cmath> #include <cstdio> include<sstream> #include <cstring> #define INF 0x7fffffff//#pragma warning (disable:4996) using namespace
Std
int v[55];
int m[55];
int a[5005];
int dp[200005];
	int main () {//freopen ("S.txt", "R", stdin);
	int n;
		while (CIN >> N) {if (n <= 0) break;
		memset (DP, 0, sizeof (DP));
		memset (A, 0, sizeof (a));
		int sum = 0;
			for (int i = 0; i < n; i++) {cin >> v[i] >> m[i];
		Sum + + v[i] * M[i];
		int num = 0;
				for (int i = 0; i < n; i++) {for (int j = 0; J < M[i]; J +) {A[num] = V[i];
			num++;
		int tmp = SUM/2;
		for (int i = 0; i < Num. ++i) {for (int j = tmp; J >= A[i];--j) dp[j] = max (Dp[j], dp[j-a[i] + a[i]); printf ("%d%d\n", sum-dp[tmp], dp[tmp]);
	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.