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].
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
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?
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 =PLet 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
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.
I don't quite understand the problem statement in problem A. Or maybe I don't understand the editorial. What about test cases like 5 1 5 7 9 11 which should have the output as 1 but the AC solution gives 5. Also what about test cases like 5 5 9 13 15 21 which should give the output as 4 but gives 5. Please help me understand the problem. I have spent quite a lot time in this.
Yeah, I also Don't understand the problem statement in problem A. Also I Don't understand the editorial of A.. Pls help..
Russian editorial is quite clear: there's only 1 odd or even number (1 even and e.g. 10 odd; 1 odd and e.g. 2 even). You have to find pos of it. My solution pass all tests: 50928772
Yeah, I understand the problem, and got AC.. But I think the statement was quite unclear..
And Thanks a lot.. #MrReDox
https://codeforces.me/contest/25/submission/58229465
Task D. You can also use bfs for each connectivity component and find "useless" edges for each component. Now we have number of edges and number of components(they are equal). Now iterate for(int i = 1; i < components_count; ++i) build_road(component_vertex[0], component_vertex[i]); Where component_vertex is one of the vertexes for each connectivity component. Time complexity O(N). UPD: (O(V+E), but E = n — 1 and V = n, so O(V+E) = O(N + N — 1) = O(N))
Can we use Z algorithm in E ? Cause I got TLE on test 24[submission:58414744]
Soltuion for D. Just find self loops,multiple edges and cycles and remove all those edges. 89373449
Can you please tell me where my code is wrong
Solution link : https://codeforces.me/contest/25/submission/101586035
For the first question (25A), I thought of writing a hint since the language initially confused not just me but some of my friends too until we came to a conclusion of what exactly the question wanted to ask.
Evenness of a number is basically telling if the number is ODD or EVEN.
Hope this helps those who are confused with the language! Thanks for giving this a read!
nice
l