ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1005. Куча камней

Please anybody help me. Wrong answer. Java
Послано Sergey 22 авг 2016 22:13
import java.util.*;

public class StonesPile {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] pile = new int[a];
        for (int i = 0; i < pile.length; i++) {
            pile[i] = sc.nextInt();
        }
        Arrays.sort(pile);
        int pile1 = pile[pile.length - 1];
        int pile2 = 0;
        for (int i  = pile.length - 2; i >= 0; i--){
            if (pile1 <= pile2){
                pile1 += pile[i];
            }
            else{
                pile2 += pile[i];
            }
        }
       System.out.println(Math.abs(pile1 - pile2));
    }
}
Re: Please anybody help me. Wrong answer. Java
Послано Oleg Baskakov 22 авг 2016 22:34
Greedy is wrong, which you could find out by yourself by reading just a few threads below yours.
A simple counter test is
5
5 5 4 3 3
(5+5=4+3+3, so the difference is 0)
Re: Please anybody help me. Wrong answer. Java
Послано Sergey 23 авг 2016 02:40
If you can help write, if not don't write and i am not greedy. USA
Re: Please anybody help me. Wrong answer. Java
Послано Oleg Baskakov 23 авг 2016 10:44
Uhh... the algo is greedy, not you https://en.wikipedia.org/wiki/Greedy_algorithm and it doesn't work for this task, so try bruteforce maybe; though i see you got ac already anyway.
Re: Please anybody help me. Wrong answer. Java
Послано Sergey 23 авг 2016 14:21
sorry. I misunderstood