Tuesday, June 24, 2014

ACM ICPC World Finals 2014 Practice day

Tuesday started with the practice session. Back when I was competing in World Finals, the practice session had very easy problems, and it was mostly about testing the judging system behavior with respect to time, memory and stack limits. These days the practice session is called "Dress Rehearsal" and it has six quite difficult problems from the previous World Finals. Some teams chose to compete using those problems, and Zhejiang University, world champions in 2011, had the best penalty time after solving all practice problems. Of course, this will have no relevance in the real competition tomorrow.

The practice session was followed by Q&A about the judging system, and then a talk by Bill Poucher which announced several nice things: they're considering adding Python to the next year's available languages, and they're considering making the spot allocation system for the World Finals public and known in advance.

The rest of the day consisted of various fun activities for participants, like football acrobatics, dancing lessons and board games - the last two are pictured on the left. Only a few people participated in those, suggesting most contestants decided to rest in their hotels before the competition.

Tomorrow's competition will be broadcast with commentary at http://icpclive.com/ (more details in this Codeforces post), but I will still try to share my own thoughts via live commentary on this blog. Check back tomorrow!

UPDATE. According to this post, there will be an opportunity to solve the World Finals problems during the contest. In that case, I will be participating together with tourist and meret, and the commentary here will be mostly about our team's work.

Monday, June 23, 2014

ACM ICPC World Finals 2014 Opening Ceremony day

Today was the Opening Ceremony day at the ACM ICPC World Finals. In the morning, the participants listened to IBM's talks and participated in some fun activities - including a team graffiti workshop, the result is pictured on the left (photo by Pashka).

Later on, the ceremony itself took place. It was a mixture of very long talks and congratulations to people who make ICPC happen, and energetic performances by local dancers and musicians. Many awards went to people from Yekaterinburg, which makes sense given that they put tremendous effort into organizing this World Finals event - on of such awards is pictured on the right.

Some of the artistic performances were tuned to the audience, and those really stood out from the rest: the blacksmith melting metal into a smartphone, and the robots telling a story of ACM ICPC in 2187 were particularly funny in my opinion. The 'normal' performances, probably still of good quality, tended to feel disconnected from the event and thus less interesting to me. Maybe I've just missed something - was there anything else related to ACM ICPC in them?

One other topic I wanted to mention is Yekarinburg itself. I was here before, but this time the city feels much more friendly and convenient than it did a year ago. Much of it is probably due to the wonderful organizers of the World Finals who make sure everything is smooth, and the nice weather. But even taking that into account, it's still so nice :)

For example, traveling from the apartment that I rent to the ICPC zone involves taking a 20-minute tram ride, then walking along the very nice shore promenade, both without big crowds I'm so used to in Moscow, enjoying the very mixed architecture - with historic wooden buildings next to modern office towers.


Please don't get me wrong - the city is not as nice and clean as Zurich, for example. The aforementioned trams might well be older than I am and make a lot of noise, the streets still suffer from traffic jams and lots of parked cars everywhere. There is quite a bit of trash on the streets, the small city park that I randomly wandered into was just trees and mostly unpaved pathways, and construction sites form an epsilon-net in the city :) Many of the completed buildings are already deteriorating.

But still, the city feels nice and friendly both to myself in particular, and to the World Finals participants in general.
It might well be that I'm just biased for Russian cities, and tend to weigh positives way above negatives for them, though. Other World Finals participants reading this - how do you feel about Yekaterinburg? 

Sunday, June 22, 2014

This week in competitive programming

The week was relatively silent with just two rounds, both on Thursday. Most competitions decided against having rounds this weekend, most probably because many of the top competitors were traveling to one of the most important competitive programming event of the year - the ACM ICPC World Finals, this time in Yekaterinburg. I will try to post some updates from the event in my blog.

