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

solution
Posted by lolpwnZzzz 19 Nov 2010 18:22
I've got AC on this solution. This solution isn't bruteforce
#include <iostream>
using namespace std;

int* sort(int*, int);

int main()
{
    int N;
    cin>>N;
    int *W = new int[N];
    for(int i=0;i<N;i++)
    {
        cin>>W[i];
    }
    sort(W,N);
    int pipe1=0;
    int pipe2=0;
    int weight=0;
    for(int i=0;i<N;i++)
    {
        weight+=W[i];
    }
    for(int i=0;i<N;i++)
    {
        if((pipe1+W[i])<=weight-W[i])
        {
            pipe1+=W[i];
            weight-=W[i];
        }
        else
        {
            pipe2+=W[i];
            weight-=W[i];
        }
        if(pipe1<pipe2)
        {
            int temp=pipe1;
            pipe1=pipe2;
            pipe2=temp;
        }
    }
    cout<<pipe1-pipe2;
    return 0;
}
where sort()is back ordered sort
Re: solution
Posted by Nickolas Pohilets 5 Dec 2010 07:10
Fails on { 2, 2, 3, 4, 5}. Should be 0 ({2, 2, 4} - {5, 3}), but returns 2 ({5, 2, 2} - {4, 3}).