Monday, February 8, 2010

The legend of Georgian ACM movement

The acronym "ACM" stands for the Association for Computing Machinery, which conducts the annual ACM International Collegiate Programming Contest, or simply ACM ICPC. This competition is widely recognized as the world championship, and its rules, possibly with mild alterations, are often used in other programming contests.

In general, the ACM rules suggest the following. The teams composed of one to three persons are provided with a set of mathematically-oriented problems and should code the solutions in one of the available languages. Upon writing the code, a team sends it to the server for evaluation on a set of testcases. Now, if the solution outputs the right answers in the time allotted and does not misperform in any way, the problem is marked as solved. Otherwise, the submission is rejected. Finally, the teams are ranked by the number of solved problems and by "penalty time" in case of ties, the latter depending on the number of wrong tries and solving time for each solved problem.

The contests which use the ACM rules are often referred to as ACM-style contests, and since they comprise the majority of the contests around, our whole activity can be called "The ACM movement".

I'm not sure about the exact birthdate of Georgian ACM movement but it's possibly around year 1999, when Tbilisi State University team participated in NEERC (the ACM ICPC semifinals region where Georgian teams participate) for the first time. For the next four years, the results have been stable but middling. The breakthrough came in 2004, when the TSU team, comprised of Nika Jimsheleishvili, Irakli Khomeriki and Revaz Khurtsilava, was just a problem away from the finals. Nika soon became the leader amongst domestic participants and his team fought every year to advance to the Finals. After a couple of line-up changes, the team Triumvirate with Nika, Giorgi Lekveishvili and me finally made it in 2008/2009 season.

You probably recognize all the names in the previous paragraph. There are several other people also worth mentioning when speaking about Georgian ACM movement, but this time I'd like to focus on one person instead. The person from whom I learned a lot in my olympic youth. The person who brought a lot into Georgian olympiads but never got the deserved credits. So, may I introduce the concealed legend of Georgian ACM movement - Davit Rachvelishvili aka rachvela.


I'll recall some of Davit's accomplishments, just to show you the whole picture. First of all, Davit was the first Georgian competitor on Topcoder and it was him who familiarized the rest of us with this truly helpful community. One can hardly overestimate the bonuses of Topcoder, including editorials and regular SRMs (which were far more regular in those distant times).
Davit also was the first of us to discover some algorithmic concepts which are extensively used in the contests nowadays - for instance, minimum cost maximum flow and fenwick trees.
Moreover, Davit was always willing to share his knowledge and help the fellow colleagues. Davit's easy temper and spiteless attitude were the perfect qualities for this. Sounds like an extract from a reference letter? Yep, but still maximally close to the truth ;)

Now let me tell about rachvela's way.

Chapter 1. The early years.
One might think that succeeding in programming, especially at the olympiads, is impossible without serious math and computer background from the very childhood. Now I'm telling you it's possible, since I have an example right before my eyes.
Davit never studied in a math-oriented school. He didn't bother studying at the normal one, either. Instead, he spent his adolescence breakdancing and hip-hopping (if you got the impression that he never went to school, that's false, he formally did). He still raps his self-written song from time to time, but his breaking will never be the same :D


Chapter 2. Engaging into olympiads.
When I was in eleventh grade, which was the final year in Georgian schools back in those times, I've set myself an objective of making it to the Georgian IOI team that year. So I worked hard and tried to participate in every contest that I found out about. UVA online judge hosted plenty of contests and I competed in the most of them. That was where I first encountered Davit. Soon, we also met at forum.ge and communicated this way for a long time. Davit was a freshman at Tbilisi State University and studied programming as diligently as me.

Now it's time for you to ask, hey, how did the break-boy leave the streets and find the aspiration for programming? Well, my friends, I doubt that there was any magic involved. Davit just grew up and found the occupation that was profitable, perspective and - the last but not the least - the occupation he liked. And once he found it, he did not stand back because of lack of knowledge or anything - he believed he's capable and he was going for it.


By the time when I joined TSU in 2005 (after failing the IOI team selection contests), Davit already was the leader of the university's second team. I also got on this team in early 2006 and we have competed together for several months, but had to break up before the start of the new season, since Davit went to the first team alongside Nika Jimsheleishvili and Giorgi Lekveishvili.

