poj 3666 Making the Grade(离散化+dp)

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

|AB1| + |AB2| + ... + |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

3

2

4

5

3

9

Sample Output

3

 

题意:给你一组序列 要你求把 非严格不降序列或者非严格不升序列 的最小花费求出来 (由于这题测试数据的问题我只求了非严格不降);

思路:dp[i][j]=abs(j-a[i])+min(dp[i-1][k]);(k<=j) i表示前i个数 最大高度为j的 最小花费 而j很大有1e10 所以显然不能直接开这么大的数组 所以 我们可以

把现有的高度存进数组 再对数组排序(离散化) 这样 j就表示为在数组里下标为j的高度。

然后 min(dp[i-1][k]) 对于这个最小值 我们也没有必要再用一次循环取求 因为 k<=j 所以每次只需要用一个变量去跟新最小值即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int a[2007];
int b[2007];
ll dp[2007][2007]; //dp[i][j]=abs(j-a[i])+min(dp[i-1][k]);(k<=j)
int n;
ll solveup(){
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        ll minn=dp[i-1][1];
        for(int j=1;j<=n;j++){
            minn=min(minn,dp[i-1][j]);
            dp[i][j]=abs(b[j]-a[i])+minn;
        }
    }
    ll ans=1e18;
    for(int i=1;i<=n;i++)
        ans=min(ans,dp[n][i]);
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        cout<<solveup()<<endl;
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/wmj6/p/10644836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值