CodeForces 713C Sonya and Problem Wihtout a Legend

$dp$。

非严格递增的可以由$dp$解决。可以将严格递增转化为非严格递增。

要保证$a[i]<a[i+1]$,就是要保证$a[i]≤a[i+1]-1$,也就是$a[i]-i≤a[i+1]-1-i$,等价于$a[i]-i≤a[i+1]-(i+1)$。

因此只要将$a[i]-i$,求非严格递增的就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c=getchar(); x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-'0'; c=getchar();}
}

const int maxn=3000+10;
long long dp[maxn][maxn];
long long a[maxn],q[maxn];
long long MIN[maxn];
int n;

long long ABS(long long a)
{
    return max(a,-a);
}

bool cmp(const int&a,const int&b)
{
    return a>b;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&q[i]);
            a[i]=q[i];
            q[i]=q[i]-i;
            a[i]=a[i]-i;
        }
        sort(q+1,q+n+1);
        memset(dp,-1,sizeof dp);
        memset(MIN,0,sizeof MIN);

        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++) dp[i][j]=MIN[j]+ABS(a[i]-q[j]);
            MIN[1]=dp[i][1];
            for(int j=2; j<=n; j++) MIN[j]=min(MIN[j-1],dp[i][j]);
        }
        long long ans1=dp[n][1];
        for(int i=2; i<=n; i++) ans1=min(ans1,dp[n][i]);

        printf("%lld
",ans1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5879399.html