Chapter 3. The years of fame.
The first olympiad in which the new-formed first team took part was the All-Ukrainian Open Olympiad in Vinnytsia. This trip brought them the bronze medals of Ukrainian championship.

The next, and main, competition was NEERC 2006. We all pinned high hopes on the first team and believed it would advance to the Finals this time. Unfortunately, due to a malfunction of their machine (which was not properly compensated by the judges!) they lost a significant amount of time (and determination, I guess) and eventually finished 15 places behind the advancers.


The same team earned the first degree diplomas at the first Vekua Cup in April 2007. Davit was also granted the third degree diploma in the individual competition.

The last event where this team participated was KPI Open. The site stubbornly doesn't show the results for years before 2008, so I have to rely on my poor memory, which tells me that the boys took the third place then.


Since Nika had decided to skip the 2007/2008 ACM season, the teams reorganized again. Davit and Giorgi resurrected Irakli Khomeriki and began another advance-to-finals campaign. It was Davit's final year in baccalaureate and he wasn't going to pursue the Masters right away, so it was also his last chance to participate in ACM (because there also is a restriction on age and he would outgrow it). The team prepared thoroughly and was highly determined. However, eventually it all went completely wrong. Some really lame mistakes kept the first team well under the advancers zone. For Davit, the ACM Finals would remain a distant dream.

After the described events Davit quit programming contests, graduated from the university and fully concentrated on the job. We rarely heard of him in the course of the year and everybody, including him I think, thought his olympic career was over...

Chapter 4. The return of the Jedi.
...but we all were wrong. After having a year-long break, Davit entered The Georgian University of Saint Andrew the First-Called and, maybe inspired by Triumvirate finally advancing to the Finals and earning the Bronze medals, maybe just following the calling inside him, returned to the world of olympiads with all the diligence he had before.

The fortune did not turn its back on Davit, and he was granted permission to participate in ACM ICPC for one last season (the rules concerning the age were softened). His teammates were Avto Rukhadze, quite an experienced contestant, and Tornike Benidze, a debutant in ACM competitions. Hoping to advance to the Finals from NEERC in this situation would be barefaced impudence (those of you who ever took part in NEERC should understand). So the real fight was for the title of Champion of Georgia.

There were at least two teams from Tbilisi State University which were ought to beat Davit's in the domestic run - Tbilisi SU1 with ACM ICPC and IOI bronze medalist Giorgi Lekveishvili and IOI medalists Giorgi Nadiradze and Davit Tvalchrelidze, and Tbilisi SU2 composed of Giorgi Saghinadze, Andrew Lutsenko and Akaki Mamageishvili - this was their third successive season as a team, and they were quite serried.

But Davit managed to surprise us all. In a difficult contest, he managed to get the team on the top of Georgian teams ranklist, thereby finishing his ACM ICPC performances with the domestic Champion title and the head held high. An interesting thing is that both TSU teams named above lost to Tbilisi SU3 comprised of first-year students Zurab Kutsia, Nika Gabisonia and Beka Barbakadze. It's nice that the youth still keeps coming :)

Chapter 5. Epilogue.
So, I told you the story of Davit Rachvelishvili, one of the key figures in Georgian ACM movement evolution. You might ask me, what is he doing now?

Davit works as developer at JSC Georgian Card, where he performs stuff of all different sorts. He keeps claiming that Java is not an inch slower than C++, loves the Twilight Saga and still does not listen to metal.

Recently, Davit married his beloved Teo. You can look at the picture of the happy couple :)


And finally, even though ACM ICPC is definitely in the past, there are many other contests worth winning. And Davit knows that :) He still battles on Topcoder and is going to kick some asses at the next Google Code Jam. Moreover, Davit, Giorgi Lekveishvili and me are going to participate in Challenge 24 as Narwhals. Should be fun :)

Friday, January 22, 2010

SRM 459

It's been almost a year since my first and almost half a year since my last SRM, and I felt like making another one. So somewhen before the New Year I processed the ideas whirling in my head, wrote several problems and was offered the 459-th Single Round Match.

