Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend(技巧 + 离散化dp)

分析:

首先,如果只是改变成非严格递增子序列,我们可以用离散化dp来做。
dp[i][j] 表示:前 i 个数,第i个数以原序列中第 j 大的数结尾的最小花费。转移的时候维护一个前缀最小值,就可以实现O(1)转移, O(n2) 状态的dp。
但是这里是严格单调递增。我们看看序列严格单调递增的充要条件:

AiAjij

AiiAjj

所以我们把原序列,每一个数的减去它的下标,就转化为第一种问题了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;
typedef long long LL;
const int maxn = 3e3 + 15;

struct jibancanyang {
    int n, A[maxn], B[maxn];
    LL dp[maxn][maxn];

    void fun() {
        cin >> n; 
        for (int i = 1; i <= n; ++i) cin >> A[i], A[i] -= i, B[i] = A[i];
        sort(B + 1, B + 1 + n);
        for(int i = 1; i <= n; i++) {
            LL temp = dp[i - 1][1];
            for(int j = 1; j <= n; j++) {
                temp = min(temp, dp[i - 1][j]);
                dp[i][j] = abs(A[i] - B[j]) + temp;
            }
        }
        LL ans = LL(1e18);
        for(int i = 1; i <= n; i++) ans = min(ans, dp[n][i]);
        cout << ans << endl; 
    }

}ac;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    ac.fun();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值