10032 - Tug of War

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

s4k1b
New poster
Posts: 1
Joined: Sat Jul 19, 2014 7:13 pm

Re: 10032 - Tug of War

Post by s4k1b » Sat Jul 19, 2014 7:20 pm

This code gives correct ans to all this inputs but still i get WA when i Submit
can anyone find me a critical input or tell me what is wrong with it?

Code: Select all

#include<stdio.h>
int swap(int *s,int start,int end,int start1)
{
    int sp,i,j;
    for(i=start,j=start1;i<=start+end;i++,j++)
    {
        sp=s[i];
        s[i]=s[j];
        s[j]=sp;
    }
}
int tugofwar(int *s,int l)
{
    int e,i,j,k;
    int s1,s2,s4,s3,d,d1,d2,m,ms1,ms2;
    m=451;
    for(e=0;e<l/2;e++)
    {
        for(i=0;i<l;i++)
        {
            for(j=i+e+1;j+e<l;j++)
            {
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                swap(s,i,e,j);
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                swap(s,i,e,j);
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                swap(s,i,e,j);
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                /*for(k=0;k<l;k++)
                    printf("%d ",s[k]);
                printf("\n");*/
                swap(s,i,e,j);
            }
        }
    }
    for(e=0;e<l/2;e++)
    {
        for(i=0;i<l;i++)
        {
            for(j=i+e+1;j+e<l;j++)
            {
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                swap(s,i,e,j);
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                swap(s,i,e,j);
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                swap(s,i,e,j);
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                /*for(k=0;k<l;k++)
                    printf("%d ",s[k]);
                printf("\n");*/
            }
        }
    }
        for(e=0;e<l/2;e++)
    {
        for(i=0;i<l;i++)
        {
            for(j=i+e+1;j+e<l;j++)
            {
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                swap(s,i,e,j);
                if(l%2==1)
                {
                s1=0;
                s2=0;
                s3=0;
                s4=0;
                swap(s,i,e,j);
                for(k=0;k<=l/2;k++)
                        s1=s1+s[k];
                    for(k=l/2+1;k<l;k++)
                        s2=s2+s[k];
                    for(k=0;k<l/2;k++)
                        s3=s3+s[k];
                    for(k=l/2;k<l;k++)
                        s4=s4+s[k];
                    if(s1>s2)
                        d1=s1-s2;
                    else
                        d1=s2-s1;
                    if(s3>s4)
                        d2=s3-s4;
                    else
                        d2=s4-s3;
                    if(d1<d2)
                        d=d1;
                    else
                    {
                        d=d2;
                        s1=s3;
                        s2=s4;
                    }
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                else
                {
                    s1=0;
                s2=0;
                swap(s,i,e,j);
                for(k=0;k<(l/2);k++)
                        s1=s1+s[k];
                    for(k=(l/2);k<l;k++)
                        s2=s2+s[k];
                    if(s1>s2)
                        d=s1-s2;
                    else
                        d=s2-s1;
                    if(d<m)
                    {
                        m=d;
                        if(s1<s2)
                        {
                            ms1=s1;
                            ms2=s2;
                        }
                        else
                        {
                            ms2=s1;
                            ms1=s2;
                        }
                    }
                }
                /*for(k=0;k<l;k++)
                    printf("%d ",s[k]);
                printf("\n");*/
                swap(s,i,e,j);
            }
        }
    }
    printf("%d %d\n",ms1,ms2);
}
int main()
{
    int s[100];
        int k,l,c,i;
        int a;
        a=0;
        scanf("%d",&c);
        for(i=1;i<=c;i++)
        {
        scanf("%d",&l);
        for(k=0;k<l;k++)
            scanf("%d",&s[k]);
        if(a)
            printf("\n");
        a=1;
        if(l==1)
            printf("0 %d\n",s[0]);
        else
            tugofwar(s,l);
        }
        return 0;
}

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 10032 - Tug of War

Post by lbv » Sat Jul 19, 2014 9:27 pm

s4k1b wrote:This code gives correct ans to all this inputs but still i get WA when i Submit
can anyone find me a critical input or tell me what is wrong with it?
Trying to figure out what is wrong with that code would be quite a feat; one that is out of my reach, I'm afraid. I suggest trying to simplify it.

You may try these:

Input

Code: Select all

4

8
4
97
50
11
96
45
27
52

8
433
385
124
27
416
366
169
145

16
59
329
446
124
171
32
430
237
4
398
217
261
354
263
142
292

16
319
37
92
253
446
180
369
137
382
443
23
144
371
229
222
31
Output

Code: Select all

187 195

1021 1044

1879 1880

1839 1839

Post Reply

Return to “Volume 100 (10000-10099)”