ZB's Birthday (Mob search Dfs)

Source: Internet
Author: User

ZB's Birthday time limit: ms | Memory limit:65535 KB Difficulty:2
Describe
today is the lunar calendar July early five, ACM team ZB's birthday. ZB is and C small plus, never in Wuhan training. He wanted to buy something for the two brothers to celebrate the birthday, after investigation, ZB found that C small plus and never are very fond of watermelon, and a eat is a bunch of the kind, ZB immediately determined to buy a bunch of watermelon. When he was ready to give the watermelon to C small plus and never, met a problem, never and C small plus not live, can only divide the watermelon into two piles to them, in order to everyone is fair, he wants to let two piles of weight difference is the smallest. The weight of each watermelon is known, can you help him?
Input
multiple sets of test data (<=1500). Data at the end of EOF first line input watermelon number n (1≤n≤20) the second row has n number, W1, ..., Wn (1≤wi≤10000) represents the weight of each watermelon respectively
Output
quality difference After the output is divided into two piles
Sample input
55 8 13) 27 14
Sample output
3
Idea: The first DFS timed out for a long time, communicate with the great God completely do not understand, or too much food; direct sticker Code bar;
Own code:#include <stdio.h>
#include <algorithm>
using namespace Std;
int melon[21],n,dd,sum;
void Birth (int i,int teamer) {
if (i<=n) {
int X=sum-2*teamer;
if (x<0) x=-x;
dd=x<dd?x:dd;
if (teamer>sum/2.0) return;
Birth (i+1,teamer+melon[i+1]);
Birth (I+1,teamer);
}}
int main () {
while (~SCANF ("%d", &n)) {sum=0;
for (int i=1;i<=n;++i) scanf ("%d", &melon[i]), sum+=melon[i];
Sort (melon+1,melon+n+1);
Dd=sum;
Birth (0,0);
printf ("%d\n", DD);
}
return 0;
}
The Code of the Great God://Although not read;

ITCSC 14-3 Chen Zhenyu 2015/7/12 23:19:10
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>
using namespace Std;
int melon[21],n,dd,sum;
int vis[21];
int ans;
void Birth (int pos, int len)//Current search location current watermelon total mass
{
ans = min (ABS (Sum-len * 2), ans);//update
if (pos = = n+1) return;
if (len > Sum/2) return;//pruning the ascending order is now over, and the search will be super.
for (int i = pos; I <= N; i++)
{
if (Vis[i]) continue;
Vis[i] = 1;
Birth (i+1, Len+melon[i]);
while (melon[i] = = melon[i+1])//pruning to remove the same quality of the watermelon after the problem data is not good this step should be useful results plus more than 100ms
++i;
Vis[i] = 0;
}
}
int main ()
{
while (~SCANF ("%d", &n))
{
sum=0;
for (int i=1;i<=n;++i)
scanf ("%d", &melon[i]), sum+=melon[i];
Sort (melon+1, melon+n+1);//ascending sort quality big watermelon influence big pruning
ans = 10000000;
memset (Vis, 0, sizeof (VIS));
Birth (1, 0);//Current position current length
printf ("%d\n", ans);
}
return 0;
}
Just start their own reference to the Great God Code://Although not pruning, but the shortest time to run, did not find out why;
#include <stdio.h>
#include <math.h>
int a[10000];
int dd;//with DD to save the most deserted
void Fun (int sum,int cur,int m,int i)
{if (i<m)//maximum of M
{//Assuming cur as one of the sub-points and, for example, sum=67,cur=35,t1=-3;
int t1=sum-2*cur;
if (t1<0)
T1=-T1;
if (T1&LT;DD)//note the smallest T1;
DD=T1;
Fun (sum,cur+a[i+1],m,i+1);//select a[i+1]
Fun (sum,cur,m,i+1);//Do not choose a[i+1];
}  }
int main () {
int m;
int i,j,sum=0;
while (scanf ("%d", &m)!=eof)
{
sum=0;
for (i=0;i<m;i++)
{
scanf ("%d", &a[i]);
Sum=sum+a[i];
}
Dd=sum;
Fun (sum,a[0],m,0);
printf ("%d\n", DD);
}
return 0;
}

ZB's Birthday (Mob search Dfs)

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.