Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp)

++


n

x

dp[i][j]=abs(b[i]a[j])+min(dp[i1][k]),kj
iji1kj
O(n2)O(1)
aiajij,ij
aiiajj
ai=aii


代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           3005
#define   MAXN          1000005
#define   maxnode       3
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-8;
const LL     mod   = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

int a[MAX];
int b[MAX];
LL dp[MAX][MAX];
LL maxv[MAX];
int main(){
    int n;
    while(cin>>n){
        for(int i=0;iscanf("%d",&a[i]);
            a[i]-=i;
            b[i]=a[i];
        }
        sort(a,a+n);
        int tot=unique(a,a+n)-a;
        for(int i=0;i0][i]=abs(b[0]-a[i]);
            if(i==0) maxv[i]=dp[0][i];
            else maxv[i]=min(dp[0][i],maxv[i-1]);
        }
        for(int i=1;ifor(int j=0;jabs(b[i]-a[j])+maxv[j];
                if(j==0) maxv[j]=dp[i][j];
                else maxv[j]=min(maxv[j-1],dp[i][j]);
            }
        }
        LL ans=1e18;
        for(int i=0;i1][i]);
        }
        cout<return 0;
}

你可能感兴趣的:(Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp))