题面
A sequence of integer numbers is called strictly monotonically increasing if every term of the sequence is strictly greater than the one preceding it. Similarly, a sequence is called strictly monotonically decreasing if every term is strictly less than the one preceding it. A strictly monotonic sequence is a sequence that is either strictly monotonically increasing or decreasing. A sequence of integers is called k-monotonic if it can be decomposed into k disjoint contiguous subsequences that are strictly monotonic.
.
For example a strictly monotonically increasing sequence is 1-monotonic — in fact it is k-monotonic for every k between 1 and the number of elements it contains. The sequence { 1, 2, 3, 2, 1 } is 2-monotonic since it can be decomposed into { 1, 2, 3 } and { 2, 1 }.
.
If a sequence is not k-monotonic, you can transform it into a k-monotonic sequence by performing the following operation one or more times: select any term in the sequence and either increase it or decrease it by one. You are allowed to perform any number of these operations on any of the terms. Given a sequence of numbers A1, A2, …, An and an integer k, you are to calculate the minimum number of operations required to transform the given sequence into a k-monotonic sequence.
题解
题意
给定一个数组,定义区间
[
l
,
r
]
[l,r]
[l,r]
K
K
K单调为,区间中,存在一种切割方式,将数组隔开为
K
K
K 个小区间,小区间内严格单调递增/递减
推到,如果一个长度是为
4
4
4 的
1
1
1 单调区间,那么它也是
k
,
k
∈
{
2
,
3
,
4
}
k,k\in\{ 2,3,4\}
k,k∈{2,3,4}单调区间
分析
考虑
1
1
1 单调的情况,求出区间
l
l
l ~
r
r
r 变为
1
1
1单调的代价,分为单调递增和单调递减两种
严格单调增,需要把
a
[
i
]
−
=
i
a[i]−=i
a[i]−=i 处理
求单调递减的时候,将
a
[
i
]
+
=
i
a[i]+=i
a[i]+=i 再取负,同样计算
遍历起点,计算所有的 l l l ~ r r r 的代价
最后,通过 d p dp dp 计算最后的结果
d p [ k ] [ i ] = min ( d p [ k − 1 ] [ j ] + min ( c o s t A [ j + 1 ] [ i ] , c o s t B [ j + 1 ] [ i ] ) ) dp[k][i]=\min(dp[k−1][j]+\min(costA[j+1][i],costB[j+1][i])) dp[k][i]=min(dp[k−1][j]+min(costA[j+1][i],costB[j+1][i]))
状态表示为,将前i个转化为k单调需要的代价
答案为 d p [ K ] [ N ] dp[K][N] dp[K][N]
//322971D
/*
@Author: YooQ
*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define sc scanf
#define pr printf
#define ll long long
#define int long long
#define FILE_OUT freopen("out", "w", stdout);
#define FILE_IN freopen("in", "r", stdin);
#define debug(x) cout << #x << ": " << x << "\n";
#define AC 0
#define WA 1
#define INF 0x3f3f3f3f
const ll MAX_N = 1e6+5;
const ll MOD = 1e9+7;
int N, M, K;
struct Tr{
int k, l, r, dis;
}tr[MAX_N];
int indx = 0;
int merge(int x, int y) {
if (!x || !y) return x | y;
if (tr[x].k < tr[y].k) swap(x, y);
tr[x].r = merge(tr[x].r, y);
if (tr[tr[x].l].dis <= tr[tr[x].r].dis) {
swap(tr[x].l, tr[x].r);
}
tr[x].dis = tr[tr[x].r].dis + 1;
return x;
}
int mk(int x) {
tr[++indx].k = x;
tr[indx].dis = tr[indx].l = tr[indx].r = 0;
return indx;
}
void pop(int& rt) {
rt = merge(tr[rt].l, tr[rt].r);
tr[rt].dis = tr[tr[rt].r].dis + 1;
}
int arr[MAX_N];
int brr[MAX_N];
int stk[MAX_N];
int sz[MAX_N];
int cnt[MAX_N];
int tt = 0;
void init() {
indx = 0;
tt = 0;
}
int costA[1005][1005];
int costB[1005][1005];
void calc(int st) {
init();
int res = 0;
for (int i = st; i <= N; ++i) {
stk[++tt] = mk(arr[i]);
cnt[tt] = sz[tt] = 1;
while (tt-1 && tr[stk[tt-1]].k >= tr[stk[tt]].k) {
if (sz[tt]&1) res += tr[stk[tt-1]].k - tr[stk[tt]].k;
stk[tt-1] = merge(stk[tt-1], stk[tt]);
sz[tt-1] += sz[tt];
cnt[tt-1] += cnt[tt];
--tt;
while (cnt[tt] > (sz[tt]+1)/2) {
pop(stk[tt]);
--cnt[tt];
}
}
costA[st][i] = res;
}
init();
res = 0;
for (int i = st; i <= N; ++i) {
stk[++tt] = mk(brr[i]);
cnt[tt] = sz[tt] = 1;
while (tt-1 && tr[stk[tt-1]].k >= tr[stk[tt]].k) {
if (sz[tt]&1) res += tr[stk[tt-1]].k - tr[stk[tt]].k;
stk[tt-1] = merge(stk[tt-1], stk[tt]);
sz[tt-1] += sz[tt];
cnt[tt-1] += cnt[tt];
--tt;
while (cnt[tt] > (sz[tt]+1)/2) {
pop(stk[tt]);
--cnt[tt];
}
}
costB[st][i] = res;
}
}
int dp[12][1005];
void solve(){
init();
for (int i = 1; i <= N; ++i) {
sc("%lld", &arr[i]);
brr[i] = - (arr[i] + i);
arr[i] -= i;
}
for (int i = 1; i <= N; ++i) {
calc(i);
}
memset(dp, INF, sizeof dp);
dp[0][0] = 0;
for (int k = 1; k <= K; ++k) {
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= i; ++j) {
dp[k][i] = min(dp[k][i], dp[k-1][j-1] + min(costA[j][i], costB[j][i]));
}
}
}
pr("%lld\n", dp[K][N]);
}
signed main()
{
#ifndef ONLINE_JUDGE
//FILE_IN
FILE_OUT
#endif
int T = 1;//cin >> T;
while (sc("%lld%lld", &N, &K), N, K) solve();
return AC;
}