CODEVS1815 BSOJ2971 Birthday gift DP or randomization _DP

Source: Internet
Author: User
Tags abs
2971--"mock test" birthday present Description a pair of twin siblings on the same day birthday, this day, their friends sent them a gift, each gift is 2 books, one to his brother, one to his sister, but did not say which is for the sister, which is to the brother,   Each book has its own value, in order to avoid conflict, let you assign, ask the two people to obtain the value of the book and the gap between as small as possible. For example, there are 4 gifts: (3,5), (7,11), (8,8), (2,9), can assign 3,7,8,2 to sister, the rest of the elder brother, the value of the difference is: 5+11+8+9-3-7-8-2=13, if the 3,7,8,9 to sister, the rest of the elder brother, the value of the difference is: 3+ 7+8+9-5-11-8-2=1, this is the best solution. Input input file The first line of contains a positive integer n, representing the number of gifts, followed by n rows, two integers per line, representing the value of two books per gift (value range between 1 and 300). Output file Gift.out contains a non-negative integer representing the minimum value difference. Sample Input 4 3 5 7 8 8 2 9 Sample Output 1 Hint "Data scale" for 20% of the data, there are n≤20, for 40% of the data, there are n≤50, for 100% of the data, there are n≤150.
The positive solution to this problem is DP, but for this kind of problem, we can do it with randomization. DP is the delt of each group's birthday present, so we can write it in a thought that is approximate to 01 Knapsack is based on greedy adjustment (in fact, I think it can only be called random, no), every time to adjust all the order, and then do the greedy, will be large to the current small, will be small to the current large.
Want to see a random look at this article of mine

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
struct node{int l,r;
} W[150];
int n;
void Wash ()
	(int i=1;i<=n;i++)
		int t=rand ()%n+1;
		Swap (W[I].L,W[T].L);
		Swap (W[I].R,W[T].R);
void Solve ()
	int ti=1000;
	int ans=0x3fffffff;
	while (ti--)
		wash ();
		int sum1=0,sum2=0;
		for (int i=1;i<=n;i++)
			if (sum1<sum2) Sum1+=max (W[I].L,W[I].R), Sum2+=min (W[I].L,W[I].R);
			else Sum2+=max (W[I].L,W[I].R), Sum1+=min (W[I].L,W[I].R);
		}		cout<< "dhasj\n";
		Ans=min (Ans,abs (sum1-sum2));
int main () {
	Srand (Time (NULL));
	for (int i=1;i<=n;i++)
	Solve ();
	return 0;

The following DP code comes from the network, lazy to write
         a:array[0..151,1..2] of Longint;
         Z:ARRAY[0..151] of Longint;
         F:ARRAY[0..450000] of Longint;
      READLN (n);
      For I:=1 to n do begin
            Readln (a[i,1],a[i,2]);
            Z[i]:=abs (a[i,1]-a[i,2]);
      V:=h Div 2;
      Fillchar (F,sizeof (f), 0);
      For I:=1 to n does begin for
          j:=v Downto z[i] Do begin
              if F[J-Z[I]]+Z[I]>F[J] then f[j]:=f[j-z[i]]+z[i];
      Writeln (ABS (H-f[v])-f[v]);

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: 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.