Div2 Easy - RecursiveFigures. The problem featured some eighth-grade level geometry, which, even with all the formulas in the statement, appeared to be not too easy (I guess there is nothing too easy for D2-250). The most interesting thing about it are the pictures by Lela Latsoshvili, who will be saving all of you from my mspaint-made illustrations from now on (I hope :D).

Div2 Medium / Div1 Easy - Inequalities. This problem can be solved in a number of ways, the most general of which has performance complexity of O(NlogN), where N is the number of inequalities, and can even produce the whole optimal intervals of values. However, since this is D1-Easy, the common solution to seek for is some neat brute force. With constraints as low as 1000, one could iterate over all possible values from -1 to 1001 with step 0.5 and check the answer for each of them - which is quite easy if you parse the input properly.

Div2 Hard - ParkAmusement. At first, the problem was deemed as too hard for Division 2, since it involved conditional probability - quite an uncommon topic for the second division. Even after the addition of thorough annotations, the tester still suggested to remove the conditional probability part - but I believed in Division 2, and Division 2 did not let me down. 24 successful submissions even made this my easiest D2-Hard yet. Not that I complain about it :) The key idea of the solution is that the probability of landing X being the starting landing is the ratio of (the probability of reaching an exit from X in K pipes) and (the probability of reaching an exit from any landing i in K pipes). Computing this probabilities is performed with dynamic programming, where the state is pair(landing, pipes left).

Div1 Medium - NumberPyramids. So, we have another problem featuring Lela's illustrations in place of my crooked landscapes. Though this time the problem itself can at least compete with its corresponding pictures. I was a little surprised with the high number of successful submissions for this problem, since its solution took me more efforts than, say, the SRM443's BinaryFlips. When I am not as sleepy as now at 3:30AM, I'll put the idea of the solution here.

Div1 Hard - TheContest
. Actually, at first I proposed another problem for this slot. However, it finally turned out to be way too hard, so I have modified my easiest problem from Vekua Cup Individual Contest's problemset - problem G, and suggested to use it. Unfortunately, eventually nobody solved it (though we were a lot more optimistic before the match).
The solution for the problem is as follows. First, let us consider N<=M. It's provable that if we have several rows already filled, then we always can fill the rest in spite of exactly how we've filled the previous rows. Therefore, the problem reduces to filling a row in a way not to conflict with already filled rows. For each position, let us try every number (which was not used in this column before) in ascending order. If it is possible to arrange the rest of the numbers in the columns to the right, then we take this number. And this possibility can be checked with the help of matching techniques: if we build a bipartite graph with the left-side nodes corresponding to columns and right-sides corresponding to numbers left, and the edge between left-side i and right-side j denoting that j was not used in i-th column, there must exist a maximum matching in this graph for the number to be feasible.
When N>M, we encounter a little problem. It is no longer true that we can fill the rows not thinking about the next ones. For example, for N=3 and M=2, filling the rows in this manner will give us {"12","21"} and the third row will be no longer fillable. It's easy to notice that every value must be used in the whole matrix a total of MIN(N,M)=M times. Therefore, an obvious restriction on the numbers to use is: if there is a number X such that if you don't use it in this row, then you won't have enough rows to use it a total of M times, you should use X in this row. A much less obvious thing is that this is also sufficient to fill the whole matrix row-by-row. So, for each row we should first identify all the values which have to be used in it and then fill this row, making sure that these values are used. But how to deal with this in the matchings we use to determine feasibility? Since augmenting paths in matchings never change the nodes matched (just edges), it's enough to perform all the augmentations for those must-use values and then match the rest of the columns to some of the values.
So, this is it. I still believe the targets just had a bad day, or the problem wouldn't stay virgin.

Alright, here is the round overview and that's all for now.

Friday, January 1, 2010

The red-colored New Year

So, the year 2009 is over. First of all, let me wish you all a happy and successful year 2010.

The previous year saw by far the greatest results of Georgian programming olympiads movement in whole and me in particular, the quintessence of which were the Bronze Medals at ACM ICPC 2009. But let me recollect them all in chronological order.

So, January began with two Topcoder SRMs prepared by Georgian writers - first of them by nika and the second by my very self. These were the first (at least as far as I know) international-level competitions with problemsets written by Georgians.

