DP-POJ-3666-Making the Grade

Making the Grade
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5028 Accepted: 2383
Description

A straight dirt road connects two fields on FJ’s farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).

You are given N integers A1, … , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . … , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove dirt at any position along the road, the total cost of modifying the road is

|A1 - B1| + |A2 - B2| + … + |AN - BN |
Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.

Input

  • Line 1: A single integer: N
  • Lines 2..N+1: Line i+1 contains a single integer elevation: Ai

Output

  • Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.

Sample Input

7
1
3
2
4
5
3
9
Sample Output

3
Source

USACO 2008 February Gold

题意:给一串0到10^9的数字,每个数字都能增加或减少,并且增加和减少的消耗是一样的,要求最少的消耗,使得整个序列变成一个单调非增或者单调非减的序列。


想了半天没想出怎么做,后来看了看其他acmer的思路,用了比较无脑的二维来做。
先将所有的数字排一个序,放在sh数组里。
然后dp[i][j]表示使原序列中前i+1个数的最大数为sh[j]所要的最小消耗。
那么dp[i][j]=min(dp[i-1][0~j]+abs(sh[j]-height[i]))。
我就先三重for提交了一发,TLE打回来。
观察后可以知道,其实不用第三个循环,用一个变量k来存min(dp[i-1][j]),那么进入j的下一次循环时,只需要将dp[i-1][j+1]与k对比取较小值即可了。
这道题在POJ上的数据比较水,所以直接求单调非减就可以了。

//
//  main.cpp
//  基础DP1-S-Making the Grade
//
//  Created by 袁子涵 on 15/10/28.
//  Copyright © 2015年 袁子涵. All rights reserved.
//
//  125ms   32108KB

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define minof(A,B)  ((A)>(B)?(B):(A))

using namespace std;

int N;
long long int height[2002],sh[2002];
long long int dp[2002][2002];

int comp(const void* a,const void* b)
{
    return *(int*)a-*(int*)b;
}

long long int ABS(long long int num)
{
    if (num>=0) {
        return num;
    }
    else
        return -num;
}

void DP()
{
    for (int i=0; i<N; i++) {
        dp[0][i]=ABS(height[0]-sh[i]);
    }
    for (int i=1; i<N; i++) {
        long long int k=dp[i-1][0];
        for (int j=0; j<N; j++) {
            long long int temp=ABS(height[i]-sh[j]);
            k=minof(k, dp[i-1][j]);
            dp[i][j]=k+temp;
        }
    }
}

int main(int argc, const char * argv[]) {
    long long int out=0x7fffffff;
    scanf("%d",&N);
    for (int i=0; i<N; i++) {
        scanf("%lld",&height[i]);
        sh[i]=height[i];
    }
    qsort(sh, N, sizeof(long long int), comp);
    DP();
    for (int i=0; i<N; i++) {
        out=minof(dp[N-1][i],out);
    }
    printf("%lld\n",out);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值