POJ-3016 K-Monotonic

文章目录

题面

传送门

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[k1][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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hexrt

客官,请不要给我小费!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值