#P2591. Minimum Segments in the K-th Layer

    ID: 15860 Type: Default 1000ms 256MiB

Minimum Segments in the K-th Layer

Minimum Segments in the K-th Layer

We are given N continuous functions \(f_i(x)\) for \(1\le i\le N\) satisfying the following conditions:

  • For any two distinct indices \(i\) and \(j\) (with \(1\le i,j\le N\)), there exists exactly one \(x\) such that \(f_i(x)=f_j(x)\), and there are infinitely many \(x\)'s where \(f_i(x)<f_j(x)\).
  • For any three distinct indices \(1\le i<j<k\le N\), there is no \(x\) such that \(f_i(x)=f_j(x)=f_k(x)\).

Such functions can be arranged so that when drawn, the lower envelope (the union of the minimal parts of the graphs) is called the first layer (illustrated in red in the picture). After removing the first layer segments, the lower envelope of what remains is called the second layer and so on. In the example with N=3 the first layer is divided into 3 segments, the second layer into 4 segments, and the third (top) layer into 2 segments.

Your task is: Given N and an integer K (with \(1\le K\le N\)), determine the minimum possible number of segments that the K-th layer can be partitioned into over all arrangements of functions satisfying the above conditions.

Observation:

  • The first layer (i.e. the lowest envelope) always comes from the fact that every one of the \(N\) functions appears at least once as the bottom function, thereby resulting in N segments.
  • The top layer (i.e. when \(K=N\)) always has 2 unbounded segments.
  • For layers in between (i.e. \(1<K<N\)), it turns out that one can achieve an arrangement where the K-th layer is divided into N+1 segments.

Thus, the answer is determined by the following formula:

[ answer=\begin{cases} N, & \text{if } K=1,\ N+1, & \text{if } 1<K<N,\ 2, & \text{if } K=N. \end{cases} ]

Input Format: The input contains two integers N and K on a single line.

Output Format: Output the minimum possible number of segments in the K-th layer.

inputFormat

The input consists of a single line containing two integers N and K separated by a space, where \(2\le N\le 10^5\) and \(1\le K\le N\).

outputFormat

Output a single integer representing the minimum number of segments in the K-th layer.

sample

3 1
3