### A. Letter

To find the smallest rectangle containing the picture, iterate through the pairs (i,j) such that the j-th symbol in i-th line is '*'; find the minimum and maximum values of i and j from these pairs. The rectangle to output is [*i*

_{min},

*i*

_{max}] × [

*j*

_{min},

*j*

_{max}].

### B. Young Photographer

First we find the intersection of all segments. To do this, denote by m the rightmost of left ends of the segments and denote by M the leftmost of right ends of the segments. The intersection of the segments is [m,M] (or empty set if m>M). Now determine the nearest point from this segment. If*x*

_{0}<

*m*, it's

*m*, and the answer is

*m*-

*x*

_{0}. If

*x*

_{0}>

*M*, it's

*M*, and the answer is

*x*

_{0}-

*M*. If , it's

*x*

_{0}, and the answer is 0. If m>M, then the answer is -1.

### C. Four Segments

There must be many ways to solve this problem. The following one seems quite easy to code.First count the number of distinct points among segments' ends. If it's not equal to 4, the segments can't form a rectangle and we output "NO". Then calculate the minimum and maximum coordinates of the 4 points:

*x*

_{min},

*x*

_{max},

*y*

_{min},

*y*

_{max}. If

*x*

_{min}=

*x*

_{max}or

*y*

_{min}=

*y*

_{max}, then even if the segments form a rectangle, it has zero area, so we also output "NO" in this case.

Now if the segments indeed form a rectangle, we know the coordinates of its vertices — (

*x*

_{min},

*y*

_{min}), (

*x*

_{min},

*y*

_{max}), (

*x*

_{max},

*y*

_{min}) and (

*x*

_{max},

*y*

_{max}). We just check that every side of this rectangle appears in the input. If it is the case, we output "YES", otherwise we output "NO".

### D. Two Paths

Take any pair of non-intersecting paths. Since Flatland is connected, there must be a third path, connecting these two. Remove a road from the third path. Then Flatland is divided into two components — one containing the first path, and the other containing the second path. This observation suggests us the algorithm: iterate over the roads; for each road remove it, find the longest path in both connected components and multiply the lengths of these paths. The longest path in a tree can be found by depth first search from each leaf.### E. Camels

Let us call an index j such that*y*

_{j - 1}>

*y*

_{j}<

*y*

_{j + 1}a

*cavity*. Also, we'll call humps and cavities by the common word

*break*. Then there must be exactly

*T*= 2

*t*- 1 breaks, and the first one must be a hump.

Denote by

*f*

_{nth}the number of ways in which a camel with

*t*breaks, ending at the point (n,h), can be extended to the end (the vertical

*x*

_{N}=

*N*) so that the total number of breaks was equal to T. Note that:

1.

*f*

_{NTh}= 1, if h=1,2,3,4. (We have already finished the camel and it has T breaks)

2.

*f*

_{Nth}= 0, if 0 ≤

*t*<

*T*,

*h*= 1, 2, 3, 4. (We have already finished the camel, but it has less than T breaks)

3.

*f*

_{n, T + 1, h}= 0, if 1 ≤

*n*≤

*N*,

*h*= 1, 2, 3, 4. (The camel has already more than T breaks).

Now we find the recurrent formula for

*f*

_{nth}. Suppose that

*t*is even. Then the last break was a cavity, and we are moving up currently. We can continue moving up, then the number of breaks stays the same, and we move to one of the points (

*n*+ 1,

*h*+ 1), (

*n*+ 1,

*h*+ 2), ..., (

*n*+ 1, 4). Or we can move down, then the number of breaks increases by 1, and we move to one of the points (

*n*+ 1,

*h*- 1), (

*n*+ 1,

*h*- 2), ..., (

*n*+ 1, 1). This gives us the formula

.

If

*t*is odd, then the last break was a hump, and similar reasoning leads to the formula

.

We can calculate

*f*

_{nth}by dynamic programming. Consider now the point (2,

*h*) on a camel. There are h-1 ways to get to this point (starting from points (1, 1), ..., (1,

*h*- 1)), and

*f*

_{2, 0, h}ways to extend the camel to the end. So the answer to the problem is .

youfor this awesome site! :)typedef complex<int> point;

set<point> S;

But it doesn't work. I can't insert any element using S.insert();

Anyway. std::complex cannot be used with std::set, because it doesn't define operator<. Write your own struct or use std::pair.

adamax.## D. Two Paths

same question on spoj.pl but 2 ≤N≤ 100000 any idea how to solve.https://www.quora.com/How-do-I-solve-SPOJ-Two-Paths-problem

7 years late -_-

About the solution of problem E.

The first one must be a hump, also ** the last one must be a hump.**

So,

f_{Nth}= 0,if0 ≤t<T,h= 1, 2, 3.BTW, when

N+ 2 <T, no camel with t humps exist, so the answer is 0.nice editorial , problem D and E are very nice ..