TopCoder SRM 625 was the first to go (problems, results, top 5 on the left, my screencast). Like in SRM 617, the most memorable moment of the match happened during the challenge phase, where I had to come up with a challenge case against an obviously incorrect solution. This time it was not very difficult, but still cute. The medium problem had a quite complicated statement which I can't really explain in a few words - so please read it if you want to understand the problem :) The problem boiled down to finding a minimum cut where both the vertices and the edges had capacities and could be cut. The correct way to do that is to duplicate each vertex of the graph, direct all incoming arcs to the first copy, all outgoing arcs from the second copy, and connect the first copy to the second copy with an arc of capacity equal to the capacity of the vertex. This way we reduce the vertex capacity case to the edge capacity case, and then we can use the standard maximum flow algorithm to find the minimum cut. The solution in question (TopCoder login required to view) skipped the duplication, and instead just propagated the capacity of the vertex to all adjacent arcs - if an adjacent arc had a higher capacity, it was reduced to the capacity of the vertex. It's not that hard to come up with a challenge case in the graph formulation - but a bit harder in the problem's grid formulation. Can you challenge this solution?

And of course, congratulations to Bruce on the convincing victory!

Codeforces Round 253 featured problems from one of the World Finals competitors, Boris from SPb ITMO team (problems, results, top 5 on the left). Gennady "tourist" won the match and was the only contestant to solve the problem "Gena and Second Distance". The background story of this problem, quite ironically, told that Gena (a reference to tourist himself) doesn't want to solve this problem and asks you to do it instead of him. Apparently he still had to do it after all :)

Thanks for reading, and check back tomorrow for some World Finals updates!

Wednesday, June 18, 2014

This week in competitive programming

Last week had a lot of very different rounds.

On Thursday, the long Russia Day weekend was kicked off by TopCoder SRM 624 (problems, results, top 5 on the left, my screencast). My medium problem failed because of the following reason: it asked to find the answer modulo one billion plus nine, whereas I mechanically returned the answer modulo one billion plus seven, since the latter modulo is the standard choice in such problems. That's not a good excuse, of course, and moreover, I actually noticed that the modulo was different when reading the problem statement, but forgot about that when I started coding. Does it mean that coding the "boring" part of the solution happens almost subconsciously?..

This problem didn't stop Gennady who won with a 100+ point gap. Congratulations!

On Friday, Codeforces ran a slightly unusual round - Zepto Code Rush 2014 (problems, results, top 5 on the left). The round had prizes, but somehow that did not attract more contestants than usual: 2148 contestants submitted at least one problem, compared to 2491 in the last Div1/2 round (it happened on Sunday, so that might account for higher turnout). I've managed to do a very stupid thing again: the hardest problem from this round was a slightly easier version of a problem from Petr Mitrichev Contest 11, so I immediately knew how to solve it and could even find working code for the more difficult version. Naturally, I was the first to submit a solution, and it failed the system test - the Codeforces problem allowed items to cost 0, while my problem didn't, and the solution didn't handle those correctly.

Nikolay (KAN) didn't solve this problem but solved all the rest, added five succesful challenges, and won the round - great job!

On Saturday, 25 people won a trip to Los Angeles in Google Code Jam 2014 Round 3 (problems, results, top 5 on the left). The advancers were decided on the two most difficult problems C and D. Problem C was tricky algorithmically, but easy to code; problem D was more straightforward but had a lot of small details and required a lot of time to code. In the end nobody solved problem D in full, so people solving problem C were at the top. Congratulations to Egor on winning this round!

Early on Sunday, the 25 contestants who will go to Berlin were decided in Yandex.Algorithm 2014 Round 3 (results, top 5 on the left).

People going to the final round were decided based on the sum of three rounds, here are overall elimination round results, top 5 on the left. Evgeny (eatmore) won a very convincing first place in the overall results - congratulations! It's interesting that just 7 contestants got in top 25 in both Yandex.Algorithm and Google Code Jam: eatmore, tourist, hos.lyric, dzhulgakov, vepifanov, sdya, Jedi_Knight. Code Jam had a lot more competitors overall, but one might expect that most of the best competitors would participate in both rounds, so the randomness in such selection systems is still very big.

