其他分享
首页 > 其他分享> > Making the Grade(POJ-3666)

Making the Grade(POJ-3666)

作者:互联网

题目大意是给出一个长度为 n 的序列,要求使序列变为单调上升或单调不减序列(非严格),问花费的最少代价?
转移方程是:dp[i][j]表示前i个元素的最后一个元素为全部元素第j小时的最小代价

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<stack>
#include<vector>
#include<map>
#define PI acos(-1.0)
#define LL long long 
#define INF 0x3f3f3f3f
const int maxn=2000+5;
const int MOD=10007;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,1,-1};
using namespace std;

int a[maxn];
int b[maxn];
int dp[maxn][maxn];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) {
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b+1,b+1+n);
	for(int i=1;i<=n;i++){
		int minn=INF;
		for(int j=1;j<=n;j++){
			minn=min(minn,dp[i-1][j]);
			dp[i][j]=abs(a[i]-b[j])+minn;
		}
	}
	int res=INF;
	for(int i=1;i<=n;i++) res=min(res,dp[n][i]);
	cout<<res<<endl;
}

标签:3666,const,minn,Grade,int,maxn,Making,include,dp
来源: https://blog.csdn.net/hahadelaochao/article/details/115588894