StablePairsDiv1
TCO18 Fun 1B · 2018-04-20 · by ltdtl
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 ≤ xi < yi ≤ n for all i between 1 and k, inclusive.
- yi < xi+1 for all i between 1 and k-1, inclusive.
- (xi+1 + yi+1) - (xi + yi) = c for all i between 1 and k-1, inclusive.
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
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
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.
5
4
2
Returns: {2, 3, 4, 5 }
This example was described in the problem statement.
4
4
2
Returns: {1, 2, 3, 4 }
1
100
1
Returns: { }
When n=1, regardless of c and k, there is no way to build a pair.
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.
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.
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