Our problemsetting experiment continued soon after at Winter Programming School in Kharkiv in February. The sixth day of the School was fully prepared by our team Tbilisi SU Triumvirate (Bogdanov, Jimsheleishvili, Lekveishvili) and its coach Temuri Zarkua. Also, we finished second in the overall raiting of the School.

Then, April came and we went to Stockholm in our quest for Georgia's first medals at ACM ICPC Finals. And the debut was really successful :)


Soon after the Finals, George also conducted his first SRM, and I had another two (440, 443).

Well, what else was there? In the end of June, George and me took the third place at KPI Open 2009. In September, I wrote the problemset for Vekua Cup 2009 Individual Competition. In November, Eduard and me took the second place at Open South Caucasian Championship 2009, the team Tbilisi SANGU 1 (Benidze, Rachvelishvili, Rukhadze) won the title of Champion of Georgia for the first time, and Nika took the sixth place at Google Code Jam Finals.

And now, for those of you who think 'What the hell is that red thing in the title', let's get to the final part of this post and my olympic year - my Topcoder rating. I firmly announced somewhen in Autumn that I would meet the New Year with red color at Topcoder, which means getting one's rating over 2200. I changed my handle from eyler to gojira_tc (simply gojira was already taken :|) and kept repeating that the new handle would only look good in red color. In the meanwhile, my rating graph kept falling and climbing, and finally December came with me still hanging close to the cherished line. I took the 11th place at the next-to-last SRM of the year and came closer to red than ever and was looking forward to the last SRM.

The last match of the year was on 6:00AM by Georgian time. Moreover, I had been celebrating the coming holidays on the evening of the previous day. So nothing really assumed that I would manage to keep my word, but still I decided to try. After performing decently in the coding phase, I was around the 50th place, which would guarantee a sufficient rating increase. However, I've made a couple of successful challenges, and looking at the division summary again, was utterly surpirsed, seeing my handle at the 5th place. The thing that surprised me even more was my score - 666 points. I decided to stop any challenge activity and keep the score. I found a code which was failing - at least I was sure I had the proper testcase. But I forced myself to leave it alone. After the testing, my position changed to #3, making it my best performance ever and giving me an unconditional red color. I saw that the code I thought was wrong actually passed the system test, which means I would spoil my great result if I tried to challenge it. Therefore, 666 points saved me.

So, now you can see my red-colored handle and complacent mug. As for me, I am going to take my friends to khinkali as a tribute to the forces which assisted me with their 666 points.

Thursday, July 9, 2009

KPI Open 2009

Как известно, международная открытая студенческая олимпиада по программированию им. С.А. Лебедева и В.М. Глушкова, или попросту KPI Open, - это мероприятие с наибольшим в Украине бюджетом среди всех олимпиадных баталий. С сожалением должен признать, что оно в то же время славится и как самое курьозное. В прошлые годы туры не обходились как без технических проблем, так и без ошибок в тестах или проверяющих программах. Тем не менее, эти факторы не становились решающими, и победители определялись более или менее справедливо.

Вот на эту самую олимпиаду нам и предстояло отправиться. Под "нами" подразумевается тренер Темури Заркуа и две команды по два человека: Tbilisi SU 1 в составе Леквы и меня, и Tbilisi SU 2 - в составе Гиоргия Сагинадзе и Акаки Мамагеишвили.

Прилетели мы за день до регистрации. Как обычно, Киев радует глаз в первую очередь обилием полураздетых красавиц, что сразу поднимает настроение. Настроение взлетело ещё выше, когда мои опасения не оправдались и вместо общежития с туалетом в другом конце коридора и душем на этаж ниже, мы оказались в профилактории, где все вышеупомянутые прелести жизни находятся в номере. Не меняя темы, мы собрались и ушли на пляж. В Киеве мы уже бывали, но почему-то на берег Днепра никогда не выходили. Чтож, всё бывает в первый раз © Ёлка

На пляже радости наших глаз уже не было предела. Воодушевлённый увиденным, Акаки, который по-русски говорит почти как я на китайском, решил выучить пару фраз и, естественно, обратился ко мне. Недолго думая, я предложил ему знакомиться с девушками фразой "Здравствуйте. Как к Вам можно обращаться?". Ну это шутка такая была. Но Акаки моего юмора не оценил и вот так и ходил по пляжу и обращался. Успешно по ходу :)


