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

Обсуждение задачи 1209. 1, 10, 100, 1000...

Time Limit In Java
Послано RoninFeng 19 фев 2012 21:37
Hi!
Why could be time limit on 3 test here:

    private void solve() {
        int N = nextInt();
        String res = "";
        for (int i = 0; i < N; i++) {
            long idx = nextLong();
            double f = Math.sqrt(8*idx - 7);
            if (f - (int)f == 0) {
                res += "1 ";
            } else {
                res += "0 ";
            }
        }
        out.print(res);
    }

May i don't know some fast way to find if integer is perfect square?
I know that on C++ is all right, but want to understand how to make it in java.
Re: Time Limit In Java
Послано 2rf 20 фев 2012 21:07
I see you have quite a lot of accepted problems in java, so you're not novice. My advice is: generate a big test, then run some kind of profiler or just comment some lines of your code and see when you'll get biggest boost in execution time. After you find problematic line in your code it should become obvious how to fix it.
Re: Time Limit In Java
Послано RoninFeng 21 фев 2012 11:44
Tnx for advice) I agree it's the best way... I've just tried to find the fastest one in other people experience :)

----------

found the problem) string concatination is very expensive) for future generations - this works fine:

private void solve() throws IOException {
    int N = nextInt();

    byte[] outArr = new byte[N];
    for (int i = 0; i < N; i++) {
        long idx = nextInt();
        double f = Math.sqrt(8 * idx - 7);
        if (f - (int) f == 0) {
            outArr[i] = 1;
        } else {
            outArr[i] = 0;
        }
    }

    for (int i = 0; i < outArr.length; i++) {
        out.print(outArr[i]);
        out.print(' ');
    }
}

Edited by author 21.02.2012 12:16

Edited by author 21.02.2012 12:17
Re: Time Limit In Java
Послано 2rf 22 фев 2012 05:09
I'm glad it helped. For future: there is a StringBuilder class in Java, it's in fact a mutable string(contrary to immutable String class). It supports appending characters and strings to itself in linear time, so you could use it in this problem for example. It has other features of course, use Javadocs to find out more!