Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:10187 | Accepted: 4724 |
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
题意:
给定一个序列a,现在想要构造一个新序列是单调不增或单调不减的
定义S为原序列和新序列对应位置数之差的绝对值之和
要求S最小时的S
思路:
求单调不减和单调不增两个值 取最小【这道题有bug啊,就求了一个单调不减我就交了居然过了,不过反正同理】
虐狗宝典上的方法一我没怎么看懂 n^3的算法还难理解 算了弃了
引理:构造的新序列B中的数都在A中出现过 【数学归纳法可证】
方法二就是一个二维的dp dp[i][j]表示以长度为i的子序列,且Bi == j时的S最小值
dp[i][j] = a[i] - j + val, 其中val为min(dp[i - 1][k]), k < j
然后先把a中出现的数离散化一下就行了
今天学习到了一个新的离散化的方法
先对数组sort, 然后使用unique函数
unique函数将一个数组中相邻的相同的数删掉,只剩唯一一个
unique(num, num + n) - num表示返回的数组的长度
//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long int LL; const double eps = 1e-; int sgn(double x)
{
if(fabs(x) < eps) return ;
if(x < ) return -;
else return ;
}
struct point{
double x, y;
point(){}
point(double _x, double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b)const
{
return point(x - b.x, y - b.y);
}
double operator ^(const point &b)const
{
return x * b.y - y * b.x;
}
double operator *(const point &b)const
{
return x * b.x + y * b.y;
}
void input()
{
scanf("%lf%lf", &x, &y);
}
}; struct line{
point s, e;
line(){}
line(point _s, point _e)
{
s = _s;
e = _e;
}
pair<int, point>operator &(const line &b)const
{
point res = s;
if(sgn((s - e) ^ (b.s - b.e)) == ){
if(sgn((s - b.e) ^ (b.s - b.e)) == ){
return make_pair(, res);
}
else return make_pair(, res);
}
double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
res.x += (e.x - s.x) * t;
res.y += (e.y - s.y) * t;
return make_pair(, res);
}
}; bool inter(line l1, line l2)
{
return
max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
sgn((l2.s - l1.s) ^ (l1.e - l1.s)) * sgn((l2.e - l1.s) ^ (l1.e - l1.s)) <= &&
sgn((l1.s - l2.s) ^ (l2.e - l1.s)) * sgn((l1.e - l2.s) ^ (l2.e - l2.s)) <= ;
} double area(point a, point b, point c)
{
return fabs((1.0 / ) * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)));
} const int maxn = ;
int n;
int a[maxn], dp[maxn][maxn], num[maxn];
map<int, int> mp; int main()
{
while(scanf("%d", &n) != EOF){
int cnt = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
num[i] = a[i];
}
sort(num + , num + + n);
cnt = unique(num + , num + + n) - num - ;
//cout<<cnt<<endl; memset(dp, inf, sizeof(dp));
dp[][] = ;
for(int i = ; i <= n; i++){
int val = dp[i - ][];
for(int j = ; j <= cnt; j++){
if(dp[i - ][j] < val){
val = dp[i - ][j];
}
dp[i][j] =val + abs(a[i] - num[j]);
}
} int ans = inf;
for(int i = ; i <= cnt; i++){
ans = min(ans, dp[n][i]);
}
printf("%d\n", ans);
}
return ;
}