Второй день начался с регистрации команд. За что я люблю украинские олимпиады - регистрацию проводили главным образом девушки, которые ничем не уступали представительницам прекрасного пола, виденным нами снаружи KPI.



Далее в программе следовало само соревнование. KPI Open проводится в два тура: первый, отборочный, состоит из 4 задач на 4 часа. На втором, основном, предлагается 6 задач и 6 часов на их решение. Рейтинговая система на этой олимпиаде уникальная и совмещает в себе ACM, topcoder и школьные олимпиады. Спросить меня, так ничего в подобной смеси хорошего нет, но на вкус и цвет... Так или иначе, за решение по каждой задаче команда награждается неким количеством очков в зависимости от "веса" этой задачи, заранее определяемого компетентной командой судей, времени, потраченного на её решение и количества пройденных тестов.

В первом туре олимпиады разыгрывается сравнительно небольшое количество очков, поэтому написать его не очень хорошо совсем не значило потерять шансы на чемпионскую гонку. Многие сильные команды вообще пришли на него в неполном составе, надеясь бросить все силы в бой в основном раунде. Как выяснилось впоследствие, зря.

Так вот, первый тур. Самую лёгкую задачу, а вернее - задачу с наименьшим весом, на которую отводили 15 минут, никто не успел сдать даже за 20. Зато с той, которая была оценена в 45 минут, много команд расправилось быстрее, чем за десять. Ну это-то ладно, главное, что, как выразился позднее кто-то на закрытии, "задачи каждый год новые и оригинальные". На эти задачи вы сможете посмотреть, если их когда-нибудь выложат на сайте олимпиады. А до тех пор можете взглянуть на две задачи, похожие на предложенные на туре как две капли воды: Decoding Task и Amusing Numbers. По какому-то чудовищному совпадению, организационный комитет KPI умудрился не только заново изобрести такие же задачи, но и повторить текст условия слово в слово на разных языках. А если всё же преположить - чисто гипотетически [
© "Enemy of the State"] - что это именно те задачи, то особо весёлым может показаться пункт в правилах по поводу подачи аппеляций: "Поводом для подачи аппеляции может быть неточность перевода условий задач на русский и английский языки".

Во втором раунде невероятных совпадений уже не наблюдалось, а может, их просто не успели заметить, потому что примерно на 100-ой минуте соревнования произошел невероятный сбой сервера. В течение трех часов производились не особо успешные попытки его реанимировать. В это время участники олимпиады из нашей комнаты (Леква и я в первую очередь) успели нарушить практически все правила олимпиады: звонили, слушали музыку, спали на приставленных друг к другу стульях, выходили наружу. Все команды пообщались между собой и с заглядывавшими иногда тренерами. Когда мы собрались пойти за пивом, в класс зашел дядька из оргкомитета и с прискорбным видом объявил трагическую весть: организаторами было принято решение о прекращении тура. После этого в течение пары часов тренерами и представителями оргкомитета обсуждались возможные варианты развития событий, и в итоге было решено аннулировать результаты неоконченного второго тура и объявить победителей на основе первого. Не знаю, лучшее ли это было решение, но в создавшейся ситуации вообще не видно было нормального выхода. Так что вот так выглядят окончательные результаты.


Что касается культурной программы олимпиады, один день был выделен на прогулку по Днепру на катере и экскурсию по городу. Также каждый вечер в профилактории проводили так называемые трейнинги, а по сути игры. Ребята делились на команды и соревновались в предложенных конкурсах. Однажды нам раздали журналы и предложили сделать плакат из вырезок, на котором была бы отражена жизнь программиста в нашем понимании. В итоге участвовало шесть команд, каждая с довольно своеобразным видением данного явления.



Ну что ж, а потом мы улетели домой, но обещали обязательно вернуться :)

SRM 443

Примерно в начале апреля был объявлен конкурс на составление задач для onsite-раунда TCO 2009 - ежегодного чемпионата, проводимого топкодером. Вплоть до начала мая авторы могли засылать свои задачи, из которых должны были выбрать лучшие 12. Потом в течение мая их тестили и отбирали лучшие шесть - 3 для полуфинала и 3 для финала.

