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

Обсуждение задачи 1003. Чётность

very strange thing :O
Послано Tbilisi SU: Giorgi , Akaki , [Andrew] 9 фев 2009 03:44
At last got AC..
my algo is binary search on answer + dfs to check

in my code I had such fragment:
for (i = 0; i < n; i++)
{
    g[i].clear();
        color[i].clear();
    u[i] = -1;
}
and got TLE

then I changed clear() function to resize()
for (i = 0; i < n; i++)
{
    g[i].resize(0);
    color[i].resize(0);
    u[i] = -1;
}
and got AC in 0.2sec
can anybody explain what happend?
clear() works so slow???
Re: very strange thing :O
Послано Vedernikoff Sergey (HSE: EconomicsForever!) 9 фев 2009 03:57
I'm shocked as well!!! I don't know what place creators of STL wrote it through...

Tbilisi SU: Giorgi, Akaki, [Andrew], thank you for this trick!
Re: very strange thing :O
Послано Fyodor Menshikov 9 фев 2009 11:18
What about such explanation?

clear() frees memory, and the next time memory should be allocated again.

resize() just changes internal pointer to the end of array nothing is freed and nothing is allocated then.
Re: very strange thing :O
Послано Fyodor Menshikov 9 фев 2009 11:22
Vedernikoff Sergey (HSE: EconomicsForever!) писал(a) 9 февраля 2009 03:57
I don't know what place creators of STL wrote it through...

I think that 5x difference is because Timus compiler uses /O1 switch instead of usual /O2. In /O2 mode heap manager is very fast.
Re: very strange thing :O
Послано Tbilisi SU: Giorgi , Akaki , [Andrew] 9 фев 2009 14:24
I think it works much slower than 5x . because , number of edges in graph is not more than 10000 , binary search runs log(5000) ~ 12 times . for example if clear() needs O(n) time to clear vector , where n is number of elements in it, then total number of operations is 2*10000*12 = 240000, it seems that it runs 500 times slower...


Edited by author 09.02.2009 14:25