HDU 1171 big event in HDU (motion gauge)

Source: Internet
Author: User
Big event in HDU Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 22623 accepted submission (s): 7948



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
Recommendwe have carefully selected several similar problems for you: 2844 2191 1176 1114 1398
Question: divide a group of numbers into two portions, with the minimum difference required.
Multiple backpacks can be converted into 0-1 backpacks, and the total capacity is half of the total.
Because the total value remains unchanged, if the value of one party is closer to half of the total value, the gap between the two parties is smaller.
The Code is as follows:
Multiple backpacks turn 01 backpacks:
# Include <iostream> # include <algorithm>Using namespaceSTD;# Define M 5000 // try 4000, and the result is hung. # Define n 250000IntDP[N],Vis[M]; IntMax(IntX, IntY) {ReturnX>Y?X:Y;} Int main (intI, IntJ, IntK) {IntN,TOT,X,Y; While (Scanf("% D",&N)! =EOF&&N> =0){K=TOT=0;Memset(Vis,0, Sizeof (Vis));Memset(DP,0, Sizeof (DP); (I=1;I<=N;I++ ){Scanf("% D",&X,&Y);TOT+ =X*Y; While (Y--){Vis[++K] =X; // Convert multiple backpacks to 01 backpacks. } (I=1;I<=K;I++) // 01 backpack template, item quantity 0-> K, backpack capacity sum/2; (J=TOT/2;J> =Vis[I];J--)DP[J] =Max(DP[J],DP[J-Vis[I] +Vis[I]); // Whether to place the I-th item or not.Printf("% D \ n",TOT-DP[TOT/2],DP[TOT/2]);} Return0;}
Baidu 0ms:
# Include <iostream> # include <string. h> # include <algorithm>Using namespaceSTD; Const intMax=110000; IntA[Max],Len,N; Int main () {While (CIN>N&&N> =0){Memset(A,0, Sizeof (A); IntNum,Value,Sum=0;Len=0; For (intI=0;I<N;I++ ){CIN>Value>Num;Sum+ =Value*Num; While (Num--)A[Len++] =Value; // Here should be the thought of 01 backpack}Sort(A,A+Len); // Do not understand... IntHalf=Sum/2;Num=0; For (intI=Len-1;I> =0;I--) {If (Num+A[I]>Half) Continue;// I don't understand the principle at all. Num+ =A[I];}Cout<Sum-Num<""<Num<Endl;} Return0;}


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.