(Hdu step 3.3.1) Big Event in HDU (01 backpack: place N items in a V-sized backpack. The cost of I-th items is c [I], the value is w [I]. Bigevent

Source: Internet
Author: User

(Hdu step 3.3.1) Big Event in HDU (01 backpack: place N items in a V-sized backpack. The cost of I-th items is c [I], the value is w [I]. Bigevent

Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 854 Accepted Submission (s): 345

Problem DescriptionNowadays, we all know that Computer College is 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 is a trouble thing too. all facilities must go halves. first, all facilities are assessed, and two facilities are thought to be same if they have the same value. it is assumed that there is N (0 <N <1000) kinds of facilities (different value, different kinds ).
 
InputInput contains multiple test cases. each test case starts with a number N (0 <N <= 50 -- 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 that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
 
OutputFor each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B shoshould be as equal as possible. at the same time, you shoshould guarantee that A is not less than B.
 
Sample Input
210 120 1310 1 20 230 1-1
 
Sample Output
20 1040 40
 
Authorlcy


Question Analysis:

This is the 01 backpack in the ninth lecture of the backpack. The so-called 01 backpack, that is to say, each item has only two States, put or not put. In this question, as long as the maximum value that software college can obtain is obtained, the maximum value that computer college can obtain is known. Therefore, set sum/2, the maximum value that software college can obtain, to the maximum capacity V of the backpack. In this question, the value of each item w [I] also serves as the cost c [I]... As long as you understand the above ideas, it is no longer difficult to code...



The Code is as follows:

/**. Cpp ** Created on: February 12, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 5005; const int maxm = 250005; int w [maxn]; // used to record the value of each item int f [maxm]; // used to record the maximum value int main () that can be obtained when the capacity is v () {int n; while (scanf ("% d", & n )! = EOF, n> 0) {// The processing that ends when n is-1 is n> 0 memset (w, 0, sizeof (w )); // reset the fee/value is 0. In fact, here resetting does not affect memset (f, 0, sizeof (f); // reset the recharge or obtain the maximum value of 0. This array must be reset. Otherwise, the state transition equation f [v] = max (f [v], f [v-c [I] + w [I]) may be affected. int v, m; int sum = 0; // the total value of all commodities int I; int j; int cnt = 0; for (I = 0; I <n; ++ I) {// traverse each item scanf ("% d", & v, & m); sum + = v * m; // Add the total value of each item to the total value of all items for (j = 0; j <m; ++ j) {w [cnt ++] = v; // record the value of each item} cnt-= 1; for (I = 0; I <cnt; ++ I) {for (j = sum/2; j> = w [I]; -- j) {/*** expand it to f [I] [j] = max (f [I-1] [j], f [I-1] [j-w [I] + w [I]) to understand * that: the maximum value that can be obtained when I items are placed in the backpack with size j is * the maximum value that can be obtained when I items are placed in the backpack with size j (I items are not placed in the backpack medium) and * the maximum value that can be reached in the backpack where the size of the I-1 item is j-w [I] (this is to make room for the I item) * maximum value */f [j] = max (f [j], f [j-w [I] + w [I]);} // The value that a school can obtain is f [sum/2]. the other method is that sum-f [sum/2] Has printf ("% d \ n", sum-f [sum/2], f [sum/2]);} 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.