tag:blogger.com,1999:blog-1523270329259575722017-07-23T01:18:05.113-07:00Siwakorn Srisakaokul (Ping)Computer Science, University of Illinois at Urbana-ChampaignSiwakorn Srisakaokulnoreply@blogger.comBlogger8125tag:blogger.com,1999:blog-152327032925957572.post-32742660543097544372014-06-16T10:21:00.002-07:002015-04-26T12:07:13.381-07:00Implemented Treap for the first time<p>As a way to solve <a href="http://codeforces.com/problemset/problem/436/E">Problem E</a> from ZeptoLab Code Rush 2014, I decided to implement <a href="http://en.wikipedia.org/wiki/Treap">Treap</a> for the first time. Treap is a randomized binary search tree that the maximum height of the tree is about <b>O(log n)</b> (similar to AVL tree). It's easier to implement than a normal AVL tree. Why do I have to implement it? I need to perform these operations in <b>O(log n)</b>: Add, Remove, Find the sum of the <b>k</b> smallest elements. My implementation was added <a href="https://github.com/ping128/cpt/blob/master/libraries/data_structure/Treap.cpp">here</a>. </p> <p>Note that this problem E can also be solved by other data structures: Binary Indexed Tree, Cartesian tree or Priority Queue, because this problem only requires calling this operation, "Find the sum of the <b>k</b> smallest elements", multiple times with the non-ascending value of <b>k</b>. </p>Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0tag:blogger.com,1999:blog-152327032925957572.post-44872760534788703172014-06-05T12:50:00.001-07:002014-06-07T08:20:38.622-07:00Topcoder | SRM 623<p>I will skip <b>Div1-300</b>, because it can be solvable after carefully thinking/coding. </p> <h4>Div1-450</h4></p>I would like to introduce a technique used to solve this problem. The technique is to apply a transformation (rotate the graph 45-degree clockwise) to the original graph. With the original graph, if we are at <b>(0, 0)</b> we can move to get any points that drop from the area above the blue lines. After applying the transformation, the claim stays true. So what is the maximum number of points we can get? Of course, we can only get the points that are in the 1<sup>st</sup>quadrant. <div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-BiONb6w7yeQ/U5FIO-UdeXI/AAAAAAAAAc4/zMN2os9k1S4/s1600/Untitled+Diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-BiONb6w7yeQ/U5FIO-UdeXI/AAAAAAAAAc4/zMN2os9k1S4/s640/Untitled+Diagram.png" /></a></div> <p>The transformation matrix is [[1, 1], [-1, 1]]. What can we do with this new graph? First, we can notice that if we are at <b>(x, y)</b>, the next fruit position that we can get to must be <b>(u, v)</b>, where <b>u >= x, v >= y</b>. This means an optimal sequence of pieces of fruits we get will be ascending by <b>x-coordinates</b>. We can think of this problem as a different problem that all the points stay still, but we are the one who moves right or up to collect the points. So let's sort all the points by their x-coordinate first. Then we name a multiset to store y-cooridnates and loop through all the points in the ascending order and do the following: <ul><li>If the point <b>p</b> is not in the 1<sup>st</sup>quadrant, we ignore (continue).</li><li>We put <b>p.y</b> (y-coordinate) into a multiset, then we remove the first element (if exists in the set) which is greater than <b>p.y</b>.</li><li>After finish looping, the size of this set will be the maximum number of pieces of fruit we can get.</li></ul> Why does this give us an optimal solution? Consider this example, suppose now our optimal sequence is (1, 1), (2, 3) (our multiset will be [1, 3]) and the next point is (4, 5). From (2, 3), we can get the fruit at (4, 5) next. So we can put this new point into our map. The other case is that the next point is (4, 2) (its y-coordinate is less than the current position's). If we are at (2, 3), we cannot get the fruit at (4, 2). We need to pick either (4, 2) or (2, 3), but we can't really tell which point we should go right now. What we know is that the number of piece of fruit will not increase. We choose to add 2 into our multiset and remove 3 because (4, 2) will allow more spaces we can go next. In other words, we pick (4, 2) as a 'shadow' of (2, 3). If sometimes later, we know that (2, 3) is not a good choice, this will be fine. At (4, 2), we can go to all the points we can go from (2, 3), so there is no harm to pick (4, 2) instead. But if (2, 3) is a good choice, we would already remove 3 (the y-coordinate of (2, 3)) from our set which is what we expect. It can be more clear if you draw some pictures of both cases. </p><p><b>Here are the problems: </b><a href="http://community.topcoder.com/stat?c=round_overview&er=5&rd=15856">SRM 623 | Problems</a></p>Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0tag:blogger.com,1999:blog-152327032925957572.post-32811333930123304372014-06-03T21:18:00.001-07:002014-06-03T21:18:35.859-07:00Heavy Light Decomposition technique<p>I have heard about this technique so many times recently, but I haven't gotten a chance to use it during a programming contest. I think this technique is very interesting and pretty cool, so I decide to implement it to solve a problem, <a href="http://www.spoj.com/problems/QTREE/">QTREE</a>. The problem is that you're given a tree of <b>N</b> nodes and a number of operations. Each query can be one of the following: <ol><li><b>CHANGE a b</b>: update the cost of the <b>a<sup>th</sup></b> edge to <b>b</b></li><li><b>QUERY a b</b>: print the maximum edge cost on the path from <b>a</b> to <b>b</b></li></ol>So you're asked to perform these operations. This problem would be very easy to solve if the graph is just a chain (not a tree). In that case, you can easily solve it by using a segment tree that can perform each operation in <b>O(log N)</b>. I claim that this problem can also be solved by segment trees. This is how Heavy Light Decomposition (HLD) technique comes into play. Before I talk about HLD, I will explain how HLD can solve this problem. Even though the given graph is a tree, we still decompose it into several chains. If we can do this, we can perform each operation on separate chains and combine the results together. We already know that performing an operation on a chain takes <b>O(log N)</b> by using a segment tree, so we need to decompose the tree into some number of chains. And HLD can do this and guarantee that the number of chains is <b>O(log N)</b>. So it will take us <b>O(log N)<sup>2</sup></b> to perform each operation which is fast enough to solve this problem. A tree after applied HLD will look like this: </p><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-7LvQewz70Tw/U46YTa8TDGI/AAAAAAAAAcI/_3zRR0s4LcQ/s1600/Untitled+Diagram-4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-7LvQewz70Tw/U46YTa8TDGI/AAAAAAAAAcI/_3zRR0s4LcQ/s400/Untitled+Diagram-4.png" /></a></div><p>The colors indicate different chains and the numbers in the nodes indicate the chain heads. I won't talk about the implementation detail (I will post my code I wrote to solve this QTREE problem on the bottom. The idea is that we do one Depth-First search to gather information (subtree's size, each node's parent), build HLD, then build a segment tree over all the chains. So if we want to answer <b>QUERY u v</b>, we can just first find <a href="http://en.wikipedia.org/wiki/Lowest_common_ancestor">the lowest common ancestor</a> between <b>u</b> and <b>v</b> which is <b>node 1</b>, then we find the maximum edge cost on the path from <b>u</b> to <b>node 1</b> and the maximum edge cost on the path from <b>v</b> to <b>node 1</b>. To find the maximum edge cost on each path, we can just crawl up the chains. For example, we can crawl up from node <b>u</b> to <b>node 1</b> by visiting the pink chain, red chain, and green chain. Upon visiting each chain, we can ask our segment tree what the maximum edge cost between me and the head of this chain. We take the maximum among those and that will give us the answer. This is a basic application of HLD. You can imagine that we can do any kinds of operations we can do on a chain. We can just make a different functionality of a segment tree to handle those operation. HLD is just a technique to decompose a tree into chains. HLD should be able to tell us how to crawl up the tree chain-by-chain as well. </p> <p>There is a similar technique to HLD. For example, we can divide an array into <b>O(√N)</b> parts. Each part contains <b>O(√N)</b> elements. When we're asked to perform some operation on the array, we can just do it separately on each part (which should be easier somehow depending on a problem). We might be able to reduce time to perform each operation from <b>O(N)</b> to <b>O(√N)</b>. </p> <p>Here is the <a href="http://ping128.blogspot.com/p/qtree-solution.html">link</a> to my code for QTREE</p> Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com1tag:blogger.com,1999:blog-152327032925957572.post-23260276130418539792014-06-02T11:00:00.000-07:002014-06-03T21:14:45.559-07:00Codeforces Round #250 | Div. 1 <h4>Problem A</h4><p>I will just give a hint for A. The idea is that disconnecting the graph requires cutting all the edges. Cutting an edge <b>e(v1, v2)</b> can cost either <b>v1.value</b> or <b>v2.value</b>. Of course, we want the cost to be the lower one, and we can always force it by some cutting sequence. </p> <h4>Problem B</h4><p>This problem is very interesting for me. It took me a large amount of time in order to solve it. So suppose that <b>x</b> is the minimum number of animals among the routes areas from <b>p</b> to <b>q</b>. If we set the cost of each edge to be the minimum value between its ends, <b>x</b> will be equal to the minimum edges' cost along the path. <b>f(p, q)</b> is the maximum value of x's among all simple routes between <b>p</b> and <b>q</b>. It looks really hard if we consider all the routes. We have to calculate the sum value of <b>f(p, q)</b> for all pairs <b>p, q (p ≠ q)</b>. But actually this sum is equal to <b>c(e<sub>1</sub>) + c(e<sub>2</sub>) + ... + c(e<sub>m</sub>)</b> where <b>c(e<sub>i</sub>)</b> is <b>e<sub>i</sub>.cost</b> ✕ the number of all pairs <b>p, q</b> whose <b>f(p, q)</b> is <b>e<sub>i</sub>.cost</b>. To find <b>c(e<sub>i</sub>)</b>, we need to start with the largest edge. If <b>e(u, v)</b> is the largest edge, <b>f(u, v)</b> is surely equal to <b>e.cost</b>. Then we put <b>u, v</b> into the same set. Then move to the second largest edge and so on. Notice that for each edge <b>e(u, v)</b>, <b>f(p, q) = e.cost</b> where <b>p ∈ the set of u and q ∈ the set of v</b>, so <b>c(e) = e.cost ✕ |u| ✕ |v|</b>. Then we union <b>u and v</b>. Doing this to all the edges in descending order of their weights will give you the sum value of <b>f(p, q)</b> for all pairs <b>p, q (p ≠ q)</b>. And the best data structure that can do this very efficiently is <a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoin Set</a>. </p> <h4>Problem C</h4><p>One can easily realize that this problem is a dp problem. The question is: what is the best way to do dp? Well let <b>dp[i][j]</b> = the number of ways to split the polygon <b>p[i], p[i + 1], ... ,p[j]</b> into non-degenerate triangles. Note that <b>i</b> can be greater than <b>j</b> and that will represent a polygon <b>p[i], p[i + 1], ..., p[n], p[1], p[2], ..., p[j]</b>. How can we calculate <b>dp[i][j]</b>? We can try splitting the <b>polygon(i, j)</b> by a non-degenerate <b>triangle(i, k, j)</b> where <b>k</b> is between <b>i</b> and <b>j</b>. We need to check that <b>triangle(i, k, j)</b> doesn't cross the entire polygon. So <b>dp[i][j]</b> will be the sum of <b>dp[i][k] ✕ dp[k][j]</b> for all valid <b>triangles(i, k, j)</b>. By doing this, we don't have to encounter double-counting problem, because we split the polygon by different triangles(i, k, j). </p> <p> <h4>Problem D</h4><p>This problem is basically a classic problem that can be solved by Segment tree if we don't have the second type of operation. Let's think about the second type of operation (mod). Does it really increase the difficulty of this problem? If we handle the second operation by simply mod-ing down the tree, we can stop when the maximum value of all the values in a subtree is less than the mod value. If <b>x < mod</b>, <b>x % mod = x</b>. Another fact is that mod-ing significantly decrease a value. Even if we mod everything down the tree, their values will become less than some mod value pretty fast. By these two facts, we can just solve this problem straightforwardly by using the normal segment tree. Each tree node keeps two values: <b>sum</b> (the sum of this subtree) and <b>max</b> (the maximum over all the leaves of this subtree). </p> <hr/></p><b>Problems:</b><a href="http://codeforces.com/contest/438"> Codeforces Round #250 | Div. 1 | Problems</a></br></p><p>Lesson learnt: <ul><li>Checking that a line connecting between two corners doesn't cross the entire polygon and is inside the polygon can be easily done by checking if the sum of the areas' parts separated by this line is still equal to the area of the entire polygon</li><li>Getting a polygon(start, end) can be handled by the following code (this is a nice way to deal with a circular array): <pre class="brush: js"><br />vector<Point> v;<br />for (int i = start; i != end; i = (i + 1) % N) {<br /> v.push_back(poly[i]);<br />}<br />v.push_back(end);<br /></pre></li><li>When you find that you're stuck in a thought process or you keep repeatedly thinking about the same idea, you should stop thinking, step back. You might want to re-read the problem statement again, think slowly about the big idea to solve the problem, and consider the simplest case. By doing this, you might end up with a new working solution. I noticed that sometimes I can fall into my own thought process and can be misled by something that won't ever direct me to the intended approach to solve a problem. Also, it's worth taking a short 10-second break. </li></ul></p>Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0tag:blogger.com,1999:blog-152327032925957572.post-18334523592135114952014-06-01T20:32:00.000-07:002014-06-03T00:13:43.167-07:00GCJ 2014 Round 2 Follow-up<p>Yesterday I left the post with 2 GCJ14 Round 2 unsolved problems. Here are the solutions to those problems </p> <p><b>Problem C: Don't Break The Nile: </b>as I said, we can use Maximum flow algorithm to solve this problem, but actually we don't have to run the full straightforward MaxFlow algorithm. We can apply the idea that <b>MaxFlow = Min-cut</b> to solve this problem. A way that we can find the min-cut to the graph is to draw the min-cut curve going from the west to the east side. Consider the following picture (from GCJ2014 sample test): </p> <div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-nSNtabslqnU/U4v9nGYT-4I/AAAAAAAAAbU/mZsrquzb2Jw/s1600/gcj2014-r2-C.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-nSNtabslqnU/U4v9nGYT-4I/AAAAAAAAAbU/mZsrquzb2Jw/s320/gcj2014-r2-C.png" /></a></div><p>The purple line is a minimum cut on this graph (the network flow graph is not shown). Basically, we try to make a new graph such that all builds are represented as vertices, and there are 2 more vertices for the west and east sides. We added an edge between each pair of vertices (For simplicity, some edges are not shown in the picture). The weight of each edge is the distance between two buildings. In analogy, the cut's cost between two buildings is the amount of water that can go through the gaps between the buildings. So let's think a bit of how we can find the minimum cut? --------- Look like a shortest path problem, right? and yes, the cost of the minimum cut is just the shortest path from the west to the east side. That's it. The size of the entire grid doesn't matter here. I really like this problem! </p> <p>About implementation, finding the distance between two buildings are somewhat tricky though. My method is like there are four points on each rectangle. Let's take the minimum distance between the 16 pairs of points between the two buildings (The distance between two points here is <b>max(abs(p1.x - p2.x), abs(p1.y - p2.y)) - 1</b>). This is not all. We have to consider 4 more cases where two buildings are located side-by-side :) </p> <p>Now move to the hardest problem: <b>Problem D. Trie Sharding</b>. There are two parts of this problem: 1) Find the maximum total number of nodes of all possible group arrangements. 2) How many group arrangements that results in that maximum number of nodes. To answer the first question, let's build a trie of all the strings. Consider the following picture for the first sample input ("AAA", "AAB", "AB", "B"): </p><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-4vdQ-BC14nE/U4vpPnTcvSI/AAAAAAAAAbI/EwiAPsdwB1c/s1600/gcj2014-r2-D-3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-4vdQ-BC14nE/U4vpPnTcvSI/AAAAAAAAAbI/EwiAPsdwB1c/s640/gcj2014-r2-D-3.png" /></a></div> <p>The original trie can be composed of these two tries ({"AAA", "B"}, {"AAB", "AB"}). Considering the original trie, the second number on each node indicates how many times this node can appear on different tries (Of course, the number can't be greater than <b>N</b>). The way that we can calculate all these numbers is to starting from leaves to the root. We can be sure that each node that represents the end of a string can appear only once on a trie, so we can set the second number to 1. For the other nodes, their second number are the sum of the second numbers of all of their children. If the sum is greater than <b>N</b>, set it to <b>N</b>. Finally, the sum of all the second numbers are the total number of the nodes. Answering the second question is harder. In different interpretation, the second number is actually the number of trie subtrees rooted at each node. We claim that that the number of group arrangements is the multiplication of the number of ways to make each subtree. Consider the root subtree as an example. There are 2 subtrees from the first child and 1 subtree from the second child. We have to count the number of ways to combine these 3 subtrees into 2 tries. So the subproblem is that given a list of numbers <b>x</b>(#subtrees on each child), and a number <b>kk</b> = min(N, total #subtree), count how many ways we can combine all the subtrees into <b>kk</b> tries (trees). The order doesn't matter and two subtrees on the same child can't be in the same resulting subtree. </p> <p>For example, <b>x = {2, 1}, kk = 2</b>, there are 2 ways: ({X, Y}, {X}) and ({X}, {X, Y}), X is a subtree from the first child and Y is a subtree from the second child. Notice that two x's need to be in the different trees. </p> <p>This can be solved by Dynamic Programming: </p> <pre class="brush: js"><br />long long count(vector<int>&x, int kk) {<br /> // dp[i] = the number of ways to combine x into i tries<br /> // C[i][j] = (i choose j)<br /> int sz = x.size();<br /> for (int i = 1; i <= kk; i++) {<br /> dp[i] = 1;<br /> for (int j = 0; j < sz; j++)<br /> dp[i] = (dp[i] * C[i][x[j]]) % MOD; // there are i tries, choose x[j] tries for x[j] subtrees <br /> for (int j = 1; j < i; j++)<br /> // we subtract dp[i] by the number of ways that we don't use all i tries.<br /> dp[i] = (((dp[i] - dp[j] * C[i][j]) % MOD) + MOD) % MOD;<br /> }<br /> return dp[kk];<br />}<br /></pre> The end!!!Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0tag:blogger.com,1999:blog-152327032925957572.post-42921843553777721762014-05-31T12:05:00.000-07:002014-06-01T23:19:09.230-07:00Google Code Jam 2014 | Round 2<p>Today <a href="https://code.google.com/codejam/">GCJ</a> 2014 Round 2 contains 4 problems. The top 500 contestants will advance to Round 3. In this round, at least 50 points with small penalty time are required to pass this round. And yeah! I got 413<sup>rd</sup> ranking. This was my first time that I got a GCJ t-shirt and advanced to Round 3. </p> <p>let's talk about the problems. The first problem <b>(A: Data Packing)</b>: given <b>N</b> files with their capacities and disc capacity, what is the minimum number of discs are required to store all the files with 2 conditions: <ol> <li>A disc can contain only at most 2 files.</li> <li>Each file can't be divided to stored separately in different discs.</li></ol>This problem was the easiest one of this round. An <b>O(nlogn)</b> solution can get accepted. My greedy solution is to loop from the smallest file <b>a</b> and for each of them, find the biggest file that can be put into a disc along with this file <b>a</b>. If we can't find one, just put <b>a</b> into a disc. We repeat until no files left. A nice data structure for doing this is multiset (C++), because we can call lower_bound() and there might be duplicate file capacities. </p> <p>The second problem <b>(B: Up and Down)</b>: give a list of integers, we would like to rearrange the sequence to an up and down sequence (one where <b>A<sub>1</sub> < A<sub>2</sub> < A<sub>3</sub> <...< A<sub>m</sub> > A<sub>m-1</sub> >...> A<sub>n</sub></b> for some index m). We can rearrange them by swapping two adjacent elements of the sequence. The problem asked you to find the minimum number of swaps needed to accomplish this. First my first attempt to solve this problem was to find where we should place <b>A<sub>m</sub></b> (it's obvious that <b>A<sub>m</sub></b> is the largest element of the sequence). There are <b>n</b> possible positions. We can try moving <b>A<sub>m</sub></b> to each position, then find the number of inversions of the left part and right part of the sequence. This idea was completely wrong. We can't prove anything that doing this will give us the minimum number of swaps. After thinking for a while, I came up with a solution which is starting from the smallest element first. We know that the smallest element need to be on either leftmost end or rightmost end. We can just pick the one end that is closest to the smallest element's position (Whether moving it to the left or right end doesn't affect the remaining sequence. So it's better to move it to the closest end). That's it! we do the same thing to the other elements. </p> <p>After finishing the first two problems, I only had 1 hour left. So I changed my strategy to just solving the small inputs on the last two problems. I will give brief ideas of how to solve the small inputs on the last two problems. The ideas are quite straight-forward to me. For <b>C: Don't Break The Nile</b> , we want to find the maximum flow of water from the south side to the north side of the river. Each grid cell can carry only 1 unit of water to its adjacent cells. Thus, let's build a network flow graph. <ol> <li>For each cell, we create 2 nodes called lower node and higher node, and have an edge with capacity = 1 going from the lower to the higher node. If the cell contains a building, set the edge's capacity to 0.</li> <li>Now create edges between the adjacent cells: connect the higher node of a cell to the lower node of each adjacent cell with an edge of capacity 1.</li> <li>Make a new node called <b>source</b> and connect it to all the lower nodes of the south side cells.</li> <li>Make a new node called <b>sink</b> and connect it to all the higher nodes of the north side cells.</li></ol>That's all. Then the answer to the problem is the maximum flow on this graph. This method, of course, is too slow to pass the large input set that the height of the river can be up to <b>10e<sup>8</sup></b>. </p> <p>Now, come to the last problem <b>(D: Trie Sharding)</b>. Given <b>M</b> strings, we would like to divide it into <b>N</b> smaller groups and make a <a href="http://en.wikipedia.org/wiki/Trie">trie</a> data structure for each group so that the total number of nodes used to make tries is maximized. So we want to find the maximum number of nodes of all possible group arrangements. For the small dataset, <b>M = 8, N = 4</b>, we can just bruteforce all the possible ways to make groups, make tries for each of them. We finally return the smallest number of nodes of all the possible ways of dividing up the strings into groups. This is all about implementation. For the large dataset <b>M = 1000, N = 100</b>, I still have no idea how to solve it. </p> <p>This post is a bit long, but that's it for GCJ2014 Round 2. I really like the problems which I can categorize them to be in a hard level for me, but improving requires solving hard problems :). I'm planning to join Round 3 as well, but before that we should read the analysis of this round and try coding it up! </p><b>Problems:</b><a href="https://code.google.com/codejam/contest/3014486/dashboard"> Code Jame 2014 | Round 2 | Problems</a></br><b>Scoreboard:</b><a href="https://code.google.com/codejam/contest/3014486/scoreboard"> Code Jame 2014 | Round 2 | Scoreboard</a></p> <hr><p>Now that, I'm starting to write blogs seriously in order to summarize what I learn from programming competition. I think it's better to rethink about problems and how well I do during each contest. This can help me prevent repetition of the same mistake. Writing can make ideas solid so that when I see the similar problem again, I can pick up the idea pretty fast. Furthermore, it might be able to guide others about the ideas to solve problems. Thanks to this <a href="http://blog.anudeep2011.com/">blog</a> which motivated me to start writing blogs again. </p>Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com1tag:blogger.com,1999:blog-152327032925957572.post-35357435775317981092013-07-21T16:30:00.000-07:002014-06-01T23:20:24.430-07:00Manacher's algorithmToday I just learned a new amazing algorithm. It's called Manacher's algorithm, the algorithm that finds the longest palindromic substring in linear time. Previously, I never thought that it can be done faster than O(N^2) time complexity (by dynamic programming). I felt really impressive after reading this blog and knew that this algorithm was found in 1975 :) So I wanna share it.<br /><br />Link: <a href="http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html">Manacher's algorithm</a>Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0tag:blogger.com,1999:blog-152327032925957572.post-74028453226463630672013-06-23T23:13:00.000-07:002014-05-31T11:51:42.478-07:00Right Under My NoseThis morning, I participated in Codeforces Round #189. It's an online programming contest for Division 1 users as usual. This contest seems crucial for me, because if I didn't do well enough, my rating would decrease and it can cause me to fall into Division 2 which I really didn't want it obviously. I'd been going back and forth between Division 1 and 2 for a while until I got improved and my skill now is just over Division 2. So I've managed to stay in Division 1 for quite a long time (I guess) and I'm trying to turn to Yellow Rating which is the next step.<br /><br />Even though I've been participating in Division 1 many times, I still ephemerally got nervous because of all the pressure from competition and the fact that I can't miss it. So my plan, the same as every other times, is to start solving problem A, B, then C. There are 5 problems, but the Problem D, E are often too hard for me. Being able to solve 2-3 problems in this level of difficulty within 2-hour contest is an achivement ...well at least in my skill level right now. After the time counted down to 00.00, the competition started. I immediately read Problem A. The problem wasn't too long. I understood it in just a moment. The problem seemed a little complicated. It had to deal with XOR operation. I knew that I could solve it, so I started thinking about the possible solution. It took me a while to think, code, check for bugs and submit it. During that time, I got circuitously nervous again, because I might spent too much time to solve A and the longer time I spent, the less score I got. I was always careful with Problem A and wanted to submit it only one time and get accepted to avoid the penalty point for a wrong submission. At around 40 minutes of the contest, I submitted it and it passed the pretests. I knew that solving A in 40 minutes wasn't good enough, but if I could solve B and/or C. My rank will be good enough. So let's continue to solve B.<br /><br />Problem B was very short and easy to understand, but it's hard and sort of an ad-hoc problem. I spent 30 minutes to code the algorithm that I thought it's correct. Unfortunately my algorithm didn't work for some special cases. So it's time for me to decide whether I should continue with this problem or read Problem C. I looked at the scoreboard, and some people did C instead of B. It might be somehow easier, so I read C. Problem C statement is a little confusing, so I stopped reading it and went back to do B. I thought that it's better to fix the bug in B than start doing a new problem (considering about the time I have left). I have a bad habit that if I can't solve an easier problem, I don't want to go and solve the harder one. But it's real misthought here. A problem that is supposed to be harder in general might be easier for me...who knows?...especially in the competition like this.<br /><br />Finally the time's up. I didn't even solve B in time. Thus, my rating is doubtlessly down, but fortunately I'm still in Division 1. After that I continued to code B and got it accepted. I read problem C and I suddenly felt really sad. After finishing reading it, I knew how to solve it for sure. It needed Convex Hull Trick idea to solve and I just learned it. I was depressed, because I didn't finish reading it during the contest, otherwise I would be able to solve it! This is one of the mistakes that I need not to forget that I should read new problems when I'm stuck. Well, as having been said, I will try to do better next time! I won't hail this mistake again.Siwakorn Srisakaokulhttps://plus.google.com/114539611090066236237noreply@blogger.com0