tag:blogger.com,1999:blog-19533250797934499712018-05-14T17:57:56.456+03:00Algorithms Weekly by Petr MitrichevPetr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.comBlogger362125PetrMitrichevhttps://feedburner.google.comtag:blogger.com,1999:blog-1953325079793449971.post-64786701588301197262018-05-14T00:55:00.001+03:002018-05-14T01:12:11.090+03:00A uniform combination week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-QmvBzeWEl78/WvinoGUwKRI/AAAAAAACCjw/jg73-0vsIkcnY98SBLf8iFmbnXPaIfkTQCLcBGAs/s1600/agc023top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="424" data-original-width="1176" height="230" src="https://3.bp.blogspot.com/-QmvBzeWEl78/WvinoGUwKRI/AAAAAAACCjw/jg73-0vsIkcnY98SBLf8iFmbnXPaIfkTQCLcBGAs/s640/agc023top5.png" width="640" /></a></div>Most teams have returned home from Beijing by the end of the Apr 23 - Apr 29 week, and the other contests returned in full swing. AtCoder Grand Contest 023 took place on Saturday (<a href="https://agc023.contest.atcoder.jp/assignments">problems</a>, <a href="https://agc023.contest.atcoder.jp/standings#page_1">results</a>, top 5 on the left, <a href="https://img.atcoder.jp/agc023/editorial.pdf">analysis</a>). The round was won by none other than the newly minted World Champion cospleermusora (also known as V--o_o--V and overtroll). yutaka1999 was also able to solve all problems, but required 30 more minutes to do so. Congratulations to both!<br /><br /><a href="https://agc023.contest.atcoder.jp/tasks/agc023_f">Problem F</a> was very cute. You are given a tree with 200000 vertices, each containing a number, either 0 or 1. We need to put all its vertices in some order in such a way each vertex except the root appears to the right of its parent. We will then write out a sequence of 0s and 1s corresponding to numbers in the vertices in this order. What is the minimum possible number of inversions in this sequence? An inversion is a pair of positions such that the number in the left position is 1, and the number in the right position is 0.<br /><br />Maybe I enjoyed this problem because I have set <a href="https://codejam.withgoogle.com/codejam/contest/3224486/dashboard#s=p1">a problem</a> in the past which involved the same strategy for producing a string from a tree.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-W4BfigQ4aI0/Wvi3pWpziWI/AAAAAAACCkw/lyLl4hj5pMsdyNw4_HIMJzUutvgrpJ7BgCLcBGAs/s1600/vk2018r3top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="270" data-original-width="1108" height="154" src="https://3.bp.blogspot.com/-W4BfigQ4aI0/Wvi3pWpziWI/AAAAAAACCkw/lyLl4hj5pMsdyNw4_HIMJzUutvgrpJ7BgCLcBGAs/s640/vk2018r3top5.png" width="640" /></a></div>VK Cup 2018 Round 3 happened on Sunday (<a href="http://codeforces.com/contest/925">problems</a>, <a href="http://codeforces.com/contest/925/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/contest/966/standings">parallel round results</a>, <a href="http://codeforces.com/blog/entry/59173">analysis</a>). The ICPC champions, this time two of them, kept with their champion-y ways and solved two more problems than everybody else. Unbelievable!<br /><br />I found <a href="http://codeforces.com/contest/966/problem/C">problem C</a> exceedingly beautiful. You are given 100000 numbers up to 2<sup>60</sup>. You need to put them in such order that the xors of all prefixes of the resulting sequence form a strictly increasing sequence themselves.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-v-6gauJZYEo/Wvis3uM758I/AAAAAAACCkI/mxNsmuYj4W4Otnamx4mclNobu9JiElA0wCLcBGAs/s1600/gcj18r1btop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="322" data-original-width="1078" height="190" src="https://3.bp.blogspot.com/-v-6gauJZYEo/Wvis3uM758I/AAAAAAACCkI/mxNsmuYj4W4Otnamx4mclNobu9JiElA0wCLcBGAs/s640/gcj18r1btop5.png" width="640" /></a></div>Right after the Codeforces round ended, Google ran Code Jam 2018 Round 1B (<a href="https://codejam.withgoogle.com/2018/challenges/0000000000007764/dashboard">problems</a>, <a href="https://codejam.withgoogle.com/2018/challenges/0000000000007764/scoreboard">results</a>, top 5 on the left, <a href="https://codejam.withgoogle.com/2018/challenges/0000000000007764/analysis">analysis</a>). overtroll has continued his impressive form (see above) with another victory, this time with a healthy margin of 12 minutes. Well done!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-z2oSFTVacxw/WvizMk4WyjI/AAAAAAACCkY/x7ZM5pCt-OIBGdqhQn7c1I6tbIhqGfgbwCLcBGAs/s1600/00001IMG_00001_BURST20180414150420_COVER.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://3.bp.blogspot.com/-z2oSFTVacxw/WvizMk4WyjI/AAAAAAACCkY/x7ZM5pCt-OIBGdqhQn7c1I6tbIhqGfgbwCLcBGAs/s640/00001IMG_00001_BURST20180414150420_COVER.jpg" width="640" /></a></div>In <a href="https://petr-mitrichev.blogspot.com/2018/05/an-alma-mater-week.html">my previous summary</a>, I have mentioned a World Finals problem: there are <i>n</i> people, each starting with 1 gem. The following operation is repeated <i>d</i> times: take one of the gems uniformly at random, and split it into two gems (so the person holding it will have one more gem). After doing all that we order all people by the number of gems they have in decreasing order, and add up the number of gems of the first <i>r</i> people in that order. What is the expected value of that sum? <i>n</i> and <i>d</i> are at most 500.<br /><br />The first part of the solution is understanding the sequence generation process. At the first sight, it looks rather complicated, with the probability to get a new gem for each person changing along the way. However, let's look at the process from the following angle: let's put all gems for all people in one sequence, and every time a gem is divided we'll insert a new gem to the right of it. We'll also distinguish the <i>original</i> gems — the ones people start with — from the newly generated ones.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-QTyfr6EOUG8/Wvizt_WHvpI/AAAAAAACCkg/OG3sQBozJnYDl_Sg6M9aDgcthG_qGoGVgCLcBGAs/s1600/IMG_20180513_144831.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="581" data-original-width="1451" height="160" src="https://2.bp.blogspot.com/-QTyfr6EOUG8/Wvizt_WHvpI/AAAAAAACCkg/OG3sQBozJnYDl_Sg6M9aDgcthG_qGoGVgCLcBGAs/s400/IMG_20180513_144831.jpg" width="400" /></a></div>We start by having <i>n</i> original gems, and we can notice now that at each step we simply insert a new gem into a position in our sequence that is picked uniformly at random, except from the position before the first original gem which is never used. This in turn makes it clear that the resulting sequence starts with an original gem, and then has a sequence of <i>n</i>-1 original gems and <i>d</i> new gems picked uniformly at random from all C(<i>n</i>-1+<i>d</i>,<i>d</i>) such sequences. Each person gets all gems from their original gem to the next original gem in this sequence.<br /><br />A uniformly chosen combination is a well-known object which is easy to work with, which allows us to proceed with solving the problem using either dynamic programming or more combinatorics, as outlined in <a href="http://www.csc.kth.se/~austrin/icpc/finals2018solutions.pdf">the semi-official analysis</a>.<br /><br />Thanks for reading, and check back around the next weekend for more!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/0J76T3h-Wgg" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com2http://petr-mitrichev.blogspot.com/2018/05/a-uniform-combination-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-55109284842683898302018-05-13T23:54:00.000+03:002018-05-13T23:54:05.583+03:00An alma mater week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-prVtA6QEaOk/Wvia0rzC6VI/AAAAAAACCjI/b1LFGWGXkegqyE5iw815QZdLRQ8gjBAtQCLcBGAs/s1600/cf475top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="271" data-original-width="1102" height="156" src="https://4.bp.blogspot.com/-prVtA6QEaOk/Wvia0rzC6VI/AAAAAAACCjI/b1LFGWGXkegqyE5iw815QZdLRQ8gjBAtQCLcBGAs/s640/cf475top5.png" width="640" /></a></div>The Sächsilüüte week started with Codeforces Round 475 (<a href="http://codeforces.com/contest/963">problems</a>, <a href="http://codeforces.com/contest/963/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/58991">analysis</a>). Three contestants managed to solve all problems, but some were faster than the others :) In particular, V--o_o--V has finished after just 81 minutes, and thus won with a 500-point margin. Well done!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-fgW5HXAQlYo/Wvicor07MDI/AAAAAAACCjU/g-Eo90aXArEU7hv89KIYjd8LrEqBh7rMQCLcBGAs/s1600/icpc2018top13.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="745" data-original-width="1514" height="314" src="https://2.bp.blogspot.com/-fgW5HXAQlYo/Wvicor07MDI/AAAAAAACCjU/g-Eo90aXArEU7hv89KIYjd8LrEqBh7rMQCLcBGAs/s640/icpc2018top13.png" width="640" /></a></div>One of the main competitive programming events of the year, ACM ICPC 2018 World Finals took place on Thursday (<a href="https://icpc.baylor.edu/download/worldfinals/problems/icpc2018.pdf">problems</a>, <a href="https://icpc.baylor.edu/scoreboard/?static=1">results</a>, top 13 on the left, <a href="https://youtu.be/nF3tSkNWRVQ">our screencast with commentary</a>, <a href="https://youtu.be/LpIT35y33WY">official broadcast</a>, <a href="http://www.csc.kth.se/~austrin/icpc/finals2018solutions.pdf">analysis</a>). The deciding events of the competition happened in the last few minutes, when the Moscow State University team managed to squeeze in a solution to roblem E with an extra log factor by changing the number of iterations in binary search and getting something like TLE-TLE-TLE-OK-WA-WA-WA from seven attempts with different values of the magic constant.<br /><br />That OK might well turn out to be TLE or WA as well, but fortune favors the bold, and what essentially happened was that the Moscow State University team did great in using all their resources and creativity up to the last minute and got a well-deserved victory. Really happy for the team and for my alma mater to finally get the cup that I and many others could not deliver in the past :)<br /><br />Big congratulations to all other medalists on the great result, too!<br /><br />Problem D brought the most excitement for me in this problemset. There are <i>n</i> people, each starting with 1 gem. The following operation is repeated <i>d</i> times: take one of the gems uniformly at random, and split it into two gems (so the person holding it will have one more gem). After doing all that we order all people by the number of gems they have in decreasing order, and add up the number of gems of the first <i>r</i> people in that order. What is the expected value of that sum? <i>n</i> and <i>d</i> are at most 500.<br /><br />I'm also really interested in hearing what do you think about our stream and about the official broadcasts, if you had a chance to check them out, and I'm sure the ICPCLive team is very interested as well. Please share your observations or ideas!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-cFNgLVKTO68/WviksUkoaPI/AAAAAAACCjk/2SW1olze0IocKM6eAObG7_4IcOjWOUnxQCLcBGAs/s1600/tco18r1atop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="150" data-original-width="764" height="124" src="https://1.bp.blogspot.com/-cFNgLVKTO68/WviksUkoaPI/AAAAAAACCjk/2SW1olze0IocKM6eAObG7_4IcOjWOUnxQCLcBGAs/s640/tco18r1atop5.png" width="640" /></a></div>Finally, TopCoder Open 2018 Round 1A took place on Saturday (<a href="https://community.topcoder.com/stat?c=round_overview&er=5&rd=17144">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17144&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="https://www.topcoder.com/blog/2018-tco-algorithm-round-1a-editorials/">analysis</a>). With the problems quite a bit on the easy side, the challenge phase was instrumental in determining the winner — congratulations to Dembel on finding the +125 and the victory!<br /><br />Thanks for reading, and check back for more!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/cxSFTDYWFoQ" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/05/an-alma-mater-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-45747481545793730902018-05-13T23:01:00.000+03:002018-05-13T23:01:39.941+03:00Changes to commenting system<div dir="ltr" style="text-align: left;" trbidi="on">I've changed the commenting system in this blog from HyperComments (thanks to its authors for making it!) to built-in Blogger comments, because HyperComments is discontinuing free usage. In the past years Blogger has added support for threaded replies which was the main motivation for me to switch to HyperComments in the past, and using an external commenting system brought its own pains, so switching back to the default seems to be the logical thing to do.<br /><br />Please tell if you encounter some issues with commenting using the new (old?) system!<br /><br />One side effect is that all comments made through HyperComments are not visible now. I have an xml export of all of them, but it's not clear at this point how to import those back into the Blogger system. Please share ideas if you have them :)</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/dR1QYQXCfH8" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com2http://petr-mitrichev.blogspot.com/2018/05/changes-to-commenting-system.htmltag:blogger.com,1999:blog-1953325079793449971.post-82458868699144272522018-05-13T22:45:00.001+03:002018-05-13T22:48:28.312+03:00A +300 week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-FEQo53qz4so/WviSJsv1t0I/AAAAAAACCik/BEctxGa566ckf9HlA6yUbht-n-7Dvt_nwCLcBGAs/s1600/gcj18r1atop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="315" data-original-width="999" height="200" src="https://4.bp.blogspot.com/-FEQo53qz4so/WviSJsv1t0I/AAAAAAACCik/BEctxGa566ckf9HlA6yUbht-n-7Dvt_nwCLcBGAs/s640/gcj18r1atop5.png" width="640" /></a></div>The week before the ICPC World Finals featured two competitions, both on Saturday. First off, Google Code Jam 2018 Round 1A took place very early (<a href="https://codejam.withgoogle.com/2018/challenges/0000000000007883/dashboard">problems</a>, <a href="https://codejam.withgoogle.com/2018/challenges/0000000000007883/scoreboard">results</a>, top 5 on the left, <a href="https://codejam.withgoogle.com/2018/challenges/0000000000007883/analysis">analysis</a>). This was the first round under complete new rules, as the penalty time did not matter in the qualification. The optimal strategy, of course, stayed more or less the same — just solve all problems quickly and without bugs :) vepifanov has executed it really well, congratulations on the victory!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-55P6YIXzzzI/WviUBQTO_TI/AAAAAAACCi4/qpR9L2tBlFMkqlETRXe3KzPnAZaQOrt5ACLcBGAs/s1600/srm733top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="149" data-original-width="778" height="122" src="https://4.bp.blogspot.com/-55P6YIXzzzI/WviUBQTO_TI/AAAAAAACCi4/qpR9L2tBlFMkqlETRXe3KzPnAZaQOrt5ACLcBGAs/s640/srm733top5.png" width="640" /></a></div>TopCoder SRM 733 followed later that day (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17140">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17140&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="https://www.topcoder.com/blog/single-round-match-733-editorials/">analysis</a>). Kriii has really dominated the proceedings, spending a bit over 18 minutes on all three problems in total Compare that to a bit over 41 minutes that kuniavski needed, and he came in second! Amazing performance by Kriii.<br /><br />This was the third SRM which doesn't list cgy4ever in its authors, marking the transition to misof as the new TopCoder problem coordinator. Congratulations to misof on the new role, looking forward to the next SRMs!<br /><br />Thanks for reading this [short] post, check back soon for more!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/ItrfGfMjRqw" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/05/a-300-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-87511130662092868822018-05-11T12:44:00.000+03:002018-05-11T12:44:20.237+03:00An elimination week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-_3v3tEIN1hw/Wtc7kmGa_GI/AAAAAAACBSY/wVtGCgEOMCIYS82qegVYeOkofZwlgglZwCLcBGAs/s1600/cf474top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="279" data-original-width="1106" height="160" src="https://4.bp.blogspot.com/-_3v3tEIN1hw/Wtc7kmGa_GI/AAAAAAACBSY/wVtGCgEOMCIYS82qegVYeOkofZwlgglZwCLcBGAs/s640/cf474top5.png" width="640" /></a></div>The Apr 2 - Apr 8 week had a couple of competitions on the weekend. Codeforces Round 474 took place on Saturday (<a href="http://codeforces.com/contest/960">problems</a>, <a href="http://codeforces.com/contest/960/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/58802">analysis</a>). Solving eight problems correctly in just over two hours is an amazing feat — OO0OOO00O0OOO0O00OOO0OO was on top of his game this time. Well done!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-hbaJjfPeNS8/WtrfX_54dnI/AAAAAAACBZw/mLyu5oY-pGs_gHLLmIX10lXthI5xTdO0wCLcBGAs/s1600/ya2018r3top6.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="442" data-original-width="1198" height="236" src="https://3.bp.blogspot.com/-hbaJjfPeNS8/WtrfX_54dnI/AAAAAAACBZw/mLyu5oY-pGs_gHLLmIX10lXthI5xTdO0wCLcBGAs/s640/ya2018r3top6.png" width="640" /></a></div>Yandex.Algorithm 2018 Round 3 on Sunday (<a href="https://contest.yandex.com/algorithm2018/contest/7989/problems/">problems with Yandex login</a>, <a href="https://contest.yandex.com/algorithm2018/contest/7989/standings/">results</a>, top 6 on the left, <a href="https://yadi.sk/i/6m40Qql53UDQSv">analysis</a>) has completed the Elimination stage. Unlike the first two rounds, this time blind submissions were actually necessary for the victory — congratulations to Merkurev for pulling four of those off !<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-r6HtRSKBa9M/Wtr_IDMlk1I/AAAAAAACBaI/ScJyirOLF7kFN2U6CTMVdRahTw_zpcQgQCLcBGAs/s1600/ya2018elimtop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="270" data-original-width="1160" height="148" src="https://1.bp.blogspot.com/-r6HtRSKBa9M/Wtr_IDMlk1I/AAAAAAACBaI/ScJyirOLF7kFN2U6CTMVdRahTw_zpcQgQCLcBGAs/s640/ya2018elimtop5.png" width="640" /></a></div>Here is the final <a href="https://contest.yandex.com/algorithm2018/algo-results/">Elimination stage scoreboard</a> (top 5 on the left), with top 25 advancing to the finals held both online and in St Petersburg.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-983ww0XCAQc/WvVktVjyQTI/AAAAAAACCHw/fADbq9kOAEINmMlkb5d_OsLLb3PJdb1HwCLcBGAs/s1600/oc1718warsawtop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="271" data-original-width="919" height="188" src="https://3.bp.blogspot.com/-983ww0XCAQc/WvVktVjyQTI/AAAAAAACCHw/fADbq9kOAEINmMlkb5d_OsLLb3PJdb1HwCLcBGAs/s640/oc1718warsawtop5.png" width="640" /></a></div>Also on Sunday, Open Cup 2017-18 continued its streak with the Grand Prix of Warsaw (<a href="http://opencup.ru/index.cgi?data=macros/stage&head=index&menu=index&stg=18&region=main&ncup=oci&class=oci">results</a>, top 5 on the left). SPb ITMO U 1 team demonstrated impressive form, solving all 11 problems at the moment when all other teams had at most 9. Three other teams got to 10 problems in the remaining time, but nobody could approach the first place. Congratulations to the ITMO 1 team!<br /><br />Thanks for reading, and check back for more!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/PNcOs3s4J-k" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/05/an-elimination-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-4538308596607615582018-04-18T15:19:00.002+03:002018-04-18T15:19:34.842+03:00A second extremal week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-Ftsw9-zIAYg/WtcqgfE6WyI/AAAAAAACBRo/EEFkJSldNNsistKAHl64zS4QOzEj8r6qACLcBGAs/s1600/srm732top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="148" data-original-width="782" height="120" src="https://4.bp.blogspot.com/-Ftsw9-zIAYg/WtcqgfE6WyI/AAAAAAACBRo/EEFkJSldNNsistKAHl64zS4QOzEj8r6qACLcBGAs/s640/srm732top5.png" width="640" /></a></div>The last week of March featured more contests from the regular platforms. First off, TopCoder SRM 732 took place very early on Friday (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17103">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17103&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left). Both the medium and the hard problem involved asymmetric games requiring quite some insight to solve, and thus only two contestants were able to solve each (top 4 in the table to the left). Congratulations to all four, and especially to pashka on the win!<br /><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-wS_axcop5bk/Wtcs-HmBjHI/AAAAAAACBR0/bDzvInxcniUbt6ScGBtElWD9gwimc4QqACLcBGAs/s1600/agc022top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="426" data-original-width="1174" height="232" src="https://3.bp.blogspot.com/-wS_axcop5bk/Wtcs-HmBjHI/AAAAAAACBR0/bDzvInxcniUbt6ScGBtElWD9gwimc4QqACLcBGAs/s640/agc022top5.png" width="640" /></a></div><div>Then AtCoder held its Grand Contest 022 on Saturday (<a href="https://agc022.contest.atcoder.jp/assignments">problems</a>, <a href="https://agc022.contest.atcoder.jp/standings#page_1">results</a>, top 5 on the left, <a href="https://img.atcoder.jp/agc022/editorial.pdf">analysis</a>). The contest had three relatively easy problems, and three very hard ones. Only a few contestants were able to solve one of the three (I couldn't solve any, for that matter), so it is extremely impressive that Um_nik got all three — huge congratulations on very well deserved victory!<br /><br /><a href="https://agc022.contest.atcoder.jp/tasks/agc022_e">Problem E</a> was the most approachable of the three. Consider a string of <span style="font-family: Courier New, Courier, monospace;">0</span>s, <span style="font-family: Courier New, Courier, monospace;">1</span>s and <span style="font-family: Courier New, Courier, monospace;">?</span>s of odd length up to 300000. First, we need to replace each <span style="font-family: Courier New, Courier, monospace;">?</span> with a <span style="font-family: Courier New, Courier, monospace;">0</span> or a <span style="font-family: Courier New, Courier, monospace;">1</span>. Then, we repeatedly do the following operation: take any 3 consecutive characters, and replace them with the most frequent character among them. For example, <span style="font-family: Courier New, Courier, monospace;">001</span> is replaced with <span style="font-family: Courier New, Courier, monospace;">0</span>. In the end we end up with a string of length 1, and our goal is to get the string <span style="font-family: "Courier New", Courier, monospace;">1</span><span style="font-family: inherit;">. How many ways are there to replace the </span><span style="font-family: "Courier New", Courier, monospace;">?</span><span style="font-family: inherit;">s so that it's possible to get the </span><span style="font-family: "Courier New", Courier, monospace;">1</span><span style="font-family: inherit;"> after all reductions?</span></div><div><br /></div><div>And finally, Open Cup 2017-18 Grand Prix of Moscow on Sunday wrapped up the week, but its results have not yet been published.<br /><br /></div><div></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-JoJ72EtwRio/Wtc3Sk9fLmI/AAAAAAACBSI/b8R3sxi_prU1zZpaGchd3TWaBQThXogygCLcBGAs/s1600/IMG_20180408_151726.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://1.bp.blogspot.com/-JoJ72EtwRio/Wtc3Sk9fLmI/AAAAAAACBSI/b8R3sxi_prU1zZpaGchd3TWaBQThXogygCLcBGAs/s640/IMG_20180408_151726.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/04/a-group-embedding-week.html">my previous summary</a>, I have mentioned an Open Cup problem: you are given <i>n</i> points <i>p</i><sub>1</sub>, <i>p</i><sub>2</sub>, ..., <i>p<sub>n</sub></i> on the plane and <i>q</i> queries (<i>n</i>, <i>q</i> <= 100000). Each query is defined by two numbers <i>a</i>, <i>b</i>, and you need to print the size of the smallest square with sides parallel to coordinate axes that contains all points from <i>a</i>-th to <i>b</i>-th (from the list <i>p</i><sub><i>a</i></sub>, <i>p</i><sub><i>a</i>+1</sub>, ..., <i>p<sub>b</sub></i>) <i>except maybe one</i>.<br /><br />Assuming we forget about "except maybe one" part, we need to find the bounding box of a segment of points, which can be done by finding the leftmost, rightmost, topmost and bottom-most points using interval trees in O(<i>n</i>*log(<i>n</i>)).<br /><br />Now we can notice that when the skipped point is not one of the four extremal points, then the bounding box does not change, so we need to check at most four possibilities for the skipped point. In order to be able to find extremal points after skipping one point, our interval trees will need hold two extremal points instead of one, but otherwise the solution stays the same.<br /><br />Thanks for reading, and check back for more!</div></div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/O79vv3yU2jI" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/a-second-extremal-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-65994091070690635072018-04-18T10:48:00.000+03:002018-04-18T10:48:19.913+03:00ACM ICPC 2018 World Finals stream on Twitch<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-Bsdpz-Rdmq4/Wtb4HHF5FFI/AAAAAAACBQ8/r--5_rTm5uQIo75a0QJcoz3ZSxmipY0RQCKgBGAs/s1600/IMG_20180418_151848.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="663" data-original-width="1600" height="264" src="https://1.bp.blogspot.com/-Bsdpz-Rdmq4/Wtb4HHF5FFI/AAAAAAACBQ8/r--5_rTm5uQIo75a0QJcoz3ZSxmipY0RQCKgBGAs/s640/IMG_20180418_151848.jpg" width="640" /></a></div>We have figured out a working setup for our stream, so tune in to <a href="https://www.twitch.tv/petrmitrichev">https://www.twitch.tv/petrmitrichev</a> around <a href="https://www.timeanddate.com/worldclock/fixedtime.html?msg=ACM+ICPC+2018+World+Finals&iso=20180419T10&p1=33&ah=5">10:00 Beijing time</a> tomorrow!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/jX5Ufrk7PUs" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/acm-icpc-2018-world-finals-stream-on.htmltag:blogger.com,1999:blog-1953325079793449971.post-43583119972488884622018-04-17T19:24:00.001+03:002018-04-17T19:24:27.172+03:00A group embedding week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-dhUK1alK9hw/WtXDQbcpF7I/AAAAAAACBIs/8IEqR_3Ruukj17q6UeHEyNTfLsdJVxlZgCLcBGAs/s1600/vk2018r2top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="269" data-original-width="1098" height="156" src="https://3.bp.blogspot.com/-dhUK1alK9hw/WtXDQbcpF7I/AAAAAAACBIs/8IEqR_3Ruukj17q6UeHEyNTfLsdJVxlZgCLcBGAs/s640/vk2018r2top5.png" width="640" /></a></div>VK Cup 2018 Round 2 took place during the The Mar 19 - Mar 25 week (<a href="http://codeforces.com/contest/924">problems</a>, <a href="http://codeforces.com/contest/924/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/contest/956/standings">parallel round results</a>, <a href="http://codeforces.com/blog/entry/58544">analysis</a>). Team Нижний Магазин SU: BZ would be first by far even without solving the last problem, but getting all problems accepted in the final minutes of the contest was of course the icing on the cake. Congratulations!<br /><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-Rz0ZxdumdUE/WtXFKI3S_3I/AAAAAAACBI4/z-xZASUfP4MUc1YFVo_OniRa88IhSYqcACLcBGAs/s1600/oc1718americatop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="212" data-original-width="814" height="166" src="https://4.bp.blogspot.com/-Rz0ZxdumdUE/WtXFKI3S_3I/AAAAAAACBI4/z-xZASUfP4MUc1YFVo_OniRa88IhSYqcACLcBGAs/s640/oc1718americatop5.png" width="640" /></a></div><div>Open Cup 2017-18 Grand Prix of America wrapped up that week (<a href="http://opentrains.snarknews.info/~ejudge/res/res10416">results</a>, top 5 on the left, <a href="https://naipc18.kattis.com/standings">parallel round results</a>), with another two World Finals favorites in top 5: SPb ITMO 1 and Moscow SU Red Panda. The first place, however, went to team Past Glory — once again thanks to their incredible accuracy. Congratulations!<br /><br />Problem K in this round was quite educational, if a bit professional. You are given <i>n</i> points <i>p</i><sub>1</sub>, <i>p</i><sub>2</sub>, ..., <i>p<sub>n</sub></i> on the plane and <i>q</i> queries (<i>n</i>, <i>q</i> <= 100000). Each query is defined by two numbers <i>a</i>, <i>b</i>, and you need to print the size of the smallest square with sides parallel to coordinate axes that contains all points from <i>a</i>-th to <i>b</i>-th (from the list <i>p</i><sub><i>a</i></sub>, <i>p</i><sub><i>a</i>+1</sub>, ..., <i>p<sub>b</sub></i>) <i>except maybe one</i>. Can you dissect this problem into standard pieces?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-sRLUZPFiRxI/WtYfboq3j4I/AAAAAAACBM8/1LruwZOKP40HnF-hVzA49OomQjsLHNeYgCLcBGAs/s1600/IMG_20180408_121656%257E2.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1025" data-original-width="1600" height="408" src="https://2.bp.blogspot.com/-sRLUZPFiRxI/WtYfboq3j4I/AAAAAAACBM8/1LruwZOKP40HnF-hVzA49OomQjsLHNeYgCLcBGAs/s640/IMG_20180408_121656%257E2.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/04/a-favorites-week.html">my previous summary</a>, I have mentioned a Yandex.Algorithm problem: you are given a string <i>s</i> with 100000 characters, each <span style="font-family: "courier new" , "courier" , monospace;">a</span>, <span style="font-family: "courier new" , "courier" , monospace;">b</span> or <span style="font-family: "courier new" , "courier" , monospace;">c</span>. You must swap exactly two distinct letters to obtain a new string <i>t</i>. How many ways are there to do that in such a way that the string <i>t</i> is good? In this problem we define a <i>good</i> string somewhat similarly to a valid parentheses sequence: empty string is good, if a string <i>u</i> is good then the strings <span style="font-family: "courier new" , "courier" , monospace;">a</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">a</span><span style="font-family: inherit;">, </span><span style="font-family: "courier new" , "courier" , monospace;">b</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">b</span>, <span style="font-family: "courier new" , "courier" , monospace;">c</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">c</span> are good as well, and if two strings <i>u</i> and <i>v</i> are good, then their concatenation <i>uv</i> is also good.<br /><br />The key insight in this problem is to learn to check if a string is good easily. Let's pick a group and 3 elements of order 2 in it: <span style="font-family: "courier new" , "courier" , monospace;">a</span><span style="font-family: "courier new" , "courier" , monospace;">a</span><span style="font-family: inherit;">=1, </span><span style="font-family: "courier new" , "courier" , monospace;">bb</span><span style="font-family: inherit;">=1, </span><span style="font-family: "courier new" , "courier" , monospace;">cc</span><span style="font-family: inherit;">=1. We will then map each string to the product of the corresponding elements of the group. We can see that any <i>good </i>string will map to 1. Moreover, if our group is general enough, in other words does not have any other non-derived products that are equal to 1 (or if such products are simply unlikely to be equal to 1), then the opposite is also true: when a string maps to 1, it is good. As an example group that works in this problem we can use the group of movements of 3D space that keep the origin fixed, where </span><span style="font-family: "courier new" , "courier" , monospace;">a</span><span style="font-family: inherit;">, </span><span style="font-family: "courier new" , "courier" , monospace;">b</span><span style="font-family: inherit;"> and </span><span style="font-family: "courier new" , "courier" , monospace;">c</span><span style="font-family: inherit;"> correspond to reflections through three random planes passing through origin. This way, we obtain a compressed representation for a string of any length as a single 3x3 matrix (modulo some prime number, to avoid dealing with floating point).</span><br /><br />We can then use the divide-and-conquer approach: we start by splitting our string in two in the middle, and consider the case where the two positions being swapped are in different halves. If we also try all 6 possibilities for the letters being swapped, we have a sub-problem of the following kind: we have two strings, and need to replace a single a with <span style="font-family: "courier new" , "courier" , monospace;">b</span> in the first string, and a single <span style="font-family: "courier new" , "courier" , monospace;">b</span> with <span style="font-family: "courier new" , "courier" , monospace;">a</span> in the second string, so that after doing those replacements and concatenating the strings we get a good one.<br /><br /><span style="font-family: inherit;">We can compute the compressed representation of each candidate for the first half using prefix and suffix products, then compute the compressed representation of each candidate for the second half in the same way, and then for each representation of first half find its inverse element in the second half, thus processing the case where the swapped elements are in different halves in O(<i>n</i>).</span><br /><span style="font-family: inherit;"><br /></span><span style="font-family: inherit;">In order to handle the case where both swapped elements are in the same half, we follow the divide-and-conquer approach and recursively execute the same algorithm for each half, with the only difference that the target product in each half we want to get is not 1, but rather the inverse of the product of the other half. This allows to complete the solution of the entire problem in O(<i>n</i>*log(<i>n</i>)).</span><br /><span style="font-family: inherit;"><br /></span><span style="font-family: inherit;">Thanks for reading, and check back soon!</span></div></div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/9rdul-z1H0E" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/a-group-embedding-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-53245832675712908602018-04-17T18:30:00.000+03:002018-04-17T18:30:27.028+03:00A favorites week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-BGYi4WECh0c/WtWz-YdtybI/AAAAAAACBHw/kuWwuu3T3884RZZmpnCWEL6fkasItz-0wCLcBGAs/s1600/ya2018r2top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="380" data-original-width="1190" height="204" src="https://3.bp.blogspot.com/-BGYi4WECh0c/WtWz-YdtybI/AAAAAAACBHw/kuWwuu3T3884RZZmpnCWEL6fkasItz-0wCLcBGAs/s640/ya2018r2top5.png" width="640" /></a></div>The Mar 12 - Mar 18 week started with Yandex.Algorithm Round 2 on Tuesday (<a href="https://contest.yandex.ru/contest/7736/problems/">problems with Yandex login</a>, <a href="https://contest.yandex.ru/contest/7736/standings/">results</a>, top 5 on the left, <a href="https://youtu.be/shoK2wIdurA">my screencast</a>). <a href="https://contest.yandex.ru/contest/7736/problems/A/">The hardest problem A</a> was left unsolved despite 78 attempts, and yet the actual implementation is quite straightforward once one gets the idea. You are given a string <i>s</i> with 100000 characters, each <span style="font-family: "courier new" , "courier" , monospace;">a</span>, <span style="font-family: "courier new" , "courier" , monospace;">b</span> or <span style="font-family: "courier new" , "courier" , monospace;">c</span>. You must swap exactly two distinct letters to obtain a new string <i>t</i>. How many ways are there to do that in such a way that the string <i>t</i> is good? In this problem we define a <i>good</i> string somewhat similarly to a valid parentheses sequence: empty string is good, if a string <i>u</i> is good then the strings <span style="font-family: "courier new" , "courier" , monospace;">a</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">a</span><span style="font-family: inherit;">, </span><span style="font-family: "courier new" , "courier" , monospace;">b</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">b</span>, <span style="font-family: "courier new" , "courier" , monospace;">c</span><i>u</i><span style="font-family: "courier new" , "courier" , monospace;">c</span> are good as well, and if two strings <i>u</i> and <i>v</i> are good, then their concatenation <i>uv</i> is also good.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-CF9QD80y7Lk/WtW0uWOrRAI/AAAAAAACBH4/WXM8KajAmKYryf1c6p42JAXwRarmdvAQACLcBGAs/s1600/srm731top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="150" data-original-width="779" height="122" src="https://4.bp.blogspot.com/-CF9QD80y7Lk/WtW0uWOrRAI/AAAAAAACBH4/WXM8KajAmKYryf1c6p42JAXwRarmdvAQACLcBGAs/s640/srm731top5.png" width="640" /></a></div>TopCoder SRM 731 took place on Saturday (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17095">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17095&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left). mjhun was the fastest during the coding phase, but Gassa was close enough to require just one successful challenge to climb into the first place. Congratulations to both of you!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-LzgtRG6o1P4/WtW2glgJ-UI/AAAAAAACBIE/c8vK9dNqi6Ed3ApzzIAsK4hYdDBiyX8TACLcBGAs/s1600/oc1718belarustop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="271" data-original-width="871" height="198" src="https://2.bp.blogspot.com/-LzgtRG6o1P4/WtW2glgJ-UI/AAAAAAACBIE/c8vK9dNqi6Ed3ApzzIAsK4hYdDBiyX8TACLcBGAs/s640/oc1718belarustop5.png" width="640" /></a></div>Finally, the Open Cup 2017-18 Grand Prix of Belarus happened on Sunday (<a href="http://opencup.ru/files/oci/gp15/problems1-e.pdf">problems</a>, <a href="http://opencup.ru/index.cgi?data=macros/stage&head=index&menu=index&stg=15&region=main&ncup=oci&class=oci">results</a>, top 5 on the left). Team Past Glory was once again the fastest — well done! This round also provided a World Finals prediction perspective, as there are two teams in the top 5 who will be competing in Beijing: Seoul NU and Peking U. This year's World Finals looks to be extremely competitive, with top teams like Seoul NU, Peking U, SPb ITMO, Moscow SU, Warsaw U all of comparable strength (did I miss a team that you think is one of the favorites to win? Sorry, and please share in comments!) I'm looking forward to the final showdown on Thursday!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-WHQolsJxd48/WtW_8b7CkII/AAAAAAACBIg/5bDquGqasj40EArIoR6S8ekAG94w_NHfACLcBGAs/s1600/IMG_20180316_142300.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1600" data-original-width="1200" height="640" src="https://2.bp.blogspot.com/-WHQolsJxd48/WtW_8b7CkII/AAAAAAACBIg/5bDquGqasj40EArIoR6S8ekAG94w_NHfACLcBGAs/s640/IMG_20180316_142300.jpg" width="480" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/04/a-binary-block-week.html">my previous summary</a>, I have mentioned a cute Open Cup problem: you are given a <i>n</i> times <i>n</i> matrix <i>A</i> of integers modulo a prime number <i>p</i>. You need to find the smallest positive integer <i>k</i> such that <i>A<sup>k</sup></i>=0 (modulo <i>p</i>), or report that it does not exist, in O(<i>n</i><sup>3</sup>).<br /><br />The answer never exceeds <i>n</i>, which in very rough terms can be derived from looking at <a href="https://en.wikipedia.org/wiki/Jordan_normal_form">Jordan normal forms</a> (is there a simpler argument?..), which enables the following O(<i>n</i><sup>4</sup>) approach: just compute the first <i>n</i> powers of <i>A</i>. It can be sped up to O(<i>n</i><sup>3</sup>*log(<i>n</i>)) by using binary search, since once a power of <i>A</i> is zero, all following powers are zero as well.<br /><br />In order to speed it up to O(<i>n</i><sup>3</sup>), we need to come up with a beautiful idea: instead of computing just <i>A<sup>k</sup></i>, we will compute <i>A<sup>k</sup></i><i>*v</i> for some vector <i>v</i> of size n. In case <i>A<sup>k</sup></i>=0, then <i>A<sup>k</sup></i><i>*v</i>=0 as well. It turns out the opposite is almost always true: <i>A<sup>k</sup></i><i>*v</i> over all <i>v</i> defines a linear subspace, which has at least one dimension in case <i>A<sup>k</sup></i> is not zero, and thus we can just pick v randomly and the probability of <i>A<sup>k</sup></i><i>*v</i> being zero will not exceed 1/<i>p</i>.<br /><br />Since we can multiply a matrix by a vector in O(<i>n</i><sup>2</sup>), we can compute <i>v</i>, <i>A</i>*<i>v</i>, <i>A<sup>2</sup></i><i>*v</i>, ..., <i>A<sup>n</sup></i><i>*v</i> in sequence in O(<i>n</i><sup>3</sup>) overall.<br /><br />Thanks for reading, and check back for more!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/OUOL0P4YGRc" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/a-favorites-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-91396420733339005932018-04-17T10:58:00.000+03:002018-04-17T10:58:33.765+03:00ACM ICPC 2018 World Finals stream<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-vgnnUdGVxPI/WtQG4sH17qI/AAAAAAACA9Q/bLAMDTiABjcoFWfjp-E8CBeC5nJfDuDKQCLcBGAs/s1600/team.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="540" data-original-width="960" height="360" src="https://2.bp.blogspot.com/-vgnnUdGVxPI/WtQG4sH17qI/AAAAAAACA9Q/bLAMDTiABjcoFWfjp-E8CBeC5nJfDuDKQCLcBGAs/s640/team.jpg" width="640" /></a></div>ACM ICPC 2018 World Finals take place this Thursday at 10:00 Beijing time (<a href="https://www.timeanddate.com/worldclock/fixedtime.html?msg=ACM+ICPC+2018+World+Finals&iso=20180419T10&p1=33&ah=5">click for other timezones</a>). Just like <a href="http://petr-mitrichev.blogspot.com/2017/05/acm-icpc-2017-world-finals-stream.html">last year</a>, we'll try to solve it in parallel with tourist and Endagorion, and stream the process, assuming the problems will be available for submission <a href="https://online.acmicpc.org/problems">on Kattis</a>.<br /><br />I'm not yet sure if it's going to be on Youtube, which does not work well in China, or somewhere else. I will post an update here and <a href="https://twitter.com/PetrMitrichev">in Twitter</a> once we figure out the right setup. Stay tuned!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/4Mu0f9cA6eE" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/acm-icpc-2018-world-finals-stream.htmltag:blogger.com,1999:blog-1953325079793449971.post-52059547937622645182018-04-14T17:10:00.001+03:002018-04-14T17:10:33.978+03:00A binary block week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-EeQLmtKhxyI/WtH3nRcqDdI/AAAAAAACA5Y/UCla8Ocr_b4jNYg-WsQCpwfpADbq3pW5QCLcBGAs/s1600/cf469top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="271" data-original-width="1098" height="156" src="https://3.bp.blogspot.com/-EeQLmtKhxyI/WtH3nRcqDdI/AAAAAAACA5Y/UCla8Ocr_b4jNYg-WsQCpwfpADbq3pW5QCLcBGAs/s640/cf469top5.png" width="640" /></a></div>The Mar 5 - Mar 11 week had two Codeforces rounds. Round 469 took place on Friday (<a href="http://codeforces.com/contest/949">problems</a>, <a href="http://codeforces.com/contest/949/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/58291">analysis</a>). Since I'm writing this post on the plane to Beijing for ICPC 2018 World Finals, I can't help but look at the scoreboard from the ICPC angle: the winner dotorya will be participating in the Seoul NU team, and the third-placed Syloviaely is part of the host Peking U team. Congratulations on the great Codeforces result, and best of luck in the World Finals!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-q90tCx3HFx8/WtH5h2TwsEI/AAAAAAACA5k/OEIIz7Y7jLAI00eSRN9N9_6XU8vwNphZgCLcBGAs/s1600/vk2018r1top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="278" data-original-width="1102" height="160" src="https://3.bp.blogspot.com/-q90tCx3HFx8/WtH5h2TwsEI/AAAAAAACA5k/OEIIz7Y7jLAI00eSRN9N9_6XU8vwNphZgCLcBGAs/s640/vk2018r1top5.png" width="640" /></a></div>VK Cup 2018 Round 1 took place one day later (<a href="http://codeforces.com/contest/923">problems</a>, <a href="http://codeforces.com/contest/923/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/contest/947/standings">parallel round results</a>, <a href="http://codeforces.com/blog/entry/58286">analysis</a>). Team VK Cup 24329020081766400008 from Saratov was already among the fastest in coding, but managed to stand out thanks to the challenges, even despite their solution for the hardest problem failing systests. Well done!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-K7f7vjenzeU/WtH9qdPUkwI/AAAAAAACA5w/cz5-gNQ0K60VIcOfvyjSs_qEaFZ4dW7rQCLcBGAs/s1600/oc1718baltictop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="268" data-original-width="819" height="208" src="https://3.bp.blogspot.com/-K7f7vjenzeU/WtH9qdPUkwI/AAAAAAACA5w/cz5-gNQ0K60VIcOfvyjSs_qEaFZ4dW7rQCLcBGAs/s640/oc1718baltictop5.png" width="640" /></a></div>Finally, after a week's hiatus the Open Cup came back for another five-week marathon which started with the Grand Prix of Baltic (<a href="http://opencup.ru/files/oci/gp14/problems-e.pdf">problems</a>, <a href="http://opencup.ru/index.cgi?data=macros/stage&head=index&menu=index&stg=14&region=main&ncup=oci&class=oci">results</a>, top 5 on the left). Team Past Glory was not as fast as t.me/umnik_team, but they were more accurate, and thus won by a few penalty minutes. Congratulations to both teams on the great performance!<br /><br />Problem E had a very simple statement and a very cute solution. You are given a <i>n</i> times <i>n</i> matrix <i>A</i> of integers modulo a prime number <i>p</i>. You need to find the smallest positive integer <i>k</i> such that <i>A<sup>k</sup></i>=0 (modulo <i>p</i>), or report that it does not exist. It is relatively straightforward to do in O(<i>n</i><sup>3</sup>*log(<i>n</i>)), but your solution needs to run in O(<i>n</i><sup>3</sup>).<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-EaFiCN9Z6hI/WtIJ-qDIUqI/AAAAAAACA6A/VZavrweDmIIMWYDRu7lPCIFb1rlAseykQCLcBGAs/s1600/IMG_20180308_153117.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://1.bp.blogspot.com/-EaFiCN9Z6hI/WtIJ-qDIUqI/AAAAAAACA6A/VZavrweDmIIMWYDRu7lPCIFb1rlAseykQCLcBGAs/s640/IMG_20180308_153117.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/03/a-power-of-two-week.html">my previous summary</a>, I have mentioned a difficult Codeforces problem: consider an unknown binary string of length <i>k</i>, <i>k</i> is up to a billion. You're given up to 100000 segments [<i>a</i><i><sub>i</sub></i>,<i>b</i><i><sub>i</sub></i>], and know that each of those segments contains at least one 0. In a similar vein, you're also given up to 100000 segments [<i>c</i><i><sub>i</sub></i>,<i>d</i><i><sub>i</sub></i>], and know that each of those segments contains at least one 1. How many such binary strings exist, modulo 10<sup>9</sup>+7?<br /><br />First, how can we approach this problem if we forget that <i>k</i> is very big? Let's split our string into blocks of 0s and 1s, and compute <i>p</i><i><sub>j</sub></i> — the number of ways to choose the first j characters ending with a block of 0s, and <i>q</i><i><sub>j</sub></i> — the number of ways to choose the first j characters ending with a block of 1s. To compute <i>p</i><i><sub>j</sub></i>, we iterate over the ending point <i>t</i> of previous block of 1s, and we see that <i>p</i><i><sub>j</sub></i> is a sum of <i>q</i><i><sub>t</sub></i>. <i>t</i> must be at most j-1, and at least max(<i>c</i><i><sub>i</sub></i>) over all i such that <i>d</i><i><sub>i</sub></i><=<i>j</i>, to make sure that no [<i>c</i><i><sub>i</sub></i>,<i>d</i><i><sub>i</sub></i>] segment only contains 0s. We can compute <i>q</i><i><sub>j</sub></i> in a symmetric way.<br /><br />Since max(<i>c</i><i><sub>i</sub></i>) only ever increases as we increase <i>j</i>, we can maintain a sliding window of <i>q</i><i><sub>t</sub></i>'s together with their sum, and thus obtain a O(<i>k</i>) amortized time solution.<br /><br />Now let's look more closely at this process in case we don't encounter any new segment endpoints. Suppose we have just found <i>p</i><i><sub>j</sub></i> and <i>q</i><i><sub>j</sub></i>, and now are looking to find <i>p</i><i><sub>j+1</sub></i> and <i>q</i><i><sub>j+1</sub></i> while the left boundaries of our summation max(<i>a</i><i><sub>i</sub></i>) and max(<i>c</i><i><sub>i</sub></i>) stay the same. <i>p</i><i><sub>j+1 </sub></i>is the same sum as <i>p</i><i><sub>j</sub></i>, but with extra term <i>q</i><i><sub>j </sub></i>added, so <i>p</i><i><sub>j+1</sub></i>=<i>p</i><i><sub>j</sub></i>+<i>q</i><i><sub>j</sub></i>. Similarly, <i>q</i><i><sub>j+1</sub></i>=<i>p</i><i><sub>j</sub></i>+<i>q</i><i><sub>j</sub></i> as well. So we got two equal numbers at (<i>j</i>+1)-th step, and on each further step these numbers will just keep multiplying by two until we encounter a new segment endpoint and summation left boundaries change.<br /><br />This allows to obtain a O(<i>n</i>log<i>n</i>) solution, where n is the number of segments: instead of computing all individual values of <i>p</i><i><sub>j</sub></i> and <i>q</i><i><sub>j</sub></i>, we will split our string into blocks between the segment endpoints, and in each block we will only store the first number, and all following numbers will be 2x the previous number. The formula for the sum of a geometric progression allows to handle such segments quickly.<br /><br />Thanks for reading, and check back soon for more summaries and for some ICPC blogging!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/JT4j2oqvwSQ" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/a-binary-block-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-6286456693107879302018-04-08T00:39:00.000+03:002018-04-08T00:39:08.272+03:00Google Code Jam qualification - last chance<div dir="ltr" style="text-align: left;" trbidi="on">There's about 4 hours left in <a href="https://codejam.withgoogle.com/2018/challenges/00000000000000cb/dashboard">the Google Code Jam 2018 qualification round</a>. There were some stability issues with the new system earlier in the day, but they seem to have been resolved, so now's your chance to join the Code Jam if you haven't already. As usual I'm setting some problems this year, and I hope you'll find them interesting!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/f8Qe7DxUyUM" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/04/google-code-jam-qualification-last.htmltag:blogger.com,1999:blog-1953325079793449971.post-83886234334845185572018-03-05T00:58:00.001+03:002018-03-05T00:58:31.632+03:00A power of two week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-_OX1545KgyE/WpvESMq5ZhI/AAAAAAAB_OY/_ZM3S3TLKcMGsspuJ12JIJjK0eUGfmgJACLcBGAs/s1600/ya2018r1top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="391" data-original-width="1193" height="208" src="https://1.bp.blogspot.com/-_OX1545KgyE/WpvESMq5ZhI/AAAAAAAB_OY/_ZM3S3TLKcMGsspuJ12JIJjK0eUGfmgJACLcBGAs/s640/ya2018r1top5.png" width="640" /></a></div>This week's contests were concentrated on the weekend. First, Yandex.Algorithm 2018 Round 1 has brought back the "open/blind" submission format on Saturday (<a href="https://contest.yandex.com/algorithm2018/contest/7636/problems/">problems with Yandex login</a>, <a href="https://contest.yandex.com/algorithm2018/contest/7636/standings/">results</a>, top 5 on the left, <a href="https://youtu.be/2OhhyMS_v4U">my screencast</a>, <a href="http://codeforces.com/blog/entry/58135">analysis</a>). It turned out that it doesn't really matter whether to choose open or blind if one gets all problems correct from the first try, and is the only one to solve everything :) Congratulations to tourist!<br /><br /><a href="https://contest.yandex.com/algorithm2018/contest/7636/problems/E/">Problem E</a> has a deceptively easy solution in the analysis, and yet it seems really hard to come up with. You are given a sequence of 100000 positive integers up to a billion. You need to find any integer <i>k</i> such that it's possible to transform our sequence into a strictly increasing sequence of positive integers by subtracting some of its elements from k (in other words, by replacing <i>x<sub>i</sub></i> by <i>k</i>-<i>x<sub>i</sub></i> for some set of <i>i</i>'s). Can you see why this problem is actually quite simple?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-GpES1erWEWA/Wpxc9303JnI/AAAAAAAB_TI/NcisX8hwvhQJxElR6PcZ_9Q5H0aqz51ggCLcBGAs/s1600/cf468top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="1106" height="156" src="https://2.bp.blogspot.com/-GpES1erWEWA/Wpxc9303JnI/AAAAAAAB_TI/NcisX8hwvhQJxElR6PcZ_9Q5H0aqz51ggCLcBGAs/s640/cf468top5.png" width="640" /></a></div>On Sunday Codeforces hosted its Round 468 (<a href="http://codeforces.com/contest/930">problems</a>, <a href="http://codeforces.com/contest/930/standings">results</a>, top 5 on the left, <a href="https://youtu.be/MDeRGstm8Zg">my screencast</a>). It was V--o_o--V's turn to be head and shoulders above the competition, finishing all problems in less than an hour. Well done!<br /><br />The hardest <a href="http://codeforces.com/contest/930/problem/E">problem E</a> involved finding an appropriate dynamic programming angle that allows "coordinate compression" to work. Consider an unknown binary string of length <i>k</i>, <i>k</i> is up to a billion. You're given up to 100000 segments [<i>a</i><i><sub>i</sub></i>,<i>b</i><i><sub>i</sub></i>], and know that each of those segments contains at least one 0. In a similar vein, you're also given up to 100000 segments [<i>c</i><i><sub>i</sub></i>,<i>d</i><i><sub>i</sub></i>], and know that each of those segments contains at least one 1. How many such binary strings exist, modulo 10<sup>9</sup>+7?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-FWvRIagDnVM/WpxrUBmwX8I/AAAAAAAB_Tc/VINNZW-pd8oAZ2kpjGzEMqJsZwMyPfXSgCLcBGAs/s1600/IMG_20180208_145857.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="909" data-original-width="1600" height="362" src="https://3.bp.blogspot.com/-FWvRIagDnVM/WpxrUBmwX8I/AAAAAAAB_Tc/VINNZW-pd8oAZ2kpjGzEMqJsZwMyPfXSgCLcBGAs/s640/IMG_20180208_145857.jpg" width="640" /></a></div><a href="http://petr-mitrichev.blogspot.com/2018/02/an-infinite-ratio-week.html">Last week</a>, I have mentioned a TopCoder combinatorics problem: find the sum of 2<sup><i>x</i><sub>1</sub></sup> (two to the power of the minimum number) over all possible ways to choose <i>k</i> distinct positive integers up to <i>n</i>: 1<=<i>x</i><sub>1</sub><<i>x</i><sub>2</sub><...<<i>x</i><sub><i>k</i></sub><=<i>n</i>, where <i>n</i> is up to a billion, and <i>k</i> is up to a million.<br /><br />The key idea is: instead of summing 2<sup><i>x</i><sub>1</sub></sup> for each combination, which seems hard to do, we will further subdivide each combination into 2<sup><i>x</i><sub>1</sub>-1</sup> different objects, so that we just need to count the number of objects and multiply by 2. More specifically, if we have <i>k</i> distinct positive integers, and the smallest is <i>x</i><sub>1</sub>, then there are exactly 2<sup><i>x</i><sub>1</sub>-1</sup> ways to add some subset of numbers between 1 and <i>x</i><sub>1</sub>-1 to it. As a result, we get a set of <i>at least</i> <i>k</i> distinct positive integers. And moreover, we can obtain all such sets: each such set can be obtained by subdividing the set of <i>k</i> its largest numbers. So we need to return 2 times the number of ways to choose at least <i>k</i> objects out of <i>n</i>, which is much easier to compute.<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/bDPI20Xfu7k" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/03/a-power-of-two-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-46551557178935470292018-02-26T00:16:00.001+03:002018-02-26T00:25:49.609+03:00An infinite ratio week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-PgPU6mih2ao/WpMdEY7N1cI/AAAAAAAB-70/6R7H3gNY_AcGjkF9F9Q8kE65ggw8ySuxgCLcBGAs/s1600/srm730top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="94" data-original-width="637" src="https://1.bp.blogspot.com/-PgPU6mih2ao/WpMdEY7N1cI/AAAAAAAB-70/6R7H3gNY_AcGjkF9F9Q8kE65ggw8ySuxgCLcBGAs/s1600/srm730top5.png" /></a></div>This week had a round from each of the platforms I usually cover. First off, TopCoder held SRM 730 on Tuesday (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17087">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17087&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57870?#comment-415959">analysis</a>). The competition was ultimately decided during the challenge phase where I was basically very lucky: my room had 8 incorrect solutions for the 250, and I've managed to bring down 4 of those; ACRush had only 4 incorrect solutions in his room, one of those was challenged by somebody else, and he got the other 3. Nevertheless, amazing comeback after an almost half-year break for ACRush!<br /><br />Quite unusually, <a href="https://community.topcoder.com/stat?c=problem_statement&pm=14795&rd=17087">the hard problem</a> was solved by ksun48 in less than 5 minutes. The problem asked to find the sum of 2<sup><i>x</i><sub>1</sub></sup> (two to the power of the minimum number) over all possible ways to choose <i>k</i> distinct positive integers up to <i>n</i>: 1<=<i>x</i><sub>1</sub><<i>x</i><sub>2</sub><...<<i>x</i><sub><i>k</i></sub><=<i>n</i>, where <i>n</i> is up to a billion, and <i>k</i> is up to a million.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-rAf3_VR5Ygk/WpMdovKfd4I/AAAAAAAB-78/P405GKvGYngTz-GOvCldQwWG9C1s8oz6QCLcBGAs/s1600/agc021top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="443" data-original-width="905" height="313" src="https://1.bp.blogspot.com/-rAf3_VR5Ygk/WpMdovKfd4I/AAAAAAAB-78/P405GKvGYngTz-GOvCldQwWG9C1s8oz6QCLcBGAs/s640/agc021top5.png" width="640" /></a></div>Next up was AtCoder Grand Contest 021 on Saturday (<a href="https://agc021.contest.atcoder.jp/assignments">problems</a>, <a href="https://agc021.contest.atcoder.jp/standings">results</a>, top 5 on the left, <a href="https://img.atcoder.jp/agc021/editorial.pdf">analysis</a>, <a href="https://youtu.be/dsqWOAvOsA8">my screencast</a>). tourist was nearly unstoppable this time, earning his first place with about 25 minutes left in the contest. Egor in 3rd place had the most realistic chance to overtake him thanks to a creative strategy, as he decided to avoid spending more time to earn the fairly technical final 300 points in the partial-scoring problem F, and instead focused on problem E which would give 1200 points if solved. Unfortunately 25 minutes were still not enough for that. Nevertheless, congratulations to both!<br /><br /><a href="https://agc021.contest.atcoder.jp/tasks/agc021_b">Problem B</a> was quite cute. You are given 100 points on the plane. For each given point, consider the part of the plane where it is the closest of the given points (a Voronoi diagram). Some of the parts will be finite, and some will be infinite. What are the relative areas of the infinite parts (see the problem statement for the formal description)?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-snYM0Sc0HTc/WpMeFcgE_0I/AAAAAAAB-8A/FlEOgytsT38CQvJKXwcHBruyyRPC7OkrQCLcBGAs/s1600/oc1718saratovtop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="215" data-original-width="835" height="164" src="https://3.bp.blogspot.com/-snYM0Sc0HTc/WpMeFcgE_0I/AAAAAAAB-8A/FlEOgytsT38CQvJKXwcHBruyyRPC7OkrQCLcBGAs/s640/oc1718saratovtop5.png" width="640" /></a></div>Sunday started with the Open Cup Grand Prix of Saratov (<a href="http://opencup.ru/files/oci/gp13/problems1-e.pdf">problems</a>, <a href="http://opentrains.snarknews.info/~ejudge/res/res10413">results</a>, top 5 on the left). Six teams were able to solve 11 this time, but team Past Glory has managed to do that without a single incorrect attempt — amazing!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-SdxoXhNSVrI/WpMeeKZxJII/AAAAAAAB-8M/sm8X9eVo3VwkWvTI6lFEE6rndbRPPNCTACLcBGAs/s1600/cf467top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="272" data-original-width="1105" height="156" src="https://2.bp.blogspot.com/-SdxoXhNSVrI/WpMeeKZxJII/AAAAAAAB-8M/sm8X9eVo3VwkWvTI6lFEE6rndbRPPNCTACLcBGAs/s640/cf467top5.png" width="640" /></a></div>After a short break, Codeforces Round 467 wrapped up the week (<a href="http://codeforces.com/contest/936">problems</a>, <a href="http://codeforces.com/contest/936/standings">results</a>, top 5 on the left, <a href="https://youtu.be/WC8iTUwViOU">my screencast</a>). mnbvmar has earned his first place by solving 4 problems in just over an hour, 15 minutes faster than anybody else — and then protected his lead from Syloviaely's surge by finding 5 successful challenges. Very well done!<br /><br /><a href="http://codeforces.com/contest/936/problem/C">Problem C</a> provided a level playing field for experienced and inexperienced competitors, as it had nothing to do with standard algorithms or even standard ideas. You are given a string <i>s</i> of length 2000, and need to transform it into a string <i>t</i> of the same length using at most 6100 operations, or report that it's impossible. In one operation, you can split the current string into two, reverse the second part, and then put the first part after the reversed second part: for example, you can obtain the string <i>fedcab</i> from the string <i>abcdef</i> in one operation. Either part is allowed to be empty. Can you see a way to do the transformation of length <i>n</i> in at most 3<i>n</i> operations? Somewhat surprisingly, there are many working approaches in this problem.<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/Fev6opPg9bU" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/02/an-infinite-ratio-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-6062008360648049262018-02-18T22:49:00.002+03:002018-02-19T12:16:00.508+03:00A Fenwick bound week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-awKrgQjT350/WomSwzr2qjI/AAAAAAAB-2M/zVG2IlWG3joqtmemRnDmLo46kp1FI3jzACLcBGAs/s1600/cf462top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="272" data-original-width="1102" height="157" src="https://3.bp.blogspot.com/-awKrgQjT350/WomSwzr2qjI/AAAAAAAB-2M/zVG2IlWG3joqtmemRnDmLo46kp1FI3jzACLcBGAs/s640/cf462top5.png" width="640" /></a></div>Codeforces was quite active this week with two Division 1 rounds. The 462nd one took place on Wednesday (<a href="http://codeforces.com/contest/933">problems</a>, <a href="http://codeforces.com/contest/933/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57763">analysis</a>). Um_nik was the only one able to solve all problems, submitting one in the last minute — but he would've won without that one anyway. Congratulations on the great performance!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-wPeOFwyYe0w/WomTpEUvugI/AAAAAAAB-2U/iHnypLHmY1UZAPXx4idNdRAsn9hp-iTBQCLcBGAs/s1600/cf463top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="270" data-original-width="1104" height="156" src="https://3.bp.blogspot.com/-wPeOFwyYe0w/WomTpEUvugI/AAAAAAAB-2U/iHnypLHmY1UZAPXx4idNdRAsn9hp-iTBQCLcBGAs/s640/cf463top5.png" width="640" /></a></div>Round 463 took place on Thursday (<a href="http://codeforces.com/contest/932">problems</a>, <a href="http://codeforces.com/contest/932/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57796">analysis</a>). It was Radewoosh's turn to be the only competitor to solve all problems, with 7 minutes to spare this time. Well done!<br /><br />He <a href="http://codeforces.com/blog/entry/57257">will be representing</a> his university in the upcoming ICPC World Finals in Beijing, along with a few others that you can see in the screenshots on the left, so the competition there promises to be very interesting :)<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-pk4T1lcwi-E/WomWgMWcaxI/AAAAAAAB-2g/ScEKlOCldAY8899XQvtQczwQw5ont7LCQCLcBGAs/s1600/oc1718gomeltop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="208" data-original-width="802" height="164" src="https://4.bp.blogspot.com/-pk4T1lcwi-E/WomWgMWcaxI/AAAAAAAB-2g/ScEKlOCldAY8899XQvtQczwQw5ont7LCQCLcBGAs/s640/oc1718gomeltop5.png" width="640" /></a></div>Finally, the Open Cup Grand Prix of Gomel on Sunday provided another glimpse into full team performances (<a href="http://opentrains.snarknews.info/~ejudge/res/res10412">results</a>, top 5 on the left). Moscow SU team Red Panda has found the winning ways again, this time thanks to being the only team to solve problem I — and doing it in the beginning of the third hour of the contest, which is quite unusual for a problem with only one solver. Congratulations!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-WEfvf-hmRJM/WomYxZ4FE1I/AAAAAAAB-2w/xqqyJFnE1i0b1I_1PVw9XubXs64dTi58QCLcBGAs/s1600/IMG_20180205_124154.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://4.bp.blogspot.com/-WEfvf-hmRJM/WomYxZ4FE1I/AAAAAAAB-2w/xqqyJFnE1i0b1I_1PVw9XubXs64dTi58QCLcBGAs/s640/IMG_20180205_124154.jpg" width="640" /></a></div><a href="http://petr-mitrichev.blogspot.com/2018/02/a-leafy-week.html">Last week</a>, I have mentioned a data structure problem from the Open Cup. <i>n</i> people are coming to lunch and forming a queue. The people are split into disjoint groups. When <i>i</i>-th person comes to the lunch, if there's nobody from their group <i>c<sub>i</sub></i> already in the queue, they stand at the back of the queue. When there is somebody from their group already in the queue, they can also stand next to any such person (directly before or after), but only if they would be cutting in front of at most <i>a<sub>i</sub></i> people by doing so. If they have multiple positions where they can join the queue according to the above restrictions, they always pick the front-most one. Initially the queue is empty. Given the values of <i>c<sub>i</sub></i> and <i>a<sub>i</sub></i> in the order the people come to lunch, how will the final queue look like?<br /><br />One could immediately start thinking about standard approaches applicable in this problem. The fact that we need to be able to run a query against the last <i>a<sub>i</sub></i> people in the queue suggests that we should probably use a balanced tree that supports quick splitting, like a treap, and maintain the size of the subtree plus some additional structures necessary to answer queries in each node. This approach can probably be made to work, but the nice thing about the problem is that we can obtain a much easier to code solution.<br /><br />Since every person only ever joins the queue either in last position, or next to a person from their group, if we maintain the queue as a list of segments of people from the same group, in other words as a list of (group, counter) pairs, then the only operations we need to support is to increment a counter and to add a new group to the end of the list. Now in order to find a place for the next person we need to find the set of groups corresponding to the last <i>a<sub>i</sub></i>+1 people, and then find the first group of the required type in that set.<br /><br />In order to find the boundary on the group level, we need to be able to find the largest prefix for which the sum of counters does not exceed a given value. This can be done in O(log<i>n</i>) by maintaining the counters in <a href="http://petr-mitrichev.blogspot.com/2013/05/fenwick-tree-range-updates.html">a Fenwick tree</a>, I will give more details below.<br /><br />And in order to find the first group of the required kind after the given one, we can simply maintain a vector of indices of groups for each kind. Since we only ever create new groups at the end, these indices never change, and a simple binary search can find the first group of the given kind after a given index.<br /><br />Finally, after we know what happens with groups we know at which position does each person enter the queue, but we still need to output the final order. This can be done using the same Fenwick tree with "largest prefix with sum not exceeding <i>x</i>" operation we already used: we go from the end and maintain the free spots in the Fenwick tree.<br /><br />So instead of a balanced tree with extra information in nodes, we've managed to get by using two very easy to code data structures: a Fenwick tree, and a vector. The solution is O(<i>n</i>log<i>n</i>), and the constant hidden in O() is also really small.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-E6bkp1G9WUQ/WonYDa4qttI/AAAAAAAB-3E/hP1Kq8fxV2cG5V3F8N7peZ0u0O_yJYU1wCLcBGAs/s1600/IMG_20180205_125812.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://1.bp.blogspot.com/-E6bkp1G9WUQ/WonYDa4qttI/AAAAAAAB-3E/hP1Kq8fxV2cG5V3F8N7peZ0u0O_yJYU1wCLcBGAs/s640/IMG_20180205_125812.jpg" width="640" /></a></div>This solution used the following non-standard operation on <a href="http://petr-mitrichev.blogspot.com/2013/05/fenwick-tree-range-updates.html">a Fenwick tree</a>: find the largest prefix with sum not exceeding <i>x</i>. A naive implementation using binary search and Fenwick prefix sums would give O(log<sup>2</sup><i>n</i>) complexity, which would most likely still be fast enough to get the problem accepted. However, <a href="http://codeforces.com/profile/tgehr">tgehr</a> has pointed out to me that the standard Fenwick tree allows an extremely simple O(log<i>n</i>) way to answer this question.<br /><br />Suppose our Fenwick tree has <i>n</i> elements, and <i>k</i> is the largest power of 2 not exceeding <i>n</i>. The (2<sup><i>k</i></sup>-1)-th element of the Fenwick tree array contains the sum <i>s</i> of elements on the segment [0;2<sup><i>k</i></sup>-1], so by comparing <i>x</i> with just this number we know if our answer is below or above 2<sup><i>k</i></sup>. Let's suppose it's above 2<sup><i>k</i></sup>. Then we notice that (2<sup><i>k</i></sup>+2<sup><i>k-1</i></sup>-1)-th element of the Fenwick tree array contains the sum of elements on the segment [2<sup><i>k</i></sup>;2<sup><i>k</i></sup>+2<sup><i>k-1</i></sup>-1], so by comparing <i>x</i>-<i>s</i> with just this number we know if our answer is below or above 2<sup><i>k</i></sup>+2<sup><i>k-1</i></sup>. We continue traversing the powers of two in the same manner, and it turns out that the standard Fenwick tree stores exactly the sums needed to execute this binary search! Here's the actual code:<br /><br /><pre style="font-family: "Courier New"; font-size: 9pt;"><span style="color: navy; font-weight: bold;">private int </span>upperBound(<span style="color: navy; font-weight: bold;">int</span>[] f, <span style="color: navy; font-weight: bold;">int </span>x) {<br /> <span style="color: navy; font-weight: bold;">int </span>res = <span style="color: blue;">0</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>max = Integer.<span style="font-style: italic;">numberOfTrailingZeros</span>(<br /> Integer.<span style="font-style: italic;">highestOneBit</span>(f.<span style="color: #660e7a; font-weight: bold;">length</span>));<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>k = max; k >= <span style="color: blue;">0</span>; --k) {<br /> <span style="color: navy; font-weight: bold;">int </span>p = res + (<span style="color: blue;">1 </span><< k) - <span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">if </span>(p < f.<span style="color: #660e7a; font-weight: bold;">length </span>&& f[p] <= x) {<br /> x -= f[p];<br /> res += <span style="color: blue;">1 </span><< k;<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">return </span>res;<br />}</pre><pre style="font-family: "Courier New"; font-size: 9pt;"></pre>Thanks for reading, and check back next week.</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/nDCbo53MML8" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/02/a-fenwick-bound-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-77502096597372205262018-02-12T01:33:00.002+03:002018-02-12T01:33:48.754+03:00A leafy week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-Lc0I4uXSuXk/WoCzS85OQjI/AAAAAAAB-jo/CBZczFflSIEdf5i3u6u9Z9_LLLBej1UDwCLcBGAs/s1600/srm729top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="95" data-original-width="639" height="94" src="https://4.bp.blogspot.com/-Lc0I4uXSuXk/WoCzS85OQjI/AAAAAAAB-jo/CBZczFflSIEdf5i3u6u9Z9_LLLBej1UDwCLcBGAs/s640/srm729top5.png" width="640" /></a></div>This week featured two weekend contests. First, TopCoder SRM 729 took place on Saturday (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17082">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17082&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="https://youtu.be/Ugf5kbxmV_8">my screencast with commentary</a>). If you sum up the problem columns in the screenshot on the left, you can notice that the sum doesn't match the score column, and that's because the match presented ample opportunities for challenging. You can see on the screencast as I try to prepare an uber-challenge-case for the 450 during the intermission, and spent the beginning of the challenge phase getting it to work, while many solutions were already being challenged. uwi has made the best use of the challenge opportunities and thus claimed the first place. Well done!<br /><br />Most of the challenge opportunities presented themselves in <a href="https://community.topcoder.com/stat?c=problem_statement&pm=14716&rd=17082">the medium problem</a>, which looked very standard at first glance. You are given a 1000x1000 grid. In one jump, you can move from a cell to any other cell that's <i>at least</i> the given distance <i>d</i> away — you can't jump very close. What is the smallest number of jumps required to get from one given cell to another given cell?<br /><br />We could use a standard breadth-first search to solve this problem, but we have 1 million cells and potentially 1 million jumps from each cell, so the total running time would be on the order of 10<sup>12</sup> which is too slow. Can you see at least one way to speed this solution up? (there are many!)<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-LzovCJtgAVM/WoC2if692jI/AAAAAAAB-j0/wo5tqAS8xooSkg_5XqtL1qpxMkl-ivUnwCLcBGAs/s1600/oc1718khamovnikitop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="356" data-original-width="1508" height="150" src="https://3.bp.blogspot.com/-LzovCJtgAVM/WoC2if692jI/AAAAAAAB-j0/wo5tqAS8xooSkg_5XqtL1qpxMkl-ivUnwCLcBGAs/s640/oc1718khamovnikitop5.png" width="640" /></a></div>The other weekend contest was the Open Cup 2017-18 Grand Prix of Khamovniki (<a href="http://opentrains.snarknews.info/~ejudge/res/res10411">results</a>, top 5 on the left). Unlike the previous round, the active ICPC teams from Russia were not at the top of the standings, with only two ICPC teams from Asia and a veteran team Past Glory able to solve 10 problems — congratulations, and especially to Seoul National U 2 team for another Open Cup victory!<br /><br /><a href="https://official.contest.yandex.ru/opencupXVIII/contest/7410/problems/D/">Problem D</a> "Lunch Queue" was from the rare species of data structure problems that I enjoyed solving. <i>n</i> people are coming to lunch and forming a queue. The people are split into disjoint groups. When <i>i</i>-th person comes to the lunch, if there's nobody from their group <i>c<sub>i</sub></i> already in the queue, they stand at the back of the queue. When there is somebody from their group already in the queue, they can also stand next to any such person (directly before or after), but only if they would be cutting in front of at most <i>a<sub>i</sub></i> people by doing so. If they have multiple positions where they can join the queue according to the above restrictions, they always pick the front-most one. Initially the queue is empty. Given the values of <i>c<sub>i</sub></i> and <i>a<sub>i</sub></i> in the order the people come to lunch, how will the final queue look like?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-HrwOnXLaIrQ/WoDEeesX_5I/AAAAAAAB-k8/VSRIGkUmZYI8pxDgyKrQVqF6jC-sJk4MgCLcBGAs/s1600/IMG_20180120_134215.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="817" data-original-width="1600" height="326" src="https://1.bp.blogspot.com/-HrwOnXLaIrQ/WoDEeesX_5I/AAAAAAAB-k8/VSRIGkUmZYI8pxDgyKrQVqF6jC-sJk4MgCLcBGAs/s640/IMG_20180120_134215.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/02/a-red-panda-week.html">my previous summary</a>, I have mentioned a hard AtCoder problem: you are given a rooted tree with <i>n</i> vertices, and each vertex contains an integer between 1 and <i>n</i>, each number appearing exactly once. Your goal is to rearrange the numbers in such a way that vertex 1 has number 1, vertex 2 has number 2, and so on. You're allowed to do the following transformation: take the path connecting the root of the tree with any vertex <i>v</i>, and cyclically rotate it, placing the number that was in root into <i>v</i>, the number that was in <i>v</i> into the parent of <i>v</i>, and so on — essentially a generalization of insertion sort from a single path to an arbitrary tree. You need to sort the tree in at most 25000 operations for <i>n</i>=2000.<br /><br />I did not solve the problem myself, so I will describe the solution from the official analysis. First, we can notice that given any leaf, we can put the correct value into it in <=<i>n</i> operations (first pull it up to the root, then put it into the leaf in one rotation, and then never touch this leaf again, so we could sort everything in <i>n</i><sup>2</sup> operations, which is too much.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-yD6nzIjFeMQ/WoDDiLabZlI/AAAAAAAB-kg/cH1yleFlC38Y3YO6x2g5Qht3rCwj4RPTACKgBGAs/s1600/IMG_20180211_231731.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="858" data-original-width="1600" height="213" src="https://1.bp.blogspot.com/-yD6nzIjFeMQ/WoDDiLabZlI/AAAAAAAB-kg/cH1yleFlC38Y3YO6x2g5Qht3rCwj4RPTACKgBGAs/s400/IMG_20180211_231731.jpg" width="400" /></a></div>However, we can notice that in this approach we have quite a lot of freedom for choosing the moves that pull the given number up. We could try to use this freedom to try to pull up the numbers for all leaves, not just for one leaf. But even that would not be enough, as when our rooted tree is a chain it only has one leaf. However, we know that for a chain a simple solution is possible, called the normal insertion sort: we'll do exactly <i>n</i> operations, and after <i>k</i> operations we'd have <i>k</i> bottom numbers of the chain already sorted in correct order, in each turn inserting the new number to the appropriate place.<br /><br />Now we need to combine the ideas of pulling the required number from anywhere in the tree with the idea of filling a chain in one O(<i>n</i>) operation stage in such a way that the number of stages would be O(log(<i>n</i>)) for any rooted tree. More precisely, we will find all <i>leaf chains</i> in the tree — chains that hang at the bottom with nothing else attached to them, and fill them all with correct values in one stage. This guarantees O(log(<i>n</i>)) stages since the number of leaves is divided by at least two after each stage.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-4sdw9pAN-Pk/WoDDnJhsUCI/AAAAAAAB-ko/2oavhsGNvUQzRAw4LjV15sa4KAvGhZ0OwCKgBGAs/s1600/IMG_20180211_232515.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1600" data-original-width="1330" height="640" src="https://2.bp.blogspot.com/-4sdw9pAN-Pk/WoDDnJhsUCI/AAAAAAAB-ko/2oavhsGNvUQzRAw4LjV15sa4KAvGhZ0OwCKgBGAs/s640/IMG_20180211_232515.jpg" width="530" /></a></div>Whenever the root of the tree has a <i>useful</i> value — one that must be in one of the leaf chains — we will send it there, inserting it into the correct place relative to other values that we've sent to the same leaf chain, just like the insertion sort approach above. And when the root has a useless value, we need to send it somewhere, so let's send it to any position which contains a useful number, but such that all its subtree contains either only useless numbers, or useful numbers that are already placed into the corresponding leaf chain (in other words, the numbers that we don't need to get to the root anymore). This will push this useful number towards the root, and create a subtree that doesn't have any numbers that we want to get to the root.<br /><br />Why does this cycle finish eventually, and more importantly why does it run in O(<i>n</i>) operations? Each useful value passes through the root only once. A useless value, after passing through the root, ends up being in a dormant subtree, and would never pass through the root again, unless we need to touch this subtree because we're putting a useful value into it. In each such operation, at most one value moves from a dormant subtree back into circulation, and thus can reach the root again. So the total number of times a useless value that we've already seen once returns to the root does not exceed the total number of times we put a useful value into its place, meaning that the total number of operations for one stage is at most <i>n</i> plus total size of leaf paths, so O(<i>n</i>).<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/wO-7fBCpt1c" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/02/a-leafy-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-33498081314498190062018-02-10T01:16:00.000+03:002018-02-10T01:16:32.193+03:00A Red Panda week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-dn2cZ_z7rCA/WnzHMhO8HpI/AAAAAAAB-gE/dvcULUKJqMcJt9PDvLMtFnIYuoCOFGzSwCLcBGAs/s1600/cf459top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="274" data-original-width="1449" height="120" src="https://2.bp.blogspot.com/-dn2cZ_z7rCA/WnzHMhO8HpI/AAAAAAAB-gE/dvcULUKJqMcJt9PDvLMtFnIYuoCOFGzSwCLcBGAs/s640/cf459top5.png" width="640" /></a></div>Last week started with Codeforces Round 459 on Monday (<a href="http://codeforces.com/contest/917">problems</a>, <a href="http://codeforces.com/contest/917/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57420">analysis</a>). Once again the hardest problem was too much to handle, and OO0OOO00O0OOO0O00OOO0OO was quite a lot faster on the first four. Congratulations on the win!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-2WIfwix18RA/Wn2O69ZjvRI/AAAAAAAB-hs/37H2-P_sbogIAawhvbnUvCMdorAPLuepgCLcBGAs/s1600/apc001top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="495" data-original-width="1173" height="268" src="https://1.bp.blogspot.com/-2WIfwix18RA/Wn2O69ZjvRI/AAAAAAAB-hs/37H2-P_sbogIAawhvbnUvCMdorAPLuepgCLcBGAs/s640/apc001top5.png" width="640" /></a></div>On Saturday, AtCoder hosted an unusually long 5-hour contest called AtCoder Petrozavodsk Contest 001 (<a href="https://apc001.contest.atcoder.jp/assignments">problems</a>, <a href="https://apc001.contest.atcoder.jp/standings#page_1">results</a>, top 5 on the left, <a href="https://youtu.be/oiJW5-wp_a8">my screencast</a>, <a href="https://img.atcoder.jp/apc001/editorial.pdf">analysis</a>). There was a huge difficulty gap between the first six problems, all of which were solved by 52 contestants, and the last four. Only two contestants have managed to solve two of the more difficult kind. Congratulations to Marcin and vepifanov on this amazing feat!<br /><br />The most approachable problem of the difficult kind, <a href="https://apc001.contest.atcoder.jp/tasks/apc001_h">problem H</a>, went like this: you are given a rooted tree with <i>n</i> vertices, and each vertex contains an integer between 1 and <i>n</i>, each number appearing exactly once. Your goal is to rearrange the numbers in such a way that vertex 1 has number 1, vertex 2 has number 2, and so on. You're allowed to do the following transformation: take the path connecting the root of the tree with any vertex <i>v</i>, and cyclically rotate it, placing the number that was in root into <i>v</i>, the number that was in <i>v</i> into the parent of <i>v</i>, and so on — essentially a generalization of insertion sort from a single path to an arbitrary tree. You need to sort the tree in at most 25000 operations for <i>n</i>=2000.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-PE373O3yt64/Wn4YUyCa5rI/AAAAAAAB-ik/4kgq-Ju3IqQgjg2qHPjMsUFhrs6Hp6QpgCLcBGAs/s1600/oc1718koreatop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="269" data-original-width="886" height="194" src="https://4.bp.blogspot.com/-PE373O3yt64/Wn4YUyCa5rI/AAAAAAAB-ik/4kgq-Ju3IqQgjg2qHPjMsUFhrs6Hp6QpgCLcBGAs/s640/oc1718koreatop5.png" width="640" /></a></div>Finally, the Open Cup season came back from the winter break on Sunday with the Grand Prix of Korea (<a href="http://opencup.ru/index.cgi?data=macros/stage&head=index&menu=index&stg=10&region=main&ncup=oci&class=oci">results</a>, top 5 on the left). Moscow State University team Red Panda have won the contest by a whole problem ahead of other super strong university and veteran teams. Congratulations on the victory!<br /><br />Since the problems for this contest came from Korea, I can't help but wonder if top Korean/Asian ICPC teams have also solved this problemset elsewhere, or will do that later. Does anybody have some scoreboards to share?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-dw2qhzaPsTs/Wn4dYoChGqI/AAAAAAAB-jA/lUUu-R1xT6wSofmvAMSSrGY0cZ8VaELJgCLcBGAs/s1600/IMG_20180113_151939%2B%25281%2529.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://4.bp.blogspot.com/-dw2qhzaPsTs/Wn4dYoChGqI/AAAAAAAB-jA/lUUu-R1xT6wSofmvAMSSrGY0cZ8VaELJgCLcBGAs/s640/IMG_20180113_151939%2B%25281%2529.jpg" width="640" /></a></div><a href="http://petr-mitrichev.blogspot.com/2018/01/a-topcoder-week.html">Last week</a> I have mentioned a 250-point TopCoder problem: you are given 50 integers, each up to a billion. In one step, you can take any integer that is greater than 1 and divide it by 2. If it was odd, you can choose whether to round up or down. What is the smallest number of steps required to make all integers equal?<br /><br />My thinking went like this: instead of deciding immediately whether we'd round up or down, let's keep a set of possible values for each integer, which will always be a segment [<i>min</i>;<i>max</i>]. After dividing some numbers by 2 a few times, we'll have such segment for each number. Whenever these segments all have a non-empty intersection, we're done.<br /><br />For example, if our initial numbers are 3, 5, and 13, then we start with segments [3;3], [5;5] and [13;13]. After dividing the last number by 2 we have [3;3], [5;5] and [6;7]. After doing it again we have [3;3], [5;5] and [3;4]. After dividing the second number by 2 we have [3;3], [2;3] and [3;4]. Now all our segments intersect in point 3, so we can make all numbers equal to 3.<br /><br />The only remaining thing is to decide which numbers to divide by 2, but it actually flows naturally from the above observation. What does it mean when all segments do <i>not</i> have an intersection? It means that there are two segments [<i>a</i>;<i>b</i>] and [<i>c</i>;<i>d</i>] that do not intersect (this is not true for arbitrary sets, but it is true for segments), without loss of generality we have <i>b</i><<i>c</i>. But then we must divide the segment [<i>c</i>;<i>d</i>] by 2, otherwise it will never intersect with [<i>a</i>;<i>b</i>] or its descendants. So as long as we don't have an intersection, we can always find a segment to divide by 2 this way.<br /><br />Thanks for reading, and check back in a couple of days for this week's summary!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/9Mtp9zJuvZs" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/02/a-red-panda-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-62919371577215882682018-01-28T23:54:00.001+03:002018-01-28T23:54:11.350+03:00A TopCoder week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-cQG2v6fYUnE/Wm4x_-wYYHI/AAAAAAAB94E/AFdGzLaSjj42FLPl8PL0u4ufMkwtwqRnQCLcBGAs/s1600/srm728top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="98" data-original-width="639" src="https://4.bp.blogspot.com/-cQG2v6fYUnE/Wm4x_-wYYHI/AAAAAAAB94E/AFdGzLaSjj42FLPl8PL0u4ufMkwtwqRnQCLcBGAs/s1600/srm728top5.png" /></a></div>TopCoder SRM 728 was this week's contest (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17064">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17064&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57324?#comment-409800">analysis</a>, <a href="https://youtu.be/7ZBljIFRgFU">my screencast</a>). <a href="https://community.topcoder.com/stat?c=problem_statement&pm=13257&rd=17064">The hard problem</a> was a relatively straightforward application of the Burnside's lemma, which I <a href="http://petr-mitrichev.blogspot.com/2008/11/burnsides-lemma.html">described in this blog</a> more than 9 (!) years ago. <a href="https://community.topcoder.com/stat?c=problem_statement&pm=14807&rd=17064">The easy problem</a> allowed a ton of completely different correct approaches, and thus in a sense was more interesting: you are given 50 integers, each up to a billion. In one step, you can take any integer that is greater than 1 and divide it by 2. If it was odd, you can choose whether to round up or down. What is the smallest number of steps required to make all integers equal?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-mjAt8oV_QaY/Wm42QoPl5dI/AAAAAAAB94Y/v-3-1Rp3XH02X0Vu7vxt9YKO8IS_MYcNgCLcBGAs/s1600/srm727top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="149" data-original-width="775" height="122" src="https://2.bp.blogspot.com/-mjAt8oV_QaY/Wm42QoPl5dI/AAAAAAAB94Y/v-3-1Rp3XH02X0Vu7vxt9YKO8IS_MYcNgCLcBGAs/s640/srm727top5.png" width="640" /></a></div>I've also just realized that I forgot to cover the previous SRM, TopCoder SRM 727 which took place two weeks ago very early in the morning (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17055">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17055&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57012?#comment-406865">analysis</a>). lych_cys, participating in Division 1 for the first time, has squeezed the victory during the challenge phase from wxh010910, who was doing only his third Division 1 round himself, while the rest of the pack was more than 100 points behind. Congratulations to the winning newcomers!<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/M-xetJx0tx0" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/01/a-topcoder-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-23919236988833755622018-01-22T03:49:00.002+03:002018-01-22T03:49:32.346+03:00A mean median week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-3D_vOE0aPGM/WmUSQXOXF-I/AAAAAAAB8-g/HCHd1NV5YZo2EfIDzfFHojrU5Lo7wYWogCLcBGAs/s1600/cf458top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="1106" height="156" src="https://3.bp.blogspot.com/-3D_vOE0aPGM/WmUSQXOXF-I/AAAAAAAB8-g/HCHd1NV5YZo2EfIDzfFHojrU5Lo7wYWogCLcBGAs/s640/cf458top5.png" width="640" /></a></div>This week Codeforces hosted its Round 458, also known as CodeCraft-18 (<a href="http://codeforces.com/contest/914">problems</a>, <a href="http://codeforces.com/contest/914/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/57250">analysis</a>). Syloviaely has made a solid claim to the first place by submitting the 3000-point problem F just 13 minutes after the contest started (was it a previously known problem, or was it really fast solving?..), but SkyDec has gathered a whopping 800 points from challenges on two different problems, 450 on B and 350 on C, and thus finished first with quite a margin. Congratulations!<br /><br />This round also had a great side story — the top-rated contestant, Um_nik, decided to go for the hardest problem H, and that bet did not play out well as he spent too much time on it and as I understood also discovered an incorrect output in one of the samples (which means an incorrect reference solution?..). The admins <a href="http://codeforces.com/blog/entry/57208?#comment-408909">have decided</a> to make the round unrated for him because of the incorrect sample issue, but he asked them to reverse that decision (and thus make him lose rating) because he <a href="http://codeforces.com/blog/entry/57208?#comment-408890">deemed</a> the issue did not impact him significantly. Kudos to Um_nik for the admirable honesty!<br /><br /><div style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><iframe allow="autoplay; encrypted-media" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/_EAAPHgEFYk" width="560"></iframe></div><a href="http://petr-mitrichev.blogspot.com/2018/01/an-umnik-week.html">Last week</a>, I have mentioned <a href="https://agc020.contest.atcoder.jp/tasks/agc020_c">an AtCoder problem</a>: you are given <i>n</i><=2000 positive integers, each up to <i>a</i><=2000. Consider all 2<sup><i>n</i></sup>-1 nonempty subsets, and compute the sum of the numbers in each subset. Now sort those sums, and find the median sum — the 2<sup><i>n</i>-1</sup>-th in the sorted order. You need to output this median sum. Time limit is 2 seconds.<br /><br />At first sight, the problem appears somewhat standard: we can run the knapsack dynamic programming that computes the number of ways to get each possible sum, and then go from lower sums to higher sums until we get 2<sup><i>n</i>-1 </sup>in total.<br /><br />This has two issues, though: first, the maximum sum is <i>n</i>*<i>a</i>, so the dynamic programming runs in O(<i>n</i><sup>2</sup>*<i>a</i>). Second, the number ways to get each sum can be on the order of 2<sup><i>n</i></sup>, so we need big integers, and the running time after accounting for that is more like O(<i>n</i><sup>3</sup>*<i>a</i>). Even O(<i>n</i><sup>2</sup>*<i>a</i>) is too slow.<br /><br />Here comes the insight: notice that all sums can be split in pairs. If the sum of all numbers is <i>s</i>, then for each subset with sum <i>x</i> we'll have the complementary subset with sum <i>s</i>-<i>x</i> (let's also include the empty subset). One of those sums is <=<i>s</i>/2, and the other is >=<i>s</i>/2, so the median is roughly equal to <i>s</i>/2. More precisely, the median will always be the smallest sum that is >=<i>s</i>/2.<br /><br />This allows us to get rid of the big integers, as now we merely need to know which sums are possible, and don't care about the number of ways anymore. Moreover, we can use bitmasks to speed up this O(<i>n</i><sup>2</sup>*<i>a</i>) solution ~32-64 times which makes it fast enough.<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/mQaypV05LHA" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/01/a-mean-median-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-60810786266368224952018-01-15T00:32:00.000+03:002018-01-15T00:32:05.366+03:00An Um_nik week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-CvofOhRiXd8/WlvF7X7Wo8I/AAAAAAAB80Y/j425zUJ8W2oRheJwgD6bGWslRZSMGraOACLcBGAs/s1600/hello2018top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="274" data-original-width="1106" height="158" src="https://1.bp.blogspot.com/-CvofOhRiXd8/WlvF7X7Wo8I/AAAAAAAB80Y/j425zUJ8W2oRheJwgD6bGWslRZSMGraOACLcBGAs/s640/hello2018top5.png" width="640" /></a></div>This week's contests shared one of the problemsetters — and the winner. On Monday, Codeforces hosted its Hello 2018 round (<a href="http://codeforces.com/contest/913">problems</a>, <a href="http://codeforces.com/contest/913/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/56992">analysis</a>). With the hardest problem having too much piecewise polynomial integration, the competition focused on the remaining seven. Only Um_nik could get them all, and even with 24 minutes to spare — congratulations!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-ztxD2wBTnxc/WlvFLlqvKgI/AAAAAAAB80Q/s6fv3UfXUzoxblK_WsUlnJT4xwXzAtjiwCLcBGAs/s1600/agc020top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="446" data-original-width="905" height="314" src="https://2.bp.blogspot.com/-ztxD2wBTnxc/WlvFLlqvKgI/AAAAAAAB80Q/s6fv3UfXUzoxblK_WsUlnJT4xwXzAtjiwCLcBGAs/s640/agc020top5.png" width="640" /></a></div>On Sunday, AtCoder Grand Contest 020 started <a href="https://atcoder.jp/post/171">the race to Japan</a> (<a href="https://agc020.contest.atcoder.jp/assignments">problems</a>, <a href="https://agc020.contest.atcoder.jp/standings#page_1">results</a>, top 5 on the left, <a href="https://youtu.be/6rFmqWl_RwA">my screencast</a>, <a href="https://img.atcoder.jp/agc020/editorial.pdf">analysis</a>). Once again the hardest problem by tourist was a tiny bit too hard (and I even solved it after the contest by integrating polynomials, probably inspired by the analysis of the previous contest), and once again Um_nik was way faster than everybody else on the remaining problems — big congratulations again!<br /><br />I found <a href="https://agc020.contest.atcoder.jp/tasks/agc020_c">problem C</a> quite cute. You are given <i>n</i><=2000 positive integers, each up to 2000. Consider all 2<sup>n</sup>-1 nonempty subsets, and compute the sum of the numbers in each subset. Now sort those sums, and find the median sum — the 2<sup>n-1</sup>-th in the sorted order. You need to output this median sum. Time limit is 2 seconds.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-u4HjmJRYhmY/WlvME4BS8jI/AAAAAAAB804/pfJxX291iLgQjEjHpAVce5B1bESvjhipgCLcBGAs/s1600/IMG_20180106_134848.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1165" data-original-width="1600" height="466" src="https://1.bp.blogspot.com/-u4HjmJRYhmY/WlvME4BS8jI/AAAAAAAB804/pfJxX291iLgQjEjHpAVce5B1bESvjhipgCLcBGAs/s640/IMG_20180106_134848.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2018/01/an-otherland-week.html">my last post</a> I've discussed what was arguably <a href="http://codeforces.com/gym/101611/problem/E">the hardest contest problem</a> of 2017: you are given an undirected graph with at most 200000 vertices and edges. Your goal is to split its vertices into four disjoint groups A, B, C, D in such a way that:<br /><ul style="text-align: left;"><li>The subgraph formed by groups A+B is connected and has at least 2 vertices.</li><li>The subgraph formed by groups C+D is connected and has at least 2 vertices.</li><li>Each vertex in A has an edge to each vertex in C.</li><li>There are no other edges between subgraphs A+B and C+D.</li></ul>If there are many possible solutions, you need to output any one of them.<br /><br />Thanks to great ideas and pointers by Swistakk, mnbvmar and lewin, we were able to put together a O(<i>n*</i>log<i>n</i>) solution — you can check it out in <a href="http://codeforces.com/blog/entry/56977?#comment-406180">this comment thread</a>.<br /><br />Thanks for reading, and check back next week!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/qeYbQW_151s" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/01/an-umnik-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-1039776478056843932018-01-08T01:37:00.001+03:002018-01-08T01:37:52.851+03:00An Otherland week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-i7OFSaWC9b0/WlKgbzS90mI/AAAAAAAB8qI/wKk5N-WuvvIE3cAT6G3cOuu2OmdRkLQYQCKgBGAs/s1600/IMG_20180107_233307.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1104" data-original-width="1600" height="275" src="https://1.bp.blogspot.com/-i7OFSaWC9b0/WlKgbzS90mI/AAAAAAAB8qI/wKk5N-WuvvIE3cAT6G3cOuu2OmdRkLQYQCKgBGAs/s400/IMG_20180107_233307.jpg" width="400" /></a></div>The first week of 2018 was quite calm, with the most devoted taking stabs at the still-ongoing <a href="https://contest.yandex.com/newyear2018/contest/7006/standings/">Prime New Year contest</a> by snarknews. Only one problem remains unsolved there, and it looks like it might take some team effort to crack it. I think it's within the spirit of the contest to discuss its problems publicly (after all, many even have analysis posted online), so I propose to do just that. Please skip the following paragraphs if you don't want spoilers!<br /><br /><a href="https://contest.yandex.com/newyear2018/contest/7006/problems/19/">The problem</a> goes like this: you are given an undirected graph with at most 200000 vertices and edges. Your goal is to split its vertices into four disjoint groups A, B, C, D in such a way that:<br /><ul style="text-align: left;"><li>The subgraph formed by groups A+B is connected and has at least 2 vertices.</li><li>The subgraph formed by groups C+D is connected and has at least 2 vertices.</li><li>Each vertex in A has an edge to each vertex in C.</li><li>There are no other edges between subgraphs A+B and C+D.</li></ul>If there are many possible solutions, you need to output any one of them.<br /><br />I have been thinking about the problem for some time, but only have the following not very helpful observations:<br /><ul style="text-align: left;"><li>If the graph has an articulation point, then we can take this point as set A, any of the components it separates as set B, and the rest as C+D. So we can assume the graph is vertex-biconnected.</li><li>The answer is not always "Yes" - the smallest counterexample is the graph with 4 vertices and 5 edges. This example also shows that if a biconnected graph has a vertex of degree 2, then this vertex and both adjacent vertices must be in the same half of our splitting, since both ends of a splitting edge must have degree of at least 3 (two splitting edges plus one inside the half).</li><li>There are also counterexamples without vertices of degree 2.</li><li>There might be some hashing trick involved. I.e., for example suppose we assign a random number <i>a<sub>i</sub></i> to each vertex, and compute for each vertex the value sum(<i style="font-style: italic;">a<sub>i</sub></i><i style="font-style: italic;">-a<sub>j</sub></i>), where the summation goes over all adjacent vertices <i>j</i>. Then if we add up those values within one half of the graph (A+B in the above terminology), then almost everything will cancel out, leaving us |C|*<i>a</i><sub>A</sub>-|A|*<i>a</i><sub>C</sub>, where <i>a</i><sub>A</sub> is the sum of <i>a<sub>i </sub></i>in A. But where to go from here?..</li></ul>Please share your ideas!<br /><div><br /><div><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-gA9fL0DOvW0/WlKfGDCJ5bI/AAAAAAAB8p0/RJjBvR772KkgH0Fy5iD8AVK9GOdMEwNSwCLcBGAs/s1600/IMG_20180106_123832.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://4.bp.blogspot.com/-gA9fL0DOvW0/WlKfGDCJ5bI/AAAAAAAB8p0/RJjBvR772KkgH0Fy5iD8AVK9GOdMEwNSwCLcBGAs/s640/IMG_20180106_123832.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2017/12/best-problems-of-2017.html">my last post</a>, I have held a vote for the best problem of 2017. And the winner is <a href="https://agc018.contest.atcoder.jp/tasks/agc018_f">this AtCoder problem</a> by <a href="https://twitter.com/maroon_kuri">maroonrk</a>: you are given two rooted trees on the same set of 10<sup>5</sup> vertices. You need to assign integer weights to all vertices in such a way that for <i>each</i> subtree of <i>each</i> of the trees the sum of weights is either 1 or -1, or report that it's impossible. The solution is explained in <a href="http://petr-mitrichev.blogspot.com/2017/07/a-week-with-two-trees.html">this post</a>. Congratulations to the problemsetter and to AtCoder, and thanks to all problemsetters of 2017!<br /><br />Thanks for reading, and check back next week.</div></div></div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/T2pX8PrZ63k" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2018/01/an-otherland-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-43662458876712853732017-12-31T20:43:00.000+03:002017-12-31T20:43:21.135+03:00Best problems of 2017<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-1QN3REiUF74/WkkhYnCWiVI/AAAAAAAB8L0/Ci5aAA70WoQl9Azw6HFUDYFXD29Dx9p1gCLcBGAs/s1600/IMG_20171216_120511.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="300" src="https://2.bp.blogspot.com/-1QN3REiUF74/WkkhYnCWiVI/AAAAAAAB8L0/Ci5aAA70WoQl9Azw6HFUDYFXD29Dx9p1gCLcBGAs/s400/IMG_20171216_120511.jpg" width="400" /></a></div>I have reviewed all problems I've highlighted this year in my blog, and also the problems sent by the readers in response to my question — thanks a lot for your input! I've distilled everything to a shortlist of five problems (in chronological order):<br /><br /><ul style="text-align: left;"><li>The Open Cup problem about interactively proving the pigeonhole principle, by <a href="http://codeforces.com/profile/tunyash">tunyash</a>, with solution in <a href="http://petr-mitrichev.blogspot.com/2017/05/a-zero-pigeon-week.html">this post</a>.</li><li>The AtCoder problem about moving two chips on a subset of a DAG, by <a href="http://codeforces.com/profile/sugim48">sugim48</a>, with solution in <a href="http://petr-mitrichev.blogspot.com/2017/07/a-dynamic-nimber-week.html">this post</a>.</li><li>The IPSC problem about picking optimal sequences of red and black cards to get the first match, by <a href="http://codeforces.com/profile/misof">misof</a>, with solution in <a href="http://petr-mitrichev.blogspot.com/2017/07/a-red-black-week.html">this post</a>.</li><li>The AtCoder problem about assigning numbers to shared vertices of two trees so that all subtrees sum to +-1, by <a href="http://codeforces.com/profile/maroonrk">maroonrk</a>, with solution in <a href="http://petr-mitrichev.blogspot.com/2017/07/a-week-with-two-trees.html">this post</a>.</li><li>The Open Cup problem about interactively exploring a maze on Mars with a communication delay, by <a href="http://codeforces.com/profile/gassa">Gassa</a>, discussed in <a href="http://petr-mitrichev.blogspot.com/2017/12/a-delayed-week.html">this post</a>. </li></ul><br />What is the best problem of 2017?<br /><iframe allowtransparency="true" frameborder="0" height="176px" name="poll-widget-1975279128869797257" src="https://www.google.com/reviews/polls/display/-1975279128869797257/blogger_template/run_app?txtclr=%23666666&lnkclr=%235588aa&chrtclr=%235588aa&font=normal+normal+100%25+Georgia,+Serif&hideq=true&purl=http://petr-mitrichev.blogspot.com/" style="border: none; width: 100%;"></iframe> <br /><div>As usual around New Year, <a href="http://codeforces.com/blog/snarknews">snarknews</a> is hosting the Prime New Year contest, which consists of problems given at top-level team competitions in 2017 but not solved by anybody there. If you like real challenge and don't have much to do during the holidays, here's <a href="https://contest.yandex.com/newyear2018/contest/7006/enter/">your link</a> :)<br /><br />Guten Rutsch!<br /><br /></div></div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/D9800lGJ3P4" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2017/12/best-problems-of-2017.htmltag:blogger.com,1999:blog-1953325079793449971.post-18946464027473545752017-12-31T00:06:00.000+03:002017-12-31T00:06:16.396+03:00A sparse week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-CjcpnyxEKt0/WkfyOa_9D_I/AAAAAAAB8Ic/SEbicbD-LZ01WVhfYlcHpQM21bFiMLTCgCLcBGAs/s1600/goodbye2017top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="270" data-original-width="1104" height="156" src="https://4.bp.blogspot.com/-CjcpnyxEKt0/WkfyOa_9D_I/AAAAAAAB8Ic/SEbicbD-LZ01WVhfYlcHpQM21bFiMLTCgCLcBGAs/s640/goodbye2017top5.png" width="640" /></a></div>This week had the now traditional last contest of the year: Good Bye 2017 at Codeforces (<a href="http://codeforces.com/contest/908">problems</a>, <a href="http://codeforces.com/contest/908/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/56713">analysis</a>, <a href="https://youtu.be/I_dvna43rY0">my screencast</a>). The hardest problem H, after removing some layers, ended up being about finding a <a href="https://en.wikipedia.org/wiki/Graph_coloring#Vertex_coloring">proper vertex coloring</a> for a graph with <i>n</i><=23 vertices with the smallest number of colors. My first thought was: surely for such classical problem the best approaches are well-known? However, it turned out that the algorithms I could remember were a O(3<sup><i>n</i></sup>) subset dynamic programming and various heuristics to speed up backtracking, both of which seemed too slow. Thanks to <a href="http://www.wisdom.weizmann.ac.il/~dinuri/courses/11-BoundaryPNP/L01.pdf">this paper</a> I have now learned how to do this in O(<i>n</i>*2<sup><i>n</i></sup>), see point 4.2 "Inclusion-exclusion". Always nice to discover better approaches to classical problems!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-ASx3FWuA0f0/Wkf_dmdJLcI/AAAAAAAB8Jc/53-4Dum1fMwMT6FcOkEiRuDfNYRrgizrQCLcBGAs/s1600/IMG_20171217_191048.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://4.bp.blogspot.com/-ASx3FWuA0f0/Wkf_dmdJLcI/AAAAAAAB8Jc/53-4Dum1fMwMT6FcOkEiRuDfNYRrgizrQCLcBGAs/s640/IMG_20171217_191048.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2017/12/a-quadratic-week.html">my previous summary</a>, I have mentioned a TopCoder problem: you are given <i>n</i>=2000000 tasks, to be done in <i>m</i>=2000 days, at most one task per day (so most of the tasks will end up not being done). For each task you know three numbers: the segment of days when it can be done (from <i>l<sub>i</sub></i> to <i>r<sub>i</sub></i>), and the profit <i>c<sub>i</sub></i> from doing it. You need to pick the task to be done in each day in such a way that the total profit is maximized.<br /><br />This can be naturally viewed as a weighted maximum matching problem, which thanks to <a href="http://petr-mitrichev.blogspot.com/2017/12/a-transversal-week.html">the transversal matroid</a> can be solved by trying to add tasks to the matching in decreasing order of profit, trying to find the augmenting path each time, and only resetting the "visited" state of each vertex once the matching is increased (this is called "Kuhn's algorithm" in the Russian-speaking community, but I couldn't find an authoritative link for it). Since the matching is only increased <i>m</i> times, we process each edge of the graph at most <i>m</i> times. The problem is that we have O(<i>n</i>*<i>m</i>) edges, so the total running time seems to be O(<i>n</i>*<i>m</i><sup>2</sup>). However, we can notice that for each task that we end up not adding to the matching we process its edges only once (since we will never visit it in subsequent iterations), so the number of actually revisited edges is O(<i>m</i><sup>2</sup>), and the running time is only O(<i>n</i>*<i>m+</i><i>m</i><sup>3</sup>), which is of course still too slow.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-EGDWp0P6jeY/Wkf-PgWSvoI/AAAAAAAB8JM/AwbzfJnAH9Aev6-t_Iz1r_bCiCxwVBAkwCKgBGAs/s1600/IMG_20171230_215810.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="857" data-original-width="1600" height="342" src="https://4.bp.blogspot.com/-EGDWp0P6jeY/Wkf-PgWSvoI/AAAAAAAB8JM/AwbzfJnAH9Aev6-t_Iz1r_bCiCxwVBAkwCKgBGAs/s640/IMG_20171230_215810.jpg" width="640" /></a></div>But now we can use another relatively standard trick: let's reduce the number of "active" edges from O(<i>m</i><sup>2</sup>) to O(<i>m*</i>log<i>m</i>) in the following manner: instead of a matching problem we'll now have a flow problem, and we'll add auxiliary vertices between left and right parts. The vertices will correspond to segments. First, we'll pick the middle point <i>b</i>=<i>m</i>/2, and add vertices corresponding to segments [1,<i>b</i>], [2,<i>b</i>], ..., [<i>b</i>-1,<i>b</i>], [<i>b</i>,<i>b</i>], and infinite capacity edges in this manner: [1,<i>b</i>]->[2,<i>b</i>]->...->[<i>b</i>,<i>b</i>]; [1,<i>b</i>]->1; [2,<i>b</i>]->2; ..., [<i>b</i>,<i>b</i>]-><i>b</i>. The idea is that if we add an edge from some vertex in the left part to a segment [<i>a</i>,<i>b</i>], then the flow can then go to any vertex in the right part between <i>a</i> and <i>b</i> using those infinite edges, and only to those vertices. We handle the other half in a similar way: [<i>b</i>+1,<i>b</i>+1]<-[<i>b</i>+1,<i>b</i>+2]<-...<-[<i>b</i>+1,<i>m-1</i>]<-[<i>b</i>+1,<i>m</i>]. These two sets of segments already allow to handle any task that can be done in days <i>b</i> and <i>b</i>+1 using only two edges: to [<i>l<sub>i</sub></i>,<i>b</i>] and to [<i>b</i>+1,<i>r<sub>i</sub></i>].<br /><br />We can then apply the same procedure recursively to segments [1,<i>b</i>] and [<i>b</i>+1,<i>m</i>]: for example, we'll pick the middle point <i>c</i>=<i>b</i>/2, and build the auxiliary vertices [1,<i>c</i>]->[2,<i>c</i>]->...->[<i>c</i>,<i>c</i>] and [<i>c</i>+1,<i>c</i>+1]<-[<i>c</i>+1,<i>c</i>+2]<-...<-[<i>c</i>+1,<i>b</i>], and so on. Overall we'll have log<i>m</i> sets of <i>m</i> auxiliary vertices each (one per level of recursion), with two outgoing edges for each vertex, and every task can now be handled with at most two edges to auxiliary vertices instead of O(<i>m</i>) edges to the right part. This approach resembles the centroid decomposition of a tree when applied to a chain; we could also have used the sparse table to achieve the same result.<br /><br />Now our graph has O(<i>m</i>log<i>m</i>) active edges, and additional O(<i>n</i>) edges that are visited only once, so the total running time is O(<i>n</i>+<i>m</i><sup>2</sup>*log<i>m</i>), which is fast enough.<br /><br />That's it for the contests of 2017 — thanks for reading, and check back tomorrow (hopefully) for the best problems of 2017!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/wzWFDtvO31E" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2017/12/a-sparse-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-87234728919840945702017-12-30T23:03:00.002+03:002017-12-31T00:17:29.290+03:00A quadratic week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-BgQ1L6gYBMI/WkfiKSoPFsI/AAAAAAAB8Hk/KPs0Ny1Djxwf_NeC85Ax7V_5Arz1R_RngCLcBGAs/s1600/cf453top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="1103" height="158" src="https://1.bp.blogspot.com/-BgQ1L6gYBMI/WkfiKSoPFsI/AAAAAAAB8Hk/KPs0Ny1Djxwf_NeC85Ax7V_5Arz1R_RngCLcBGAs/s640/cf453top5.png" width="640" /></a></div>Last week had a relatively high density of contests, so much that two of them collided: Codeforces Round 453 and TopCoder SRM 726 took place at the same time on Tuesday. The Codeforces round started 25 minutes earlier, so we'll begin with it (<a href="http://codeforces.com/contest/901">problems</a>, <a href="http://codeforces.com/contest/901/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/56478">analysis</a>). dotorya had chosen a seemingly suboptimal order to solve the first four problems, as A and B required disproportionately much time relative to their value, but he was faster than the competition and thus still ended up in first place — well done!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-7ED5pgjKFxw/WkfjRHRAlGI/AAAAAAAB8Hw/BXZ5kLBwazcZ9VaAAvu-jw0k3XVPhbFBgCLcBGAs/s1600/srm726top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="94" data-original-width="629" src="https://3.bp.blogspot.com/-7ED5pgjKFxw/WkfjRHRAlGI/AAAAAAAB8Hw/BXZ5kLBwazcZ9VaAAvu-jw0k3XVPhbFBgCLcBGAs/s1600/srm726top5.png" /></a></div>TopCoder SRM 726 at roughly the same time attracted the second half of the community (<a href="https://community.topcoder.com/stat?c=round_overview&rd=17048">problems</a>, <a href="https://community.topcoder.com/stat?c=round_stats_sorted&rd=17048&dn=1&sq=Round_Statistics_Data&sc=16&sd=asc">results</a>, top 5 on the left, <a href="https://youtu.be/BG2tGOJmjO0">my screencast</a>). <a href="https://community.topcoder.com/stat?c=problem_statement&pm=14750&rd=17048&rm=330805&cr=10574855">The hard problem</a> has proved decisive: you are given 2000000 tasks, to be done in 2000 days, at most one task per day (so most of the tasks will end up not being done). For each task you know three numbers: the segment of days when it can be done (from <i>l<sub>i</sub></i> to <i>r<sub>i</sub></i>), and the profit <i>c<sub>i</sub></i> from doing it. You need to pick the task to be done in each day in such a way that the total profit is maximized. This is naturally reduced to a weighted maximum matching problem, but surely it will time out with 2 million vertices in the graph — or will it?<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-ZJZ3GwzJPMo/Wkflgqm7RhI/AAAAAAAB8H8/AMDXQa2ghuExhMCLI4xlblsy04TSrlCWACLcBGAs/s1600/cf454top5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="1114" height="156" src="https://1.bp.blogspot.com/-ZJZ3GwzJPMo/Wkflgqm7RhI/AAAAAAAB8H8/AMDXQa2ghuExhMCLI4xlblsy04TSrlCWACLcBGAs/s640/cf454top5.png" width="640" /></a></div>Codeforces ran another round, Round 454, on Saturday (<a href="http://codeforces.com/contest/906">problems</a>, <a href="http://codeforces.com/contest/906/standings">results</a>, top 5 on the left, <a href="http://codeforces.com/blog/entry/56601">analysis</a>). All problems were tractable this time for moejy0viiiiiv and Radewoosh, and in case of the former with 30 minutes to spare. Congratulations on the victory!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-O6RDnkfTK3Y/Wkfw8Ps4XpI/AAAAAAAB8IQ/CoX1NzlWcGoAueteM_XN-OUz8CLPq3wxACLcBGAs/s1600/IMG_20171216_120459.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="480" src="https://2.bp.blogspot.com/-O6RDnkfTK3Y/Wkfw8Ps4XpI/AAAAAAAB8IQ/CoX1NzlWcGoAueteM_XN-OUz8CLPq3wxACLcBGAs/s640/IMG_20171216_120459.jpg" width="640" /></a></div>In <a href="http://petr-mitrichev.blogspot.com/2017/12/a-newton-week.html">my previous summary</a>, I have mentioned an Open Cup problem: you are given an integer <i>n</i> with at most a million digits. You need to determine if it's a perfect square or not. You don't need to actually find the square root.<br /><br />As is often the case in problems with a Yes/No answer, it suffices to find an algorithm that always finds one of the two answers correctly, and the other with a certain probability that is greater than zero. We can then apply it repeatedly until the probability of not finding the correct answer is exponentiated to become essentially zero.<br /><br />Let's pick a prime number <i>p</i>. If n is a perfect square, then <i>n</i> mod <i>p</i> is always a quadratic residue. If not, then assuming <i>p</i> is picked randomly the probability of <i>n</i> mod <i>p</i> being a quadratic residue is roughly 0.5, since roughly half of all numbers mod <i>p</i> are quadratic residues (this is merely handwaving; can anyone prove this probability estimation formally?).<br /><br />So we can pick, say, 30 random prime numbers, and check if <i>n</i> is a quadratic residue modulo each of them. If at least one says no, then the overall answer is no, otherwise it's yes. Checking if a number is a quadratic residue modulo p is done by checking if <i>n</i><sup>(<i>p</i>-1)/2</sup>=1 (mod <i>p</i>).<br /><br />Thank for reading, and check back for this week's summary!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/zOEzhkZBcqU" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2017/12/a-quadratic-week.htmltag:blogger.com,1999:blog-1953325079793449971.post-52504790878233327282017-12-30T21:33:00.000+03:002017-12-30T21:33:24.868+03:00A Newton week<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-ntOrf0Qgp64/WkfZYL4kFKI/AAAAAAAB8HU/-3q0rDW5XswSl9Pi3X7okCh4xlrxBnkOwCLcBGAs/s1600/oc1718peterhoftop5.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="269" data-original-width="849" height="202" src="https://1.bp.blogspot.com/-ntOrf0Qgp64/WkfZYL4kFKI/AAAAAAAB8HU/-3q0rDW5XswSl9Pi3X7okCh4xlrxBnkOwCLcBGAs/s640/oc1718peterhoftop5.png" width="640" /></a></div>The Dec 11 - Dec 17 week contained the last Open Cup round of the year: the Grand Prix of Peterhof (<a href="http://opencup.ru/files/oci/gp9/problems1-e.pdf">problems</a>, <a href="http://opencup.ru/index.cgi?data=macros/stage&head=index&menu=index&stg=9&region=main&ncup=oci&class=oci">results</a>, top 5 on the left). The deciding problem H required the ability to find the exponent a formal series fast, and the teams that were able to do so claimed the first two places — congratulations to the Moscow IPT team and to SPb Havka-papstvo!<br /><br />I found the relatively easy problem J very nice. You are given an integer with at most a million digits. You need to determine if it's a perfect square or not. You don't need to actually find the square root. Can you see how to get this problem accepted on the 8th minute of the contest?<br /><br />Thanks for reading, and check back soon!</div><img src="http://feeds.feedburner.com/~r/PetrMitrichev/~4/zHAamKHEpS8" height="1" width="1" alt=""/>Petr Mitrichevhttps://plus.google.com/108329321411299197209noreply@blogger.com0http://petr-mitrichev.blogspot.com/2017/12/a-newton-week.html