poj3666(Making the Grade)

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
1
3
2
4
5
3
9

Sample Output

3

题意:给定一个正整数序列a[1...n],要求你改变每一个数变成c[1...n],使得改变后的序列非严格单调,改变的代价为sigma(abs(a[i]-c[i])),求代价最小值。

题解:显然c[i]必定为a[1...n]中的某个值,且由于a过大,所以离散化,将a数组保存在b数组中并排序。设dp[i][j]为第i个数改变为b[j]时代价最小值,则dp[i][j]=min(dp[i-1][k])+abs(a[i]-b[j]),其中k<=j,即b[k]<b[j]。这题还可以使用滚动数组来优化空间。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

int n, a[2010], b[2010], dp[2010];

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b, b+n);
    for (int i = 0; i < n; ++i)
        dp[i] = abs(a[0]-b[i]);
    for (int i = 1; i < n; ++i)
    {
        int tmp_min = dp[0];
        for (int j = 0; j < n; ++j)
        {
            tmp_min = min(tmp_min, dp[j]);
            dp[j] = tmp_min + abs(a[i]-b[j]);
        }
    }
    cout << *min_element(dp, dp+n) << endl;
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 20:25:47

poj3666(Making the Grade)的相关文章

线性DP POJ3666 Making the Grade

Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7347   Accepted: 3415 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

「kuangbin带你飞」专题十二 基础DP

layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathjax: true - kuangbin - 动态规划 传送门 A.HDU1024 Max Sum Plus Plus 题意 给你N个数,然后你分成M个不重叠部分,并且这M个不重叠部分的和最大. 思路 动态规划最大m字段和,dp数组,dp[i][j]表示以a[j]结尾的,i个字段的最大和 两种情况:1.第a[j

Making the Grade(POJ3666)

题目大意: 给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调增或者单调减(不严格). 题解: 1.一开始我有一个猜想,就是不管怎么改变,最终的所有数都是原来的某个数.然而我并不会证明,然而我属于那种不彻底弄清楚就不会去写的那种顽固分子,于是就脱了好几天.网络上有很多关于此题的题解,确实用了这个猜想来离散化,但是都是讲怎么dp,然后最后扯一句“由于数据比较大,可以离散化”之类的话,要么就是相当粗略的证明(也许已经说的够清楚了只不过我没理解...). 2.今天早上起

POJ3666:Making the Grade——题解

http://poj.org/problem?id=3666 题目大意:给n个数,每次操作可使一个数+1或-1,求最小操作数使得序列不下降或不上升. —————————————————————— 思路:http://blog.csdn.net/luovilonia/article/details/44004041 因为我再讲什么也没什么好讲的了. 但是这人的代码是错的……请注意查找最长不上升和不下降所要减的值是不一样的. #include<cstdio> #include<cstring

【POJ3666】Making the Grade 离散化+DP

学到了一个引理:在满足S最小化的条件下,一定存在一种构造序列B的方案,使得序列B中的数值都来自于A中.(数学归纳法+中位数定理得证) 对于状态的表示来说,首先肯定有一个 i ,表示选到了第 i 个数时对应的最优解,由于需要维护序列单调性,因此需要再在状态中加入一个因素 j ,表示在第 i 位选了离散化后的A[ j ]. 状态转移为\(dp[i][j]=min\{dp[i-1][k],k\in[1,j]\}+|A[i]-B[j]|\) 代码如下: #include <cstdio> #inclu

POJ3666Making the Grade[DP 离散化 LIS相关]

Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 2994 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

BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )

最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 i 个路面单调不递减, 第 x 个路面修整为原来的第 t 高的高度. 时间复杂度O( n³ ). 令g(x, t) = min{ dp(x, k) } (1 <= k <= t), 则转移O(1), g() 只需在dp过程中O(1)递推即可, 总时间复杂度为O( n² ) 然后单调不递增也跑一遍

POJ 3666 Making the Grade [DP]

题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式. 思路: 很容易想到是DP. 1. 对前i个序列,构成的最优解其实就是与两个参数有关.一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost. 显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌. 用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost. 所以状态转移方

Furniture grade plywood from Long Da Wood xzml

Glad to hear that you're on the market for plywood products. We specialize in this field for 16 years, with good quality and competitive price two hour reach Qingdao port, this is how we keep good quality and competitive prices for global valued cust