Tug of War
Topic link: Tug of War
Title description
Problem solving ideas
This is a very simple deep search.
According to the question, each group has at most n 2 \frac{n}{2}2n personal.
For everyone, there are only two cases of choosing and not choosing.
Accumulate the total selected, and subtract the total of all people to get the weight of the other pair.
Just take the absolute value.
code
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int T;
int n,ans,st;
int a[30];
void dfs(int x,int y,int s)
{
if(y>=n/2)
{
ans=min(ans,abs(st-2*s));
return;
}
if(x>n)
return;
dfs(x+1,y,s);
dfs(x+1,y+1,s+a[x]);
}
int main()
{
cin>>T;
while(T--)
{
cin>>n;
st=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),st+=a[i];
ans=0x3f3f3f3f;
dfs(1,0,0);
cout<<ans<<endl;
}
}