Making the Grade
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4647 | Accepted: 2202 |
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 A_{1}, … , A_{N} (1 ≤
N ≤ 2,000) describing the elevation (0 ≤ A_{i} ≤
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 B_{1}, . … ,
B_{N} 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
|A_{1} – B_{1}| + |A_{2} –
B_{2}| + … + |A_{N} – B_{N} |
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 钱柜999：动态规划。i+1 contains a single integer elevation:
A_{i}
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[i],枚举大肆气风发可观作为最大中度，取前i-1个的合法最优解。
多少范围不小10^9，但n唯有二〇〇一朗朗上口，能够离散化，用坐标代替高度。
a[i]存原始数组，b[j]存排序后依次增加的数组。
dp[i][j]=min(dp[i-1][0..j])+abs(a[i]-b[j]); （把第i
个数中度改为b[j],当时的非常小费用。卡塔尔
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll __int64
#define mem(a,t) memset(a,t,sizeof(a))
#define N 2005
const int M=305;
const int inf=0x7fffffff;
int a[N],b[N];
int dp[N];
void solve(int n)
{
int i,j,tmp;
sort(b,b+n);
for(i=0;i
3666 Making the Grade (动态规划) Making the
Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4647
Accepted: 2202 Description A straight dirt road connects t…
Input
必发88手机版 ，Sample Input
|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.
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).
3