Topic links
cf868f. Yet another minimization problem
Exercises
\ (f_{i,j}=\min\limits_{k=1}^{i}\{f_{k,j-1}+w_{k,i}\}\)
\ (w_{l,r}\) is the cost of the interval \ ([l,r]\) , the classic form of 1d1d
This is a shift that has a monotonic decision.
Can not be quickly transferred, we consider the division of Treatment
For the current division of Partition \ ([l,r]\) , its optimal decision interval is between \ ([l,r]\) .
For \ ([l,r]\) midpoint \ (mid\), we can brute force sweep \ ([l?mid]\)
Find the optimal decision point P for mid. Because the decision is monotonous, the \ ([l,mid?1]\) Optimal decision interval is \ ([l,p]\), and \ ([mid+1,r]\), the optimal decision interval in \ ([p,r]\) on
Divide and conquer.
Solution interval:\ (|\gets preprocessing \to | l\frac{\qquad\qquad\qquad\downarrow^{mid}\qquad\qquad\qquad}{}r\)
Decision interval:\ (l\frac{\qquad\qquad\qquad\downarrow^{p}\qquad\qquad\qquad}{}r\)
Code
#include <cstdio> #include <cstring> #include <algorithm> #define LL Long Long #define GC GetChar () # Define PC Putcharinline int read () {int x = 0,f = 1; char C = GC; while (C < ' 0 ' | | c > ' 9 ') C = GC; while (c <= ' 9 ' && C >= ' 0 ') x = x * ten + C-' 0 ', C = GC; return x * f;} void print (LL x) {if (x >=) print (X/10); PC (x 10 + ' 0 '); } int n,k; const int MAXN = 200007; int A[MAXN],B[MAXN],C[MAXN]; LL F[MAXN],DP[MAXN]; void Solve (int l,int r, int l,int R,int W) {if (L > R) return; int mid = L + R >> 1,k = 0,p = Std::min (mid,r); for (int i = l;i <= mid;++ i) W + = C[a[i]] + +; for (int i = l;i <= p;++ i) {W-=-c[a[i]]; if (Dp[mid] > F[i] + w) dp[mid] = F[i] + w,k = i; } for (int i = l;i <= p;++ i) W + = C[a[i]] + +; for (int i = l;i <= mid;++ i) W-=--c[a[i]]; Solve (L,MID-1,L,K,W); for (int i = l;i <= mid;++ i) W + = C[a[i]] + +; for (inti = L;i < k;++ i) W-=-c[a[i]]; Solve (mid + 1,r,k,r,w); for (int i = L;i < k;++ i) + + c[a[i]]; for (int i = l;i <= mid;++ i)--c[a[i]]; } int main () {n = read (), K = Read (); for (int i = 1;i <= n;++ i) f[i] = F[i-1] + c[a[i] = Read ()] + +; memset (c,0,sizeof c); for (int i = 1;i <= k;++ i) {memset (dp,0x3f,sizeof DP); Solve (1,n,1,n,0); Std::swap (F,DP); } print (Dp[n]); return 0; }
CF868 F. Yet another minimization problem decision-making monotone optimization divide-and-conquer