POJ 2576 Tug of War stochastic algorithm

Source: Internet
Author: User

Original title address: http://poj.org/problem?id=2576

Tug of War

Time limit:3000ms

Memory limit:65536k

Total submissions:8525

accepted:2320

Description

A tug of war is to being arranged at the local office picnic. For the tug of war, the picnickers must is divided into and teams. Each person must is on a team or the other; The number of people on the teams must not differ by more than 1; The total weight of the people in each team should is as nearly equal as possible.

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight for person 1; The second the weight of person 2; And so on. Each weight are an integer between 1 and 450. There is at the very people at the picnic.

Output

Your output would be a single line containing 2 numbers:the total weight of the people on one team, and the total weight o f The people on the other team. If These numbers differ, give the lesser first.

Sample Input

3

100

90

200

Sample Output

190 200

Test instructions: There are n individuals to be divided into two teams to carry out tug-of-war competition, require the difference between the two teams not more than 1, and the weight and as close as possible.

Idea: first by weight from small to large, and then the first half of a team, the latter half of Team B, calculate the difference in weight as the current minimum value. Generate two random number x, Y, as the number of players in the exchange, that is, the team a X players to Team B, the team Y Team B to the team A, check the exchange after the weight difference is smaller, if the exchange and record the new minimum value, otherwise do not exchange. Repeat Operation N Times, as long as the RP is good enough to AC.

1#include <cstdio>2#include <algorithm>3#include <stdlib.h>4#include <time.h>5 using namespacestd;6 7 intMainvoid)8 {9 Srand ((unsigned) time (NULL));Ten     intN, N, a[ the], b[ -], c[ -], NB, NC, Sumb, SUMC, Close, X, Y, SX, SY; One      while(~SCANF ("%d", &N)) A     { -          for(intI=0; i<n; ++i) -scanf"%d", A +i); the         if(n==1) -         { -printf"0%d\n", a[0]); -             Continue; +         } -Sort (A, A +n); +Sumb = SUMC =0; A         if(n&1) at         { -NB = n/2+1; -NC = n-nb; -             inti =0; -              for(; i<nb; + +i) -B[i] = A[i], sumb + =A[i]; in              for(; i<n; + +i) -C[I-NB] = A[i], SUMC + =A[i]; to         } +         Else -         { theNB = NC = n/2; *             inti =0; $              for(; i<nb; + +i)Panax NotoginsengB[i] = A[i], sumb + =A[i]; -              for(; i<n; + +i) theC[I-NB] = A[i], SUMC + =A[i]; +         } AClose = ABS (sumb-SUMC); theN =1000000; +          while(n--) -         { $x = rand ()%nb; $y = rand ()%NC; -SX = Sumb-b[x] +C[y]; -SY = SUMC + b[x]-C[y]; the             if(ABS (Sx-sy) <close) -             {WuyiSumb =SX; theSUMC =Sy; -B[X] ^= c[y], C[y] ^= b[x], b[x] ^=C[y]; WuClose = ABS (sx-sy); -             } About         } $         if(Sumb >SUMC) -Sumb ^= SUMC, SUMC ^= sumb, Sumb ^=SUMC; -printf"%d%d\n", Sumb, SUMC); -     } A}

POJ 2576 Tug of War stochastic algorithm

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.