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

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

What my solution is Time Limit Exceed?
Послано bentham 15 май 2011 06:58
Hello

I have submitted my solution 2 times, and I get Time Limit Exceed, I dont understand why?, can someone help me, maybe its my loop, but I still dont understand, here its my code

import java.io.*;
import java.util.Arrays;

public class StonePile {

    public static void main(String args[]) throws IOException {
        StreamTokenizer str = new StreamTokenizer(new

BufferedReader(new InputStreamReader(System.in)));
        //int diff = 0;
        str.nextToken();
        int[] array = new int[(int) str.nval + 1];
        int array2[] = new int[array.length - 1];
        //System.out.println(array.length);
        int i = 0;
        int diff = 0;

        while (((str.nextToken() != StreamTokenizer.TT_EOL))) {

            if (str.ttype == StreamTokenizer.TT_NUMBER) {
                // System.out.println(i);
                array[i++] = (int) str.nval;

            }

        }


        for (int j = 0; j < array.length - 2; j++) {
            diff = Math.abs(array[j + 1] - array[j]);
            array2[j] = diff;
            // System.out.println("El array es en" + j+" es"+

array2[j]);
        }

        Arrays.sort(array2);
        System.out.println(array2[1]);



    }
}
Re: What my solution is Time Limit Exceed?
Послано AterLux 16 май 2011 12:14
may be there is no TT_EOL in input

you need not to wait EOL or EOF because in this task input has a determined size.

Read this: http://acm.timus.ru/help.aspx?topic=java.

you can call pair of "str.nextToken(); (int)str.nval;" so many times how first number you readed, without any checking for EOL or EOF.

after that you can get your WA ;)


Re: What my solution is Time Limit Exceed?
Послано bentham 17 май 2011 09:03
thanks for answer, so I still dont understand what the problem ask? in other thread it says that brute force search, can you explain me what the problem ask me to do, I need to generate permutation ?? I dont know much about algorithms


this is my code
import java.io.*;
import java.util.Arrays;

public class StonePile {

    public static void main(String args[]) throws IOException {
        StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        //int diff = 0;
        str.nextToken();
        int[] array = new int[(int) str.nval];
        int array2[] = new int[array.length];
        //System.out.println(array.length);
        int i = 0;
        int diff = 0;
        //System.out.println(array.length);
        //System.out.println(array2.length);

        for (int k = 0; k <= array.length - 1 && str.nextToken() <= k; k++) {

            if (str.ttype == StreamTokenizer.TT_NUMBER) {
                //System.out.println(str.nval);
                array[i] = (int) str.nval;
                //System.out.println(array[i]);
                i++;

            }
        }

        for (int j = 0; j < array.length - 1; j++) {
            diff = Math.abs(array[j + 1] - array[j]);
            array2[j] = diff;
            //System.out.println("El array es en" + j+" es"+ array2[j]);
        }

        Arrays.sort(array2);
        System.out.println(array2[1]);

    }
}

Edited by author 18.05.2011 04:03