Technique · CP 2400+
Connected-Component DP (insertion DP)
Process elements in sorted order, keeping only how many 'components' (open groups, or partial segments of the final permutation) exist. Each new element starts a component, extends one, or merges two, with counts for each choice.
0%
mastery
0/4 solved
0/4 solved
Recognise it
The signal
count permutations where adjacent elements must satisfy a relative-size condition
1Brute force
Enumerate permutations/partitions: n!.
2The observation
The positions of earlier elements do not matter, only how many groups are still open.
3Why it works
After the i smallest elements, the state (j open components, cost so far) decides the future. The cost of the open groups grows by j*(a_{i+1} - a_i).
Reach for it when
- +partition sorted items into teams whose cost is max - min
- +the final arrangement is built by inserting values in increasing order
Not when
- ×the condition depends on exact positions, not relative order
Practice
0/4 solved
- Coding CompanyCSESMedium
- Counting PermutationsCSESMedium
- Phoenix and ComputersCodeforces2200
- Ant ManCodeforces2500
Pitfalls
Misconceptions
- !forgetting border components in permutation problems