Я тоже решил испытать судьбу и предложил пару задач - один Easy и один Medium. В итоге в 12 кандидатов ни одна не попала. Во время моего второго SRMа я пообщался с Иваном по их поводу, и он сказал, что Easy была слишком сложной, а Medium - слишком лёгкой :) Тем не менее, он считал, что первую можно использовать как Div1 Medium для обычного матча, а вторую - как Div2 Hard. Так что я вскоре состряпал ещё 3 задачи и попросил на них взглянуть.

Сразу после окончания TCO, то есть в первых числах июня, Иван действительно посмотрел на задачи. Более того, он сразу предложил мне выбрать дату матча - честно говоря, проведя SRM только за месяц до этого, я этого не ожидал. На календаре значился только SRM 443, а остальные ещё не были прописаны, поэтому особо выбирать мне не пришлось.

В процессе тестирования я пару раз заменил Div2 Easy, а также придумал другой Div1 Hard. Наконец задачи были утверждены в том комплекте, который и попал на матч. Но вот по поводу распределения очков за задачи довольно долго шел спор. Вернее, главным образом спорили по поводу задачи BinaryFlips - той, что я задумывал как Easy для TCO. Мне она представлялась не такой уж сложной, и я требовал за неё чуть ли не 450 очков. Тестер и Метельский же, напротив, настаивали на 600. В итоге я сдался, и результаты ясно показывают, что я был неправ.

Что касается решений, в каком-то виде они лежат в editorialе на топкодере, но я всё же опишу свои варианты.

Div2 Easy - SoccerLeagues. Как и подобает задаче этого уровня, никаких особых знаний или оригинальных подходов в ней не требуется. На самом деле задача была даже легче обычного: правильно написать два вложенных цикла умудрились почти все участники.

Div2 Medium / Div1 Easy - CirclesCountry. Тут я ещё в условии прикольнулся, правда без знания грузинского языка этого всё равно не понять. Так вот, к этой задаче есть два подхода. Первый, более сложный - построить граф (а собственно дерево) из окружностей и найти в нём длину пути между окружностями, содержащими требуемые точки. Честно говоря, моё решение было именно таким, и я был довольно удивлён, когда в процессе тестирования нашлось намного более лёгкое: просто посчитать количество окружностей, которые содержат ровно одну из требуемых точек. Таким образом, для многих опытных участников проблемой стало то, что они сразу начали реализацию первого подхода и потеряли минут десять относительно людей, имплементировавших подоход №2.

Div2 Hard - Polygons2. Учитывая, что многие кодеры во втором дивизионе справились с первыми двумя задачами за 20-25 минут, по этой задаче было много (92) submission-ов. Тем не менее, большинство из них было просто перебором. В итоге правильными было только 5 решений, и самое быстрое из них заняло 42 с лишним минуты. Это меня хоть немного успокоило, потому что для меня задача была совсем не лёгкой. А решать её надо с помощью динамического программирования: ответ можно легко вычислить, если знать, сколькими способами можно получить (K-1)-угольник с наибольшей стороной длины A и периметром P. Эту начальную идею нужно было несколько оптимизировать, но в итоге код получался длиной в несколько строк.

