Procon 2015 Editorials

Revision en5, by ashish1610, 2015-08-08 18:57:06

Little Red Riding Hood and Candy Factory

The problem basically asks to check if the integer is even or odd. As integer is very large you can check if last digit is even or odd to decide the ans. If the integer is even than the ans is YES else ans is NO

Little Red Riding Hood and Convolutions


Convolution(L) = L1*L2*L3*L4 + L2*L3*L4*L5 + ........
Since only allowed values for L[i] is 1 or -1, we can say that the number of terms (Li*Lj*Lk*Ll) which evaluates to -1 are equal to number of terms which evaluates to 1. So, if we negate the given list the number of terms which evaluates to -1 or 1 doesn't change and we get another list L' whose Convolution(L') = 0

An Unfair Fair Game


Greedy, O(k):
Lets start with simple greedy solution. Given n, subtract k, k-1, k-2 from n till the residue of number is between 0 to k. If the residue is non-zero, add the remaining number to the answer set, and report (k — the cardinality of the answer set).
Optimizing greedy to just count the cardinality, O(log(k)):
Observe that the numbers we are subtracting form a simple AP, {k, k-1, …, k-x}. The sum of this is S(x): k*(k+1)/2 — (k-x-1)*(k-x)/2. This is a strictly decreasing function in the range x = [1, k]. We can do binary search to find the first non-negative value of S(x). Let this value occur at x = z, answer is (k — (z + (s(z) == 0))).
Expected complexity : O(log(k))

Graph Sum


Editorial will be published later

Code Land


The problem statement reduces to this:
Given an undirected weighted graph and information of weights of every edge. Remove one bridge and add another such that the weight of the graph is maximized.
Lets see what the brute force solution for this will be. Lets try adding every edge and removing minimum weight bridge corresponding to that edge. The complexity of this solution will be O(n3).
Lets see how we can optimize this code.
Claim 1: Every bridge of the graph will be in ST.
Proof : Let say tree T is a ST of graph but there is an edge e (x, y) which is not in this ST. Now, as e is not in ST than there is no way to go from x to y (By definition of bridge. The bridge is the only way to reach y from x). That means ST is not connected and hence is not ST, Therefore every bridge of the graph has to be in the ST.
Now, we have a ST which contains all the nodes and all the bridges. Lets find the minimum weight bridge between every pair of nodes (n1, n2). Since, now we have a tree there is only one path from any one node to another, we can find this minimum weight bridge between every pair of nodes by doing n dfs.
Now, lets try out our original solution. Since, now we know the minimum weight bridge for every pair of node we can do the same solution in O(n2).
Final running time : O(n2)

Range Query


To solve this problem we will use MO's algorithm. If we have sorted our queries with MO's. We can than use range query and range update data structures to solve the problem. For any add or remove we need to update [A[i] — k, A[i] + K] and correspondingly we will add and subtract cnt of range [A[i] — k, A[i] + k] from our ans.
We need to use BIT rather than Segment tree (I tried with both and there was a huge difference between time taken to generate ans by both codes. I think the reason for this was recursion but not sure. If anyone know the exact reason please comment.).
Expected complexity : O(n*sqrt(n)*log(n))
Sources for MO's algorithm : Tutorial

If you find any issues with the editorials let us know. Thanks for participating. :)

Tags esya, prosort, iiitd, editorials

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English ashish1610 2015-08-08 21:55:19 122
en6 English ashish1610 2015-08-08 21:39:25 767
en5 English ashish1610 2015-08-08 18:57:06 2
en4 English ashish1610 2015-08-08 18:56:35 37 Tiny change: ') == 0))).\nGrap' -brh3
en3 English ashish1610 2015-08-08 18:51:23 92
en2 English ashish1610 2015-08-08 18:49:01 2688 (published)
en1 English ashish1610 2015-08-08 15:43:49 1307 Initial revision (saved to drafts)