Team Selection
One of the cherished customs of my childhood was choosing up sides for a cricket game. We did it this way: The two bullies of our gully would appoint themselves captains of the opposing teams, and then they would take turns picking other players. On each round, a captain would choose the most capable (or, towards the end, the least inept) player from the pool of remaining candidates, until everyone present had been assigned to one side or the other. The aim of this ritual was to produce two evenly matched teams and, along the way, to remind each of us of our precise ranking in the neighbourhood pecking order.
We all believed this was the fairest process, but does it ensure the fairest selection of players with evenly matched teams? We believed so, but then there were times when, as the game progressed we realized that the other team was stronger than ours and may be an exchange of a couple of players between the teams would have made them balanced. That scope of improvement seemed to be there...
Here, we need to find a way to create two evenly balanced teams for any game(or as evenly balanced as possible considering the strength of each player). A set of players must be divided into two teams. Each player must be on one team or the other; the number of player on the two teams must not differ by more than 1; each player will have a skill-point associated with him. The total skill-points of the players on each team should be as nearly equal as possible.(The absolute difference of the sum of skill-points of players in each team should be the least).
Input
The first line of input will contain the number of test cases 'T'(1<=T<=100). This is followed by 'T' test cases. Each test case starts with a blank line, followed by N, the total number of players. N lines will follow with the first line giving the skill-point of person 1; the second line, the skill-point of person 2; and so on. Each skill-point shall be an integer between 1 and 450. There shall be at most 100 players in all(1<=N<=100).
Output
Your output should be exactly '2T-1' lines. The output for each test case should be followed by a blank line, except the output for the last test case. Each odd numbered line should contain 2 numbers: the total skill-points of the players on one team, and the total skill-points of the players on the other team. Print the smaller sum first.
Example Input: 4
3
90
200
100
10
2
3
10
5
8
9
7
3
5
2
10
1
1
1
1
1
1
1
1
1
9
8
87
100
28
67
68
41
67
1
Output: 190 200
27 27
5 13
229 230
Code:
#include<iostream>
#include<stdio.h>
static int swapi,swapj,tries;
int main()
{
static int t,result1[100],result2[100],k;
int i,j,temp,x,y;
int sum(int[],int);
int abs1(int);
void findfirstswap(int,int[],int[]);
scanf("%d",&t);
for(k=0;k<t;k++)
{
static int no;
static int a[100];
static int b[100];
printf("\n");
scanf("%d",&no);
for(i=0;i<1000;i++)
a[i]=0;
for(i=0;i<no;i++)
scanf("%d",&a[i]);
for(j=0;j<no;j++)
{
for(i=0;i<(no-1);i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
j=0;
if((no%2)!=0)
no=no+1;
for(i=(no/2);i<no;i++)
{
b[j]=a[i];
a[i]=0;
j++;
}
findfirstswap(no,a,b);
while(swapi!=-1 && swapj!=-1 && tries<100)
{
x=a[swapi];
y=b[swapj];
a[swapi]=y;
b[swapj]=x;
findfirstswap(no,a,b);
}
if( sum(a,no) < sum(b,no) )
{
result1[k]=sum(a,no);
result2[k]=sum(b,no);
}
else
{
result1[k]=sum(b,no);
result2[k]=sum(a,no);
}
}
for(i=0;i<t;i++)
{
printf("%d %d",result1[i],result2[i]);
if(i!=(t-1))
printf("\n");
}
return 0;
}
int sum(int a[],int no)
{
int i,sum=0;
for(i=0;i<(no/2);i++)
sum=sum+a[i];
return sum;
}
int abs1(int a)
{
if(a<0)
a=a*-1;
return a;
}
void findfirstswap(int no,int a[],int b[])
{
int diff=0,i,j,x,y,newdiff=0;
swapi=-1;
swapj=-1;
diff=sum(a,no)-sum(b,no);
if(diff==0)
return;
for(i=((no/2)-1);i>=0;i--)
{
for(j=0;j<=((no/2)-1);j++)
{
tries=tries+1;
x=a[i];
y=b[j];
newdiff=abs1(diff-(2*x)+(2*y));
if(newdiff<abs1(diff))
{
swapi=i;
swapj=j;
return;
}
}
}
return;
}
The above code works fine on my pc.But shows Wrong Answer on Codechef.Any help would be deeply appreciated.Thanks in advance