2289C Problem from Ust-Katav
- Difficulty: Reference Unknown/Perceived 1700
- Result: 1A
- Time spent: 62min
- Knowledge point: divide and conquer
- Chain of thought:
- . Noticed that the given is a permutation, i.e., there will be no duplicate numbers
- . The n^2 approach is obvious, but the required time complexity is n or nlogn
- ? Process each position one by one and then add them up?
- X No result after thinking for 20min
- ? Notice that the upper bound is n*(n+1)/2; consider finding the number of invalid ones?
-
- Notice that 1 in this sequence will drag many pairs down.
- ! Divide and conquer, each time find the smallest one, calculate the negative contribution caused by it, and then split it into two halves.
- Brief solution:
- Sparse table preprocessing st[l][r] represents the index of the minimum value from l to r
- Let L(n,k) be the number of invalid pairs caused by a sequence of length n whose minimum is at the k-th position (when it exceeds n/2, calculate it in reverse)
If k=1, L(n,k)=0; otherwise L(n,k)=(n-k)(k-1)+(n-2)
This has been proven
- Let dfs(l,r) represent the number of invalid pairs from l to r, p=st[l][r]
- We have dfs(l,r)=dfs(l,p-1)+dfs(p+1,r)+L(r-l+1,min(p-l+1,r-p+1))
- Final answer n*(n+1)/2-dfs(1,n)
- Time complexity is O(nlogn)








Auto comment: topic has been updated by haj1m1 (previous revision, new revision, compare).