Connection Status:
Competition Arena > StablePairsDiv1
TCO18 Fun 1B · 2018-04-20 · by ltdtl · Dynamic Programming, Simple Math
Class Name: StablePairsDiv1
Return Type: int[]
Method Name: findMaxStablePairs
Arg Types: (int, int, int)
Problem Statement

Problem Statement

Consider the set of integers between 1 and n, inclusive, and two positive integers c and k. You want to build an ordered list of k pairs (x1, y1), (x2, y2), ... (xk, yk) such that the following conditions are met.

  1. 1 ≤ xi < yin for all i between 1 and k, inclusive.
  2. yi < xi+1 for all i between 1 and k-1, inclusive.
  3. (xi+1 + yi+1) - (xi + yi) = c for all i between 1 and k-1, inclusive.
If a list of pairs satisfies all of the above conditions, the list is said to be stable. For any fixed n, c, and k, a stable list of k pairs is said to be maximal if its sum of elements (the sum of all 2k integers it contains) is the largest among all stable lists of k pairs.


For instance, consider n=5, c=4, and k=2. There are two stable lists of pairs: one is [(1, 2), (3, 4)] and the other is [(2, 3), (4, 5)]. The latter is the only maximal stable list of pairs in this example as its sum is (2+3+4+5) = 14.


Given n, c, and k, find one maximal stable list of pairs, and return a int[] that describes the list. Specifically, if (x1, y1), (x2, y2), ..., (xk, yk) is your maximal stable list of pairs, you must return { x1, y1, x2, y2, ..., xk, yk }.

If there are many maximal stable lists of pairs, you may return any one of them. If no stable list of pairs with the desired properties exists, you must return an empty int[].

Constraints

  • n will be between 1 and 100, inclusive.
  • c will be between 1 and 100, inclusive.
  • k will be between 1 and 100, inclusive.
Examples
0)
5
4
2
Returns: {2, 3, 4, 5 }

This example was described in the problem statement.

1)
4
4
2
Returns: {1, 2, 3, 4 }
2)
1
100
1
Returns: { }

When n=1, regardless of c and k, there is no way to build a pair.

3)
3
100
1
Returns: {2, 3 }

With k=1 we are looking for stable lists that only contain a single pair of numbers. There are three stable lists: [(1, 2)], [(1, 3)], and [(2, 3)]. Obviously, the last one is the only maximal one in this case.

4)
10
6
3
Returns: {2, 5, 6, 7, 9, 10 }

Submissions are judged against all 171 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class StablePairsDiv1 with a public method vector<int> findMaxStablePairs(int n, int c, int k) · 171 test cases · 2 s / 256 MB per case

Submitting as anonymous