Question Link
- Question:
N and K represent n vertices. Each vertex has a value. Note: c = max (ABS (N [I]-N [I + 1]). it is required that at most K element values in the array be changed (to any value), and the minimum value of C is obtained.
- Analysis:
You can divide the value of C into two parts to determine whether the current array can change the number of K to cut the number of C less than two points. DP and DP [I] can be used to determine the number of numbers that need to be changed when n [I] is not changed and the value of C <= 2.
- Note:
During DP, if DP [I]-> DP [J] can be transferred, DP [J] = min (DP [J], DP [I] + J-I + 1), that is to say, the elements between I and j are changed. This correctness can be explained as follows: if an element in the middle does not need to be changed, when DPI is transferred to this element and then to J, the answer is better than directly from I to J, so this transfer is correct.
Const int maxn = 2100; int EPT [maxn], DP [maxn]; int N, K; int check (LL max) {rep (I, n) DP [I] = I; rep (I, n) rep (J, I) if (ABS (IPT [J]-IPT [I]) <= max * (I-j) DP [I] = min (DP [I], DP [J] + I-j-1); rep (I, n) if (DP [I] + n-I-1 <= k) return true; return false;} int main () {While (~ Rii (n, k) {rep (I, n) RI (IPT [I]); ll l = 0, r = 2e9, M; while (L <= r) {M = (L + r)> 1; if (check (M) r = m-1; else l = m + 1 ;} cout <L <Endl;} return 0 ;}
Codeforces round #210 (Div. 1) -- levko and array