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

problem with the task\ С++\ Задача 1005
Posted by Giorgi 13 Jan 2011 02:16
program correctly solves all problems, but for some reason the site shows that the program is wrong ... can you explain why?

#include<iostream>
using namespace std;

int main()
{
    double ves_k[20];
    int i,num;
    double t,perv,vtor,res;

    cin>>num;
    if(num<1||num>20) goto vozvrat;
    for(i=0; i<num; i++){
        cin>>ves_k[i];
        if(ves_k[i]<1||ves_k[i]>10000) goto vozvrat;
    }
    if(num!=1){
    for(int a=1; a<i; a++)
        for(int b=i-1; b>=a; b--){
            if(ves_k[b-1]>ves_k[b]) {
                t=ves_k[b-1];
                ves_k[b-1]=ves_k[b];
                ves_k[b]=t;
            }
        }
    perv=ves_k[num-1];
    vtor=ves_k[num-2];
    for(i=num-3; i>=0; i--){
        if(perv>=vtor) vtor+=ves_k[i];
        else perv+=ves_k[i];
    }
    res=perv-vtor;
    if(res<0) res=-res;
    } else res=ves_k[0];

    cout<<res;
vozvrat:
    return 0;
}

Edited by author 13.01.2011 02:22
Re: problem with the task\ С++\ Задача 1005
Posted by atamachi 13 Jan 2011 10:21
print out your answer with end line.

cout << res << endl;
Re: problem with the task\ С++\ Задача 1005
Posted by Alexey Dergunov [Samara SAU] 13 Jan 2011 12:39
Greedy algorithm is wrong! You should check all 2^n situations.
Re: problem with the task\ С++\ Задача 1005
Posted by Giorgi 13 Jan 2011 17:36
Alexey Dergunov [Samara SAU] wrote 13 January 2011 12:39
Greedy algorithm is wrong! You should check all 2^n situations.
why the wrong ... it calculates all the results correctly, you can check yourself
Re: problem with the task\ С++\ Задача 1005
Posted by Alexey Dergunov [Samara SAU] 13 Jan 2011 18:23
You can find some tests on the forum and check yourself that greedy algo is wrong.
Re: problem with the task\ С++\ Задача 1005
Posted by Giorgi 13 Jan 2011 23:25
I know that the greedy algorithm is not always appropriate, but in this case, it should work