Div1 Medium - BinaryFlips. В отличии от подходов, описанных на топкодере, моё решение имеет сложность O(1). Я бы его и туда добавил, но боюсь, что не смогу нормально объяснить. По крайней мере, пока здесь попытаюсь.
Давайте каким-либо образом проиндексируем все A+B чисел. В конечном итоге нам нужно изменить ровно A индексов по отношению к их начальному состоянию. Очевидно, что за один ход мы можем изменить
ровно K индексов. За два же хода можно изменить любое четное количество индексов в пределах от 2 до 2*min(K,N-K). Например, чтобы изменить ровно 2 индекса за 2 хода (при K>1), мы можем флипануть за первый ход некоторое множество индексов {i1,i2,...,i(K-1),j1}, а за второй - множество {i1,i2,...,i(K-1),j2}. Таким образом, относительно начального положения изменятся только индексы j1 и j2. Аналогично, для изменения большего количества индексов, надо брать меньше общих элементов для изменяемых на каждом ходе множеств. Именно поэтому невозможно изменить больше, чем 2*(N-K) индексов.
Так что мы имеем? Мы должны выразить число A в виде суммы чисел K и четных чисел между 2 и 2*min(K,N-K), причём цена числа K равна 1, цена остальных чисел равна 2, и сумма должна быть как можно дешевле. Вот тут уже надо рассмотреть несколько случаев:
1) A=0 => ANS=0
2) A=K => ANS=1
3) Если ни одно не выполняется, то K>=N => ANS=-1
4) (A%2=1) AND (K%2=0) => ANS=-1. В этом случае мы пытаемся получить нечётное число суммой четных.
Обозначим теперь Z=2*min(K,N-K) и функцию ceil(X)=наименьшее целое, большее или равное X.
5) (A%2=1) AND (K%2=1) => ANS=1+2*ceil(abs(A-K)/Z).
6) (A%2=0) AND (K%2=1) => ANS=2*ceil(A/Z).
7) (A%2=0) AND (K%2=0) =>
min(1+2*ceil(abs(A-K)/Z), 2*ceil(A/Z)).
Вдумавшись в каждое из этих выражений, можно (я надеюсь) понять, какая логика за ним стоит. Правда, написав всё это, я уже не думаю, что задача была такая уж и лёгкая :D

Div1 Hard - ShuffledPlaylist. Я снова написал задачу про музыку, правда, много наврал - никогда я ничего не shuffle-ю. Так или иначе, задачу можно было решать двумя методами. Для обоих общей частью является построение матрицы переходов из одного положения в другое. Положением в задаче описывается, песня какого стиля в данный момент проигрывается и сколько минут осталось до её окончания, плюс желательно иметь одно положение для начального момента.
Более сложный метод на этом останавливается и далее работает следующим образом. Если возвести начальную матрицу в степень X и посчитать сумму элементов, обозначающих переход от начального положения в конец песни для каждого стиля, мы получим количество плейлистов длины ровно X. Если мы как-то умудримся посчитать сумму таких элементов S(X) для всех степеней матрицы от 1 до X, мы получим количество плейлистов длины от 1 до X. Тогда ответом задачи будет S(maxLength)-S(minLength-1). А считать суммы в непрерывном интервале можно по формуле: S(1)+S(2)+...+S(X) = (S(X/2)+1)*[S(1)+S(2)+...+S(X/2)] (это при чётном X, при нечётном соответственно придётся отдельно прибавить элемент S(X)). Короче говоря, возни много.
Обойтись без суммирования матриц можно, если несколько видоизменить матрицу переходов, а конкретно - добавить в вершину, соответствующую концу плейлиста, из которой будут переходы только в саму себя. Тогда количество плейлистов длины от 1 до X будет храниться в элементе матрицы, соответствующем этой самой вершине.

В итоге SRM выдался довольно тяжелым, со всеми задачами в первом дивизионе справилось лишь 3 человека. Хотя в наших краях была глубокая ночь, и многие могут сослаться на это, оправдывая свои неудачи. Так или иначе, я снова получил удовольствие и до Нового года попытаюсь опять собраться с мыслями и выдать ещё один проблемсет ;)

SRM 440

После возвращения с зимней школы, у меня была пара свободных недель до начала следующего семестра в университете, и я уделил время созданию задач для своего второго матча на топкодере. На этот раз я написал 6 разных задач, но концепт обеих Easy и обеих Medium был общий, и в случае Medium-ов мне это очень нравилось.

Ответа я ждал примерно месяц. За это время сменился координатор задач на топкодере - Олексия сменил Иван Метельский. Ответил мне уже последний. Посмотрев задачи, он довольно скоро предложил мне выбрать матч - и я взял первый же неночной SRM после финала ACM.

Тестера на матч назначили в тот день, когда мы с финала вернулись, и им, к моей большой радости, оказался не кто иной, как Вася. За дело он принялся довольно скоро, а расправился с задачами ещё быстрее. Более того, он написал быстрое и красивое решение для div1 Hard, на которое я в итоге и заменил своё страшное и неповоротливое детище.

