Codeforces 13C Sequence dp

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}


以上是关于Codeforces 13C Sequence dp的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 438D. The Child and Sequence(线段树区间更新)

CodeForces 1265 D. Beautiful Sequence

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

Increasing Sequence CodeForces - 11A

Codeforces 374D - Inna and Sequence

codeforces438D The Child and Sequence