首页 > 其他 > 详细

poj 3666

时间:2019-03-19 11:22:03      阅读:155      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int SIZE = 2010;
int a[SIZE], c[SIZE], nums[SIZE];
int f[SIZE][SIZE];
int n;
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        nums[i] = a[i];
    }
    sort(nums+1, nums+n+1);
    int m = unique(nums+1, nums+n+1)-nums-1;
    for (int i = 1; i <= n; i++)
        c[i] = lower_bound(nums+1, nums+m+1, a[i]) - nums;
    memset(f, 0x3f, sizeof(f));
    for(int i=1;i<=m;i++)
       f[0][i]=0;
    for (int i = 1; i <= n; i++) {
        int temp = f[i-1][1];
        for (int j = 1; j <= m; j++) {
            f[i][j] = temp + abs(a[i] - nums[j]);
            temp = min(temp, f[i-1][j+1]);
        }
    }
    int ans = 1 << 30;
    for (int i = 1; i <= m; i++)
        ans = min(ans, f[n][i]);
    cout << ans << endl;
}

 

poj 3666

原文:https://www.cnblogs.com/lishengkangshidatiancai/p/10557311.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!