zoukankan      html  css  js  c++  java
  • Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]

    E. Sonya and Problem Wihtout a Legend
    time limit per test
    5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Sonya was unable to think of a story for this problem, so here comes the formal description.

    You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.

    Next line contains n integer ai (1 ≤ ai ≤ 109).

    Output

    Print the minimum number of operation required to make the array strictly increasing.

    Examples
    input
    7
    2 1 5 11 5 9 11
    output
    9
    input
    5
    5 4 3 2 1
    output
    12
    Note

    In the first sample, the array is going to look as follows:

    11

    |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9

    And for the second sample:

    5

    |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12


    题意:一个序列加减1变成严格单增最少操作数


    很像LIS,然后就没时间了

    d[i][j]表示前i个以j结尾的最少操作数

    d[i][j]=min{d[i-1][k]+a[i]-j:k<=?j}

    j离散化 --> m[j]  sort(m)  可以发现最后的一个一定可以是原序列中的值

    严格单调递增,直接处理好难我不会(因为那样的话不一定原序列结尾了,可能+1),网上题解上都是让a[i]-=i变成不严格

    可以用mn维护min(d[i-1][k]),少了一层循环

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    const int N=3005;
    const ll INF=1e19;
    int n,k,a[N],m[N];
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    ll d[N][N],ans=INF;
    void dp(){
        for(int i=1;i<=n;i++){
            ll mn=INF;
            for(int j=1;j<=k;j++){
                mn=min(mn,d[i-1][j]);
                d[i][j]=mn+abs(a[i]-m[j]);
            }
        }
    }
    
    int main(int argc, const char * argv[]) {
        n=read();
        for(int i=1;i<=n;i++) a[i]=m[i]=read()-i;
        sort(m+1,m+1+n);
        k=unique(m+1,m+1+n)-m-1;//printf("k %d
    ",k);
        dp();
        for(int i=1;i<=k;i++) ans=min(ans,d[n][i]);
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    四则运算3.2
    第二周进度条
    构建之法阅读笔记02
    四则运算2
    第一周进度条
    构建之法阅读笔记01
    四则运算 Python
    第一周第二周学习进度条
    《构建之法》学习中疑问
    小学四则运算1.0
  • 原文地址:https://www.cnblogs.com/candy99/p/5875191.html
Copyright © 2011-2022 走看看