Опишу вкратце подходы к задачам:
Div2 Easy - IncredibleMachineEasy. Действительно, есть такая старая игра от компании Sierra, и когда-то я в неё играл. Было весело. Так вот, написав уравнение зависимости времени, ускорения и высот падений шариков, мы получим что-то этакое: T=sqrt(2*h1/a)+sqrt(2*h2/a)+...+sqrt(2*hN/a). Умножив обе части уравнения на sqrt(a)/T и возведя получившееся в квадрат, мы придём к формуле для ускорения: a=2*(sqrt(h1)+sqrt(h2)+...+sqrt(hN))/T]^2. Альтернативой этому подходу был перебор ускорения двоичным поиском: проверить, годно ли то или иное значение, можно подставив его в первоначальную формулу.

Div1 Easy - IncredibleMachine. Тут мы видим уже усложнённый вариант: в дело вступают наклонные плоскости. Решать задачу можно опять чисто аналитически, но в данном случае, на мой взгляд, намного легче применить двоичный поиск. Несмотря на то, что в условии даны были все формулы, кто-то всё равно посчитал его не достаточно ясным. Мне трудно судить, хотя учитывая, что большинство всё же решило задачу довольно быстро, наверное, условие всё же было понятным. Так или иначе, приносить задачи физического характера на топкодер довольно тяжело, потому что сложно описать все допущения и условия, принятые в задаче относительно классических законов физики.

Div2 Medium - MazeWanderingEasy. Задача эта возникла как дополнение к её более сложному собрату из первого дивизиона. Я на самом деле видел по Discovery программу, где подобным экспериментом показывали, как изменялись умственные способности мышей при прослушивании различной музыки. Несмотря на то, что я слушаю большей частью металл, подобная задача показалась мне весёлой (хотя кто-то воспринял условие MazeWandering как личное оскорбление :D). Что касается решения, надо было всего лишь найти путь в дереве между двумя вершинами и посчитать на нём количество вершин с decision-ами, то есть именно то, что сказано в условии.

Div1 Medium - MazeWandering. Я люблю задачи на вероятность, поэтому появление такой задачи на моём матче было лишь вопросом времени. Как только вспомню, как я её решал - сразу напишу ;)

Div2 Hard - WickedTeacher. Человеку, знакомому с динамическим программированием и решавшему задачи на битмаски, задача не должна показаться особо оригинальной. Понятно, что вероятность равна отношению [количества таких перестановок входящих фрагментов, для которых итоговое число делится на K] к [общему количеству перестановок, то есть собственно N!]. А для того, чтобы считать число в знаменателе, надо уметь считать NUM(mask,rem), то есть количество перестановок входных фрагментов, составленных из множества, определённого mask, и дающих при делении на K остаток rem. Всего аргументов у NUM может быть 2^N * K штук, что составляет примерно 3*10^6. В конечном счёте, в задаче было над чем повозиться, и решил её один-единственный участник.

Div1 Hard - SquareFreeSets. К этой задаче я видел два различных подхода. Первый, который я заметил в пяти из 7 прошедших systest решений и который описан в editorialе - перебор с запоминанием. Второй, который и использовал Вася - динамическое программирование. Положение описывается тремя параметрами: какие числа уже рассмотрены (а так как их можно рассматривать по возрастанию, то надо запоминать только наибольшее), сколько чисел взято в наше множество и маска простых чисел, которые уже использованы при составлении множества. Ясно, что маску всех простых запоминать невозможно - а достаточно запомнить маску тех простых, которые в квадрате не превосходят N. Чтобы не повторять варианты при рассмотрении чисел, делящихся на более большие простые, надо при рассмотрении простого числа, большего sqrt(N), сразу рассмотреть все кратные ему числа, и вообще исключить их из рассмотрения позднее.

Обзор матча можете посмотреть здесь. Сам я был очень доволен результатами, и матч был, на мой взгляд, очень интересный.

ACM ICPC World Finals 2009 - Stockholm

This Olympiad-related post is a stub. Unfortunately, you cannot alter its content by simply clicking the Edit button. However, you are welcome to leave the appropriate stories in the comments.