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

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

where the crash?:)
Послано roee1 7 ноя 2009 17:57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace _1005
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.In.ReadToEnd().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
            int[] array = new int[int.Parse(input[0])];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = int.Parse(input[i + 1]);
            }
            Array.Sort(array);

            int[] sum1 = new int[array.Length - 1];
            int[] sum2 = new int[array.Length - 1];
            if (array.Length == 1)
                Console.WriteLine(array[0]);
            if(array.Length==2)
                Console.WriteLine(Math.Abs(array[0]-array[1]));
            //length>2:
            sum1[0] = array[array.Length-1];
            sum2[0] = array[array.Length-2];
            for (int i = array.Length - 3; i > -1; i--)
            {
                if (sumary(sum1) > sumary(sum2))
                    sum2 = add(sum2, array[i]);
                else
                    sum1 = add(sum1, array[i]);
            }

            if (sumary(sum1) != sumary(sum2))
            {
                int dif = Math.Abs(sumary(sum1) - sumary(sum2));
                int big = 0, pos = 0;
                if (sumary(sum1) > sumary(sum2))
                {
                    int i = 0;
                    while (sum1[i] != 0&&i<sum1.Length)
                    {
                        if (dif - 2 * sum1[i] > 0)
                        {
                            if (sum1[i] > big)
                            {
                                big = sum1[i];
                                pos = i;
                            }

                        }
                        i++;
                    }
                    if (big != 0)
                    {
                        sum2 = add(sum2, sum1[pos]);
                        sum1[pos] = 0;
                    }
                }
                else
                {
                    int i = 0;
                    while (sum2[i] != 0&&i<sum2.Length)
                    {
                        if (dif - 2 * sum2[i] > 0)
                        {
                            if (sum2[i] > big)
                            {
                                big = sum2[i];
                                pos = i;
                            }

                        }
                        i++;
                    }
                    if (big != 0)
                    {
                        sum1 = add(sum1, sum2[pos]);
                        sum2[pos] = 0;
                    }
                }

            }
            Console.WriteLine(Math.Abs(sumary(sum1) - sumary(sum2)));

        }

        public static int sumary(int[] arr)
        {
            int sum = 0;
            for (int i = 0; i < arr.Length; i++)
                sum += arr[i];
            return sum;
        }

        public static int[] add(int[] arr, int num)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == 0)
                {
                    arr[i] = num;
                    return arr;
                }
            }
            return arr;
        }
    }
}