And finally, Internet Problem Solving Contest 2014 rounded up the week (problems, results, top 5 on the left). This is a very exciting competition that has created a format of its own. It happens just once a year and doesn't have any prizes, but it still attracts a lot of contestants, including those who don't participate in any other competitions, like Reid Barton. The reason is very simple: every year it has a very diverse set of problems, and the problems are never boring, but are always interesting and satisfying to solve. It's hard to describe what that means exactly, so I'll just provide links to different types of problems from this year's contest: a very difficult problem that could appear at a "normal" ACM-style contest, a problem requiring to analyze the hashing code in the C++ and Java standard libraries, a problem that asks you to recognize facial expressions - but only on the surface; I'm pretty sure no team who solved this problem actually analyzed the faces. Can you see the trick?

Congratulations to Reid, Tomek and John on an amazing victory!

Thanks for reading, and see you next week (well, in 4 days)!

Tuesday, June 10, 2014

This week in competitive programming

The week's events started with TopCoder SRM 623 (problems, results, top 5 on the left) at 5am Moscow time. Many of recent Moscow 5am SRMs have still been unexpectedly won by Russians despite the lack of sleep, but this time we got the more natural result - Mark from the United States claimed the top spot, congratulations! This was his second SRM win after almost five years delay - the previous one was SRM 447 back in August 2009.

On Friday, Yandex.Algorithm 2014 Round 2 took place (results, top 5 on the left, my screencast). The problems were more difficult than in the first round, but the optimal strategy was the same - submitting one or two problems blindly (and correctly) was enough to get a place that guarantees or almost guarantees a spot in the onsite round in Berlin.

TopCoder Open 2014 Round 2B selected another 50 lucky contestants on Saturday (problems, results, top 5 on the left). Looking at the advancers from rounds 2A and 2B by country, the top 5 countries are Russia - 25 contestants, China - 17 contestants, and Poland - 10 contestants. Last year at the same point the top 3 countries were the same, but there were 34 advancers from Russia, 12 from China and 10 from Poland - so the Chinese have made the biggest progress this year (and Russians have the biggest fall).

At the same time, the 50 advancers from Round 2A could solve the same problems in an alternative round called TopCoder Open 2014 Parallel Round 2B (results, top 5 on the left, my screencast). However, there was yet another option for people who advanced from Round 2A: they could set problems for Round 2B - and tourist did exactly that, setting the easy and the hard problems. The easy problem was a pretty greedy: you have N lamps, each lamp can be on or off, and you want to iterate through several states of the lamps in order. Each of the states describes the state of all lamps: on, off or doesn't matter. When switching from one state to another, you can switch lamps off or on, and you spend 1 minute if you don't touch any lamps, 2 minutes if you only switch some lamps on or only switch some lamps off, and 3 minutes if you do both switching on and switching off. What is the smallest time needed to iterate through given states in given sequence?

And finally, Russian Code Cup 2014 became the first competition of the current bunch to select its onsite competors in the final online round (problems, results, top 5 on the left, my screencast). 50 top finishers will come to Moscow in October for the championship round, and also receive nice gifts from the organizers: three years ago it was an iPad, two years ago it was Lego Mindstorms, and last year it was AR.Drone 2.0. Last two gifts are good examples of things software engineers will likely not buy for themselves, but are very happy to play with - so they are very good as such gifts, in my view.

The hardest problem of the round had a very simple statement: you are given two cells on an infinite grid, (x1, y1) and (x2, y2). You need to move the robot from (x1, y1) to (x2, y2) in exactly t turns, where each turn consists of moving the robot from a cell to one of its 4 adjacent cells. You're not allowed to move the robot to cells where at least one coordinate is zero or negative, and you're not allowed to visit (x2, y2) before the t-th turn. How many ways are there to achieve these goals? All five numbers in the input are up to 250000.

The problem statement also had the following clause, not very important at first sight: output the answer modulo 998244353. Many combinatorial problems ask for an answer modulo a large prime number, to allow contestants to operate within the range of the programming language's integer types. But in most cases the modulo is 1000000007 - a billion plus seven. Here, the modulo was unusual, and it was actually a very strong hint towards the solution. Not only is 998244353 a prime number, the number that is 1 less divides by a large power of two: 998244352=223*7*17. Why is that important? It means that there's some number X such that X223 = 1 (mod 998244353), and X222 != 1 (mod 998244353). And that, in turn, allows the Fast Fourier Transform to work over remainders modulo 998244353 for polynomials of degree up to 223, which is conveniently greater than 250000.

