标签:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 2000 + 10;
int a[maxn],b[maxn],n;
int dp[maxn][maxn];
bool cmp(int x,int y){
return x > y;
}
int main(){
scanf("%d",&n);
for(int i = 0;i < n ; ++i){
scanf("%d",&a[i]);
}
memcpy(b,a,sizeof(int) * (n + 1));
sort(b,b + n);
memset(dp,0x7f,sizeof(dp));
for(int i = 0;i < n ; ++i){
dp[0][i] = abs(a[0] - b[i]);
}
for(int i = 1;i < n ; ++i){
int mindp = 0x7f7f7f7f;
for(int j = 0;j < n;++j){
mindp = min(dp[i-1][j],mindp);
dp[i][j] = min(dp[i][j] , mindp + abs(a[i] - b[j]));
}
}
int ans = 0x7f7f7f7f;
for(int i = 0;i < n ; ++i){
ans = min(dp[n - 1][i] ,ans);
}
sort(b,b + n,cmp);
memset(dp,0x7f,sizeof(dp));
for(int i = 0;i < n ; ++i){
dp[0][i] = abs(a[0] - b[i]);
}
for(int i = 1;i < n ; ++i){
int mindp = 0x7f7f7f7f;
for(int j = 0;j < n;++j){
mindp = min(dp[i-1][j],mindp);
dp[i][j] = min(dp[i][j] , mindp + abs(a[i] - b[j]));
}
}
for(int i = 0;i < n ; ++i){
ans = min(dp[n - 1][i] ,ans);
}
printf("%d\n",ans);
return 0;
}
[2016-03-28][POJ][3666][]Making the Grade]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/0f251142b5970f12318ac5ce3612fb50.html