ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

Md.Forhad Hossain solution [4] // Problem 1005. Stone Pile 29 Jan 2011 01:55
#include<iostream>
using namespace std;
int main(void)
{
    int input,weight,temp,i;
    long int sum=0;
    cin>>input;
    if(input<1 && input>21)
        exit(0);
    int ary[20];
    for(i=0;i<input;i++)
    {
        cin>>weight;
        if(weight>=1 && weight<=100000)
        {
            ary[i]=weight;
            sum+=weight;
        }
    }
    for(i=0;i<input-1;i++)
    {
        for(int j=0;j<input-i-1;j++)
        {
            if(ary[j]>ary[j+1])
            {
                temp=ary[j];
                ary[j]=ary[j+1];
                ary[j+1]=temp;
            }
        }
    }
    int x=input-1;
    if(x==0)
    {
        cout<<ary[x];
        exit(0);
    }
    long int k1,k2,middle;
    middle=sum/2;
    k2=ary[x];
    k1=ary[x-1];
    int r=0;
    int bol=0;
    for(i=input-3;i>=r;)
    {
        if((k1+k2)<=middle)
        {
            k2=k1+k2;
            k1=ary[i];
            i--;
        }
        else if(k1<k2)
        {
            if(bol==0)
            {
            k1+=ary[r];
            r++;
            bol=1;
            }
            else
            {
                k1+=ary[i];
                bol=0;
                i--;
            }
        }
        else
        {
            if(bol==0)
            {
            k2+=ary[r];
            r++;
            bol=1;
            }
            else
            {
                k2+=ary[i];
                bol=0;
                i--;
            }
        }
    }
    if(k2>k1)
        cout<<k2-k1;
    else
        cout<<k1-k2;
    return 0;
}
tanu Re: solution // Problem 1005. Stone Pile 20 Mar 2011 13:19
can u pls explain ths code..atr sortng wht hv u dn??
tanu Re: solution // Problem 1005. Stone Pile 20 Mar 2011 13:20
can u pls explain ths code..aftr sortng wht hv u dn??
ScaV Re: solution // Problem 1005. Stone Pile 29 Aug 2011 17:36
 if(input<1 && input>21)
??? is it possible?
zdw1224 Re: solution // Problem 1005. Stone Pile 24 Nov 2011 21:34
Consider the following situation : 3221

1) k2=3,k1=2
2) k1+k2=5>4 bol=0 k2>k1, so k1=k1+1=3, k2=3, bol=1
3) k1+k2=6>4 bol=1 k1>=k2, so k2=k2+2=5, k1=3, bol=0

-> result = 2,   but   31 | 22  -> 0

!!!