choice's blog

By choice, history, 8 years ago, In English

I found the following derivation using Lagrange multipliers for the solution of the Core Training problem in Code Jam this year. I liked this approach particularly because I don't get to use Lagrange multipliers very often in programming competitions.

Let denote the initial vector of success probabilities that we are given, and let be an arbitrary vector of success probabilities. Let denote the probability of exactly i cores succeeding given the success probabilities in . Then we are trying to maximise

subject to

By using the Lagrangian method, we get that an optimal solution must satisfy

or it must be on the boundary (but we'll get to that later).

Lemma

where is the vector of probabilities with pi removed, i.e. (p1, p2, ..., pi - 1, pi + 1, ..., pn). That is, the partial derivative of f with respect to pi is the probability that exactly K - 1 of the cores succeed, not including core i itself.

Proof of Lemma

This is a somewhat well-known derivation, but I have included it for completeness. By expanding the terms,

which simplifies to

(End proof.)

In order to have , then the most obvious thing to do is to set all the pi equal to each other. However this is not possible, since we cannot reduce pi below p0i. By taking into account boundary conditions, this then gives us three choices for each core i:

  1. We leave pi at its initial value p0i.
  2. We increase pi to 1.
  3. We try to equalise pi with the some other {pj} that are not on the boundary.

This observation is sufficient to restrict the solution space to the point where we can search over it. (Well, we still need to make some efficiency arguments, like "only increase pi up to 1 if it is already big", and "only try to equalize pi's that are close to each other", etc..., but you get the idea.)

Full text and comments »

  • Vote: I like it
  • +111
  • Vote: I do not like it

By choice, 10 years ago, In English

Hi everyone, I've written the following Fenwick tree tutorial because I've found many other tutorials to be unintuitive. Most notably, they don't seem to describe the intersecting chain property, which I think is a useful way of seeing why Fenwick trees work. Comments and/or critiques for this tutorial are appreciated.

Link here: http://math-porn.tumblr.com/post/93129714459/range-queries-and-fenwick-trees

Full text and comments »

  • Vote: I like it
  • +70
  • Vote: I do not like it

By choice, 11 years ago, In English

Whenever your rating on Codeforces increases, they alert you with a message saying "You have Wow! You have +XX!". (See top right corner.)

(Yes, I know I forgot to change the +8 contribution to -100.)

Full text and comments »

  • Vote: I like it
  • +71
  • Vote: I do not like it

By choice, 14 years ago, In English

Problem A - IQ Test

We can store two values, countodd and counteven, as the number of odd or even elements in the series. We can also store lastodd and lasteven as the index of the last odd/even item encountered. If only one odd number appears --- output lastodd; otherwise only one even number appears, so output lasteven.


Problem B - Telephone Numbers

There are many ways of separating the string into clusters of 2 or 3 characters. One easy way is to output 2 characters at a time, until you have only 2 or 3 characters remaining. Here is a possible C++ solution:

<code>
for( i=0; i<n; i++ )
{
    putchar(buf[i]);
    if( i%2 && i<n-(n%2)-2 ) putchar('-');
}
</code>


Problem C - Roads in Berland

If you are familiar with the Floyd-Warshall algorithm, then this solution may be easier to see.

Initially, we are given a matrix D, where D[i][j] is the distance of shortest path between city i and city j. Suppose we build a new road between a and b with length shorter than D[a][b]. How do we update the rest of the graph accordingly?

Define a new matrix D', whose entries D'[i][j] are the minimum path distance between i and j while taking into account the new road ab. There are three possibilities for each i, j:
  • D'[i][j] remains unchanged by the new road. In this case D'[i][j] = D[i][j]
  • D'[i][j] is shorter if we use the new road ab. This means that the new path i, v1, v2, ..., vn, j must include the road a, b. If we connect the vertices i, a, b, j together in a path, then our new distance will be D[i][a] + length(ab) + D[b][j].
  • Lastly, we may have to use the road ba. (Note that this may not be the same as road ab.) In this case, we have D'[i][j] = D[i][b] + length(ab) + D[a][j].
Thus, for each new road that we build, we must update each path i, j within the graph. Then we must sum shortest distances between cities. Updating the matrix and summing the total distance are both O(N2), so about 3002 operations. Lastly, there are at most 300 roads, so in total there are about 3003 operations.

One thing to note is that the sum of all shortest distances between cities may be larger than an int; thus, we need to use a long when calculating the sum.


Problem D - Roads not only in Berland

Before we start this problem, it is helpful to know about the union find data structure. The main idea is this: given some elements x1, x2, x3, ..., xn that are partitioned in some way, we want to be able to do the following:
  • merge any two sets together quickly
  • find the parent set of any xi
This is a general data structure that sometimes appears in programming competitions. There are a lot of ways to implement it; one good example is written by Bruce Merry (aka BMerry) here.

Back to the problem: Every day we are allowed to build exactly 1 road, and close exactly 1 road. Thus, we can break the problem into two parts:
  • How do we connect the parts of the graph that are disconnected?
  • How do we remove roads in a way that does not disconnect parts of the graph?
Let build be the list all roads that need to be built, and let close be the list of nodes that need to be closed. We can show that in fact, these lists are of the same size. This is because the connected graph with n nodes is a tree if and only if it has n - 1 edges. Thus, if we remove more roads than than we build, then the graph is disconnected. Also, if we build more roads than we remove, then we have some unnecessary roads (the graph is no longer a tree).

Now consider the format of the input data:
a1, b1
a2, b2
...
an - 1, bn - 1
We can show that edge (ai, bi) is unnecessary if and only if the nodes ai, bi have already been connected by edges (a1, b1), (a2, b2), ..., (ai - 1, bi - 1). In other words, if the vertices ai, bi are in the same connected component before we, add (ai, bi) then we do not need to add (ai, bi). We can use union-find to help us solve this problem:

<code>
for( i from 1 to n-1 )
{
    if( find(ai)=find(bi) ) close.add(ai, bi);
    else merge(ai, bi);
}
</code>

In other words, we treat each connected component as a set. Union find allows us to find the connected component for each node. If the two connected components are the same, then our new edge is unnecessary. If they are different, then we can merge them together (with union find). This allows us to find the edges that we can remove.

In order to find the edges that we need to add to the graph, we can also use union-find: whenever we find a component that is disconnected from component 1, then we just add an edge between them.

<code>
for( i from 2 to n )
    if( find(vi)!=find(v1) )
    {
        then merge(v1, vi);
        build.add(v1, vi);
    }
</code>

We just need to store the lists of roads that are unnecessary, and the roads that need to be built.


Problem E - Test

The way I solved this problem is with a hash function. Hash functions can fail on certain cases, so in fact, my solution is not 'correct'. However, it passed all the test cases =P

Let the input strings be s0, s1, s2. We can build the shortest solution by permuting the strings and then trying to 'attach' them to each other. I.e., we need to find the longest overlapping segments at the end of string a and the beginning of string b. The obvious brute force solution won't run in time. However, we can use a hash function to help us calculate the result in O(n) time, where n is min(len(a), len(b)). The hash function that I used was the polynomial hash(x0, x1, ..., xn) = x0 + ax1 + a2x2 + ... + anxn. This polynomial is a good hash function in this problem because it has the following useful property:
Given hash(xi, ..., xj), we can calculate the following values in O(1) time:
  • hash(xi - 1, xi, ..., xj) = xi - 1 + a × hash(xi, ..., xj)
  • hash(xi, ..., xj, xj + 1) = hash(xi, ..., xj) + aj + 1 - i × xj + 1
In other words, if we know the hash for some subsequence, we can calculate the hash for the subsequence and the previous element, or the subsequence and the next element. Given two strings a, b, we can calculate the hash functions starting from the end of a and starting from the beginning of b. If they are equal for length n, then that means that (maybe) a and b overlap by n characters.

Thus, we can try every permutation of s0, s1, s2, and try appending the strings to each other. There is one last case: if si is a substring of sj for some i ≠ j, then we can just ignore si. We can use hash functions to check that one string is contained within another one.

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it