Can you see how Fast Fourier Transform helps in this problem?

Thanks for reading, and see you next week!

Sunday, June 1, 2014

This week in competitive programming

This week's contests started with TopCoder SRM 622 early on Thursday (problems, results, top 5 on the left). The medium problem was a nice (spoiler alert!) greedy algorithms practice. You're given a tree with edge lengths and a constant M, and need to remove some edges to separate the tree into the smallest number of connected parts in such a way that each part's diameter is at most M. A diameter of a graph is the maximum of lengths of all shortest paths between its nodes. Can you see how and why it can be solved greedily? Even the official editorial suggests to use dynamic programming here :)

This round also made SPb SU 4 an all-target (3000+ TopCoder rating) team: Dima, rating 3027, 18th in the world, Egor, rating 3019, 20th in the world, and Pasha, rating 3001, 24th in the world. Congratulations!

The weekend had a bunch of elimination rounds for "big" contests. I guess it's not that much of a coincidence - most contests have elimination rounds in spring and onsite finals in autumn. The first to go was Google Code Jam 2014 Round 2 on Saturday (problems, results, top 5 on the left). It narrowed the field from 3000 competitors still hoping to win the grand prize to 500 - just one online round left before the finals in Los Angeles!

Early on Sunday, Yandex.Algorithm 2014 Elimination Round 1 became the first test of various strategies for the blind/open format (results, top 5 on the left). Unfortunately the problems are not visible to those who's not logged in, so it doesn't make much sense to discuss them here. As for the strategies, this time getting a good result required solving six problems correctly and at least 2-3 of them blindly.

Later on Sunday, Russian Code Cup 2014 Quaification Round 4 (problems, results, top 5 on the left) was the last chance for everybody to qualify for the Elimination Round. Congratulations to Fefer.Ivan on solving all problems correctly almost half an hour before everybody else!

And finally, Codeforces Round 250 rounded up the busy Sunday (problems, results, top 5 on the left). The round had a very standard problem as problem C - to count the number of triangulations of a non-convex polygon - but it still held a surprise! The usual solution splits the problem into two parts: first, we solve the tedious geometric problem to find whether each diagonal is inside the polygon. Then, we use dynamic programming that counts the number of triangulations of each sub-polygon formed by taking consecutive edges of the polygon plus one diagonal. The dynamic programming transition is obtained by considering which will be the third vertex of the triangle containing the diagonal as its side, and thus separating the problem into two analogous problems.

bmerry, though, has managed to avoid the geometric part almost completely! Here's his code: codeforces.com/contest/438/submission/6768704. Instead of checking that the triangle has valid diagonals as its sides, he just checks that the triangle is oriented correctly. Initially, this doesn't seem a sufficient condition, since the polygon is non-convex and thus even a correctly oriented triangle might intersect another side of the polygon. However, it turns out that in such cases there will be zero triangulations of the part that has a self-intersection. In other words, we'll find no way to find the next triangle some way down the dynamic programming. Is there an easy way to prove that?

Thanks for reading, and see you next week!

Friday, May 30, 2014

Coming up with tough dynamic programming solutions

Let’s talk about the hard problem from TopCoder SRM 619.

Consider two sequences of N (up to 100) integers, each integer between 1 and M. We call them similar if it's possible to remove at most two elements from each sequence so that they become equal. How many pairs of similar sequences are there for the given N and M (modulo 109+7)?

It’s quite natural to expect that this problem can be solved by dynamic programming: add characters one by one to both strings, and remember something about the characters already added. But how do we figure out what to remember – in other words, what the dynamic programming state is?

It occurred to me that we can approach this question itself algorithmically. Let’s start with the trivial dynamic programming – when we remember the entire strings. In other words, the solution simply is: answer(N, M, [], []), where answer(N, M, prefix1, prefix2) = {1 if len(prefix1) == N and the largest common substring of prefix1 and prefix2 is at least N-2; 0 if len(prefix1) == N and the largest common substring is less than N-2; sum(answer(N, M, prefix1 + [c1], prefix2 + [c2]) for all numbers c1 from 1 to M and for all numbers c2 from 1 to M otherwise}.

This algorithm has O(M2N) states, so it’s way too slow for any reasonable constraints. But we can still implement it and run for very small values of M and N. And then we can notice that some states of our DP are actually equivalent. First, if len(prefix1) ==  N, then there are just two equivalence classes of states: where then answer is 1 and where the answer is 0. Then, for each state with len(prefix) == N-1 we can check how many pairs of (c1, c2) lead to a state with answer 1 (the rest lead to a state with answer 0), and this is the only thing that matters – so there will be at most M2+1 different states. In the general situation, the states with length A are classified based on the multiset of states with length A+1 that are reachable from them.

This allows to write a program that will find the equivalence classes of states, and thus might give us a clue on what do we really need to remember in the DP state! The program is available here, here’s its output for N=6, M=2:
For len 6 there are 2 representatives.
For len 5 there are 4 representatives.
For len 4 there are 10 representatives.
For len 3 there are 9 representatives.
For len 2 there are 4 representatives.
For len 1 there are 2 representatives.
For len 0 there are 1 representatives.

So we have just 32 different states out of the total 5461 states!

But how do we translate this knowledge into a better solution? Well, the program referenced above also prints out the equivalence classes of states with length 3:
aaa$baa bba$baa aba$aaa bbb$bab bbb$abb aaa$aba abb$bab abb$bbb baa$aba baa$aaa aab$abb bab$bbb baa$bba aba$baa bab$abb abb$aab 
aaa$bbb bbb$aaa 
aaa$bba bba$aaa aab$bbb bbb$aab 
aaa$bab bbb$aba aba$bbb bab$aaa 
bba$aab aab$bba 
bba$aba aab$bab aba$bba bab$aab 
bba$abb bba$bab bba$bbb aba$aab bbb$bba aba$abb aab$baa aaa$aab baa$aab abb$bba aab$aaa aab$aba bab$bba bab$baa baa$bab aba$bab bab$aba abb$aba 
bba$bba bbb$bbb aba$aba aaa$aaa aab$aab bab$bab baa$baa abb$abb 
bbb$baa aaa$abb baa$abb abb$baa baa$bbb abb$aaa

By looking at those equivalence classes, we can notice certain symmetries. First, replacing a with b and vice versa clearly doesn’t change the state. Also, swapping the first and the second string doesn’t change the state.

Now we can use this knowledge to obtain a faster solution. Instead of considering all M2N possible states, we can already merge the states that differ only by swapping the strings and permuting the alphabet. We modify our program (new version available here) and run it for larger values of N and M, hoping to gain more insight. The result of a run for N=7, M=3, and printing equivalence classes of length 4 is available here.

We can notice two things: first, even for N=7 and M=3 there are only a handful of truly different states; and then, there are much more situations where states are equivalent which we’re still missing.

And now comes the part that I still haven’t figured out: can we make the jump from the above output to the final idea required to solve this problem? The final idea is: we should only remember the last 3 characters from each string (again up to a permutation of the alphabet), and the answers for the following 9 questions: what is the largest common subsequence of prefix1[1..l1] and prefix2[1..l2] for l1 from len(prefix1)-2 to len(prefix1) and for l2 from len(prefix2)-2 to len(prefix2). Since the largest common subsequence of prefix1 and prefix2 must be at least len(prefix1)-2 (otherwise we’ll be unable to complete both sequences in such a way that the problem constraints are satisfied), there’re only a few such states possible. This idea is explained in more detail in the editorial.

Can you see how the output of our program can lead to such insight? Do you have ideas on how to complete this approach? Are there any interesting articles that use this approach that I should read? Having a disciplined way to come up with dynamic programming solutions sounds quite nice :)