Making the Grade
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions:10187 |
|
Accepted: 4724 |
Description
A straight dirt road connects the fields on FJ's farm, but it changes elevation more than FJ would like. His cows does not mind climbing to a single slope, but they is not fond of a alternating succession of hills and V Alleys. 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 all 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 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 |
Compute the minimum cost of grading he road so it becomes a continuous slope. FJ happily informs you signed 32-bit integers can certainly is 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's the minimum cost of FJ to grade he dirt road so it becomes nonincreasing or Nondecrea Sing in elevation.
Sample Input
71324539
Sample Output
3
Source
Usaco February Gold
Test instructions
Given a sequence a, we now want to construct a new sequence that is monotonous or monotonically non-decreasing.
Defines s as the sum of the absolute values of the difference between the original sequence and the corresponding position number of the new sequence
s minimum required S
Ideas:
For monotonous and monotonous no increase of two values to minimize "This problem has a bug ah, I asked for a monotonous not to lose I have to turn over, but anyway."
Dog abuse of the method on the book I don't know how to understand N^3 's algorithm is difficult to comprehend forget it.
Lemma: The number of the new sequence B in the construct has appeared in a "mathematical inductive method can be proven"
Method Two is a two-dimensional DP Dp[i][j] representing a sub-sequence of length I, and bi = = j when the s minimum value
DP[I][J] = A[i]-j + Val, where Val is min (Dp[i-1][k]), K < J
And then we'll just scatter the numbers that appear in a.
Today, I learned a new method of discretization.
Sort the array first, and then use the unique function
The unique function deletes the same number of neighbors in an array, leaving only one
Unique (num, num + N)-num Indicates the length of the returned array
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7#include <map>8 9 #defineINF 0x3f3f3f3fTen using namespacestd; OnetypedefLong Long intLL; A - Const DoubleEPS = 1e-8; - the intSgnDoublex) - { - if(Fabs (x) < EPS)return 0; - if(X <0)return-1; + Else return 1; - } + structpoint{ A Doublex, y; at Point () {} -PointDouble_x,Double_y) - { -x =_x; -y =_y; - } inPointoperator-(ConstPoint &b)Const - { to returnPoint (x-b.x, Y-b.y); + } - Double operator^(ConstPoint &b)Const the { * returnX * b.y-y *b.x; $ }Panax Notoginseng Double operator*(ConstPoint &b)Const - { the returnX * b.x + y *b.y; + } A voidinput () the { +scanf"%LF%LF", &x, &y); - } $ }; $ - structline{ - Point S, E; the Line () {} - Line (Point _s, point _e)Wuyi { thes =_s; -E =_e; Wu } -pair<int, point>operator& (ConstLine &b)Const About { $Point res =s; - if(SGN (S-E) ^ (B.S-B.E)) = =0){ - if(SGN (S-B.E) ^ (B.S-B.E)) = =0){ - returnMake_pair (0, res); A } + Else returnMake_pair (1, res); the } - DoubleT = ((S-B.S) ^ (B.S-B.E))/((S-E) ^ (B.S-B.E)); $Res.x + = (e.x-s.x) *T; theRes.y + = (e.y-s.y) *T; the returnMake_pair (2, res); the } the }; - in BOOLInter (line L1, line L2) the { the return AboutMax (l1.s.x, l1.e.x) >= min (l2.s.x, l2.e.x) && theMax (l2.s.x, l2.e.x) >= min (l1.s.x, l1.e.x) && theMax (L1.s.y, l1.e.y) >= min (l2.s.y, l2.e.y) && theMax (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)) <=0&& -SGN ((L1.S-L2.S) ^ (L2.E-L1.S)) * SGN ((L1.E-L2.S) ^ (L2.E-L2.S)) <=0; the }Bayi the DoubleArea (Point A, point B, point C) the { - returnFabs ((1.0/2) * (a.x * (B.Y-C.Y) + b.x * (C.Y-A.Y) + c.x * (A.Y-( b.y) )); - } the the Const intMAXN =2005; the intN; the intA[MAXN], DP[MAXN][MAXN], NUM[MAXN]; -map<int,int>MP; the the intMain () the {94 while(SCANF ("%d", &n)! =EOF) { the intCNT =0; the for(inti =1; I <= N; i++){ thescanf"%d", &a[i]);98Num[i] =A[i]; About } -Sort (num +1, Num +1+n);101CNT = unique (num +1, Num +1+ N)-num-1;102 //cout<<cnt<<endl;103 104Memset (DP, INF,sizeof(DP)); thedp[0][0] =0;106 for(inti =1; I <= N; i++){107 intval = dp[i-1][0];108 for(intj =1; J <= CNT; J + +){109 if(Dp[i-1][J] <val) { theval = dp[i-1][j];111 } theDP[I][J] =val + ABS (a[i)-num[j]);113 } the } the the intAns =inf;117 for(inti =1; I <= CNT; i++){118Ans =min (ans, dp[n][i]);119 } -printf"%d\n", ans);121 }122 return 0;123}
poj3666 Making the Grade "Linear DP"