📜 ⬆️ ⬇️

Challenges marathon round of Yandex. Algorithm 2017

And again, as in previous years, the final of the Yandex.Algorithm contest is approaching. This year we have introduced a new round - marathon. It is a single optimization problem without an exact solution, which participants were asked to “twist” within 48 hours. This format is similar to solving practical problems more than popular competitions in sports programming.




A feature of most practical problems is the lack of an exact solution — or the algorithms for finding it turn out to be too slow. The team and the individual developer need to make a good prototype of the solution that will be implemented in the final algorithm. Tasks of this kind have long been encountered in TopCoder competitions, annual Marathon24 , Deadline24 , Google Hash Code and other competitions. The competition lasts longer than standard algorithmic rounds, so that participants can implement the invented method in a relaxed atmosphere and at a convenient time for themselves.


We, the organizers of the Algorithm, really want the diverse participants to successfully show themselves. Therefore, the addition of the marathon round is seen as a way to expand the audience and popularize such competitions.


We asked the participants who showed the best result to explain how they achieved it.


We suggest that you try to solve the problem ! Anyone who will show the best result before the final of the Algorithm, we will present a t-shirt with the symbols of the competition. The final will take place on July 18th. Both those who decided the round within the framework of the Algorithm and new participants are invited to participate.


The condition of the task "The separation of the country


View condition
Time limit:Memory Limit:Enter:Conclusion:
2 s256 MBstandard inputstandard output

In one elven country ripened. As often happens, after the death of the king, his sons for a long time could not decide which of them would rule the country, and in the end did not come up with anything better than to divide it into separate areas.


The elven country is represented by a checkered rectangle of n lines and m columns. In k cells there are sources of life, in k cells there are sources of magic, and all sources are in different cells. By a happy coincidence, the deceased king had exactly k sons, so they want to divide the country into k connected areas so that each region contains exactly one source of life and exactly one source of magic. A region is called connected if from any of its cells one can get into any other cell of this region, passing only on the cells belonging to the region and having a common side. Each cell must belong to exactly one area. Areas may contain “holes”, that is, one of the areas may be surrounded on all sides by another.


Input format


  • The first line contains three integers n, m and k (3 ≤ n, m ≤ 50, 3 ≤ k ≤ 16) - the size of the country and the number of sources of life and magic.
  • The following k lines contain pairs of integers rli, cli (0 ≤ rli ≤ n − 1, 0 ≤ cli ≤ m − 1) - coordinates of the sources of life.
  • The following k lines contain pairs of integers rmi, cmi (0 ≤ rmi ≤n − 1, 0 ≤ cmi ≤ m − 1) —the coordinates of the sources of magic.
  • It is guaranteed that all sources are in different cells, and also that for the specified input data there is at least one division into areas that satisfy the above conditions.

Output format


Print n lines of m Latin letters each: splitting the country into regions. Sections containing the same letters will be assigned to the same area. As the characters are allowed to use lowercase and uppercase English letters. A total of exactly k different letters should be used. Cells with the same letters must form a coherent region. Each area must contain exactly one source of life and exactly one source of magic.


Rating system


The partitioning provided will be rated using the following system:


  • If the split is incorrect (it contains disconnected areas, or the areas contain an incorrect number of sources, or the output format is not followed), your solution will receive 0 points for this test;
  • Otherwise, for each region, we define its “convexity” as the ratio of the area to the square of the perimeter of the area, where the area is the number of cells in the area, and the perimeter is the number of sides of the cells adjacent to other areas or with the border of the country ;
  • The sum of points for the test is the average “bulge” of all areas, multiplied by 160,000 and rounded to the nearest integer.

When sent out during the competition, your decision will be checked on a set of 50 preliminary tests. Your preliminary result will be equal to the average number of points for all tests. Pay attention to the “report” link next to the sent solution: it allows you to see on which tests your program successfully worked, and on which gave an incorrect answer or did not issue it at all. It is not possible to see both input and output data for a specific test.


After the end of the competition, your last sent and compiled solution will be tested on a full set of 250 tests (50 preliminary tests are not included in the full set). The average number of points for all tests from the complete set will be your final result.


Test generation


  • 40% of the tests in each set are completely random: n, m, k are selected equiprobably, after which the corresponding number of sources of life and magic are randomly distributed on the field.
  • 40% of tests in each set are generated as follows: n, m, k are selected equiprobably, after which k sources of life are randomly distributed to the field. Next to each source of life, a source of magic is randomly generated in some way so that the difference in coordinates on each axis does not exceed d, where d is a fixed number for the test from 1 to 4, chosen equiprobably.
  • 20% of tests in each set are generated as follows: n, m, k are selected equiprobably, after which a number s is chosen such that 2s ≤ min (n, m) and k ≤ 4 (s - 1). After that, two non-intersecting squares of size s × s were selected at random in the field, in one of which k sources of life were randomly distributed in some, and k sources of magic in another.

If at the next generation step in any of the algorithms no further action is possible, then the whole generation process begins again.


