Codeforces 13C Sequence dp

题目链接:http://codeforces.com/problemset/problem/13/C

题意:

给定n长的序列

每次操作能够给每一个数++或--

问最少须要几步操作使得序列变为非递减序列

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<queue>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 5005
#define ll __int64
inline ll Abs(ll x){return x>0?x:-x;}
ll n;
ll a[N],b[N], dp[N];//dp[j]为[1-j]为定点的把前i个数变成x序列的花费
int main(){
	ll i, j;
	while(cin>>n){
		for(i=1;i<=n;i++)scanf("%I64d",&a[i]), b[i] = a[i];
		sort(b+1,b+1+n);
		memset(dp, 0, sizeof dp);
		for(i=1;i<=n;i++) {
			for(j=1;j<=n;j++) {
				dp[j]+=Abs(a[i]-b[j]);
				if(j>1)
				dp[j] = min(dp[j-1], dp[j]);
			}
		}
		cout<<dp[n]<<endl;
	}
	return 0;
}


posted @ 2017-06-13 20:03  llguanli  阅读(143)  评论(0编辑  收藏  举报