POJ 3666--Making the Grade(dp)

来源:互联网 发布:剑三成男脸型数据网盘 编辑:程序博客网 时间:2024/04/30 12:33

题意:给出一组序列, 对序列中的数字每次进行加1或减1视为进行操作一次, 求将此序列变成非增序列或非减序列(并非严格单调)最小的操作次数。

解题思路:自己脑卡,看了别人的才会。

首先a数组储存原始数据,b数组储存排序后的数据。要保证操作的次数最小,那么新序列的每个数字一定是原始序列中的一些数字。

定义dp[i][j]表示第i个位置 为b数组j位置的数字时,所对应的最小值。

状态转移方程:dp[i][j] = min(abs(a[i] - b[j]) +  dp[i - 1][k]) k <= j.即在a数组前i- 1个变为有序所花费的最小代价加上a[i]变为b[j]的代价。这样反复可以求出a数组前n个变成有序的代价。在用一遍循环可以求出a数组前n个变为有序的最小代价。

因为可非增可非减,排两次序, 进行两次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

71324539

Sample Output

3


#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<list>#include<iostream>#include<map>#include<queue>#include<set>#include<stack>#include<vector>using namespace std;int buf[10];inline long long readint(){    char c = getchar();    while(!isdigit(c)) c = getchar();    int x = 0;    while(isdigit(c))    {        x = x * 10 + c - '0';        c = getchar();    }    return x;}inline void writeint(int i){    int p = 0;    if(i == 0) p++;    else while(i)        {            buf[p++] = i % 10;            i /= 10;        }    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);}//////////////////////////////////////////////////////////////////////#define MAX_N 2005const int INF = 0x3f3f3f3f;int a[MAX_N];int b[MAX_N];int dp[MAX_N][MAX_N];//dp[i][j]表示第i个位置 为b数组j位置的数字时,所对应的最小值。bool cmp(int a, int b){    return a > b;}int main(){    int n = readint();    for(int i = 0 ; i < n ; i++)    {        scanf("%d", &a[i]);        b[i] = a[i];    }    sort(b, b + n);    memset(dp, INF, 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 min_num = INF;        for(int j = 0 ; j < n ; j++)        {            min_num = min(min_num, dp[i - 1][j]);            dp[i][j] = min(min_num + abs(a[i] - b[j]), dp[i][j]);        }    }    int ans = INF;    for(int i = 0 ; i < n ; i++)    {        if(dp[n - 1][i] < ans)        {            ans = dp[n - 1][i];        }    }    sort(b, b + n , cmp);    memset(dp, INF, 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 min_num = INF;        for(int j = 0 ; j < n ; j++)        {            min_num = min(min_num, dp[i - 1][j]);            dp[i][j] = min(min_num + abs(a[i] - b[j]), dp[i][j]);        }    }    for(int i = 0 ; i < n ; i++)    {        if(dp[n - 1][i] < ans)        {            ans = dp[n - 1][i];        }    }    cout<<ans<<endl;    return 0;}






0 0
原创粉丝点击