Example


inputconclusion
  4 6 3
 0 0
 13
 thirty
 3 5
 2 4
 2 2 
  aaabbc
 aaabbc
 aaabbc
 cccccc 

Notes


In the example there are three areas:


  • Area 'a': area 9, perimeter 3 + 3 + 3 + 3 = 12, “bulge” 0.0625
  • Area 'b': area 6, perimeter 3 + 2 + 3 + 2 = 10, “bulge” 0.06
  • Area 'c': area 9, perimeter 4 + 6 + 1 + 5 + 3 + 1 = 20, "bulge" 0.0225

Then the average "bulge" is equal to 0.048 (3), and the number of points for this test is 7733.


You can send solutions no more than once every 5 minutes.




Konstantin Khadaev (Russia, 7th place according to the results of the round)

First we find some paths from one source to another. This is a standard threading task. Some participants here used min-cost max-flow, but in my decision it did not give any gain, and in the latest version I left the algorithm Dinnitsa.

Next you need to somehow distribute all the other cells. I did this very primitive. We pass through all undistributed cells, choose a random neighbor. If a neighbor is given to a son, we give this cell to him. Otherwise, leave undefined. And so we iterate until we distribute all the cells. A small optimization: if three neighbors are given to the same son, we must give him a cage.
')
Of course, randomness can be added to both previous steps. Shuffle the vertices for Dynitsa, iterate over the cells in a random order.

Next we have some kind of map, we will optimize it locally.

Here I have three cases:
  • A cell has only one neighbor from the same territory as it, and at least two neighbors with another. Then she should be transferred there, with whom she has two neighbors.
  • The cell has two neighbors from the same territory and two from some other. Then the usefulness of the transfer depends on the areas of these territories.
  • The cell has one neighbor from the same territory and one neighbor from three more.

To quickly understand what is more profitable to do in cases 2 and 3, you need to maintain the area and perimeters of all territories. But each case gave a fairly noticeable increase.

Well, all this can be done many times while there is time, and choose the best.

In this form, the solution is already gaining quite a lot of points, but it often slips into local minima. A possible way to deal with this: in cases 2 and 3, even if the change does not bring benefits, it is still with some probability to do it, and this probability decreases with each iteration. This change increased the result by almost 200 points.

And about the feedback:
I did not like the fact that it intersects with the VK Cup. By the way, this also concerns the next round - it’s on the same day as the Russian code cup and Distributed code jam, I’m not going to pull out three contests on the same day. And the task is very pleasant, yes.

Eryx (Poland, 2nd place at the end of the round)

Thank you for the great competition and a very interesting challenge. It was a pleasure to work on it. The only thing I did not like was a slightly incomprehensible description of the generation of test data (“some random distribution” —but what?). Perhaps it was worth adding the “official” data generator (without the initial value), and we should leave a non-trivial part where we need to “make sure that the solution exists”.

A version of my solution with comments can be found here:
https://www.mimuw.edu.pl/~erykk/yandex/solution.cpp

I see that some of the final tests fell (although with public tests everything was fine). I had no idea to use min-cost max-flow to build paths. I think if I used this as an alternative approach, I would get the correct answers on the above tests, and possibly improve other results.
Original in English
Thanks for the great contest and interesting task! It was a pleasure to work on it. I'm not clear
statement of the test case generation (“some random distribution” - but what distribution?), possibly a “official” test generator could be even included (with no seeds), with the non-trivial part “make sure that a solution exists” left for us to implement (or solve manually).

The commented version of my solution can be found at:
https://www.mimuw.edu.pl/~erykk/yandex/solution.cpp

I see that I have failed some final tests (the public tests were all okay). I didn’t need to know how to use paths; You can get positive scores for these tests.

Ilya Kornakov (Switzerland, 6th place in the round)

I already wrote something in http://codeforces.com/blog/entry/51858?#comment-358766

I liked the task, according to the Java brakes module. The solution is as follows:
  • First, min-cost-max-flow searches for paths connecting sources and drains.
  • Then the paths expand greedily until the field is filled. Greedy steps are trying to add one by one cell, as well as a column / row segment to an existing component. The objective function is the change in the sk / (the number of added cells + 10).
  • The resulting solution is greedily optimized by throwing cells or column / row segments between components (with the same objective function).
  • The described process is launched many times, the best solution is chosen. The process is added randomly: the edges in the graph for the flow are mixed, the cores in greed are multiplied by a random number from 1 to 1.2.

Most of all points were brought by rewriting the solution from Java to C ++ (~ + 150). In addition, greedy improvements after the solution was generated - apparently a useful idea. From unrealized - try more changes in greed (for example, cut off the "corners" - a pair of sides) and instead of the flow do something that is better adapted to the target metric (since for the flow a long horizontal path is much better than the diagonals with the same horizontal displacement, and from the point of view of the target metric - much worse). Another idea was to try simulated annealing instead of greed, but there was no time left.

Source: https://habr.com/ru/post/331482/


All Articles