• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
井_中窥月
万物美好,我在中央
博客园    首页    新随笔    联系   管理    订阅  订阅
A-Making the Grade(POJ 3666)
Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4656   Accepted: 2206

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

USACO 2008 February Gold

经典dp

dp[i][j]表示第i个数变成第j小的数的最小消耗。

dp[i][j] = dmin[i - 1][j] + abs(o[i] - d[j]) (ps : dmin[i - 1][j] 表示前i - 1个数并且第i - 1个数小于等于j的最小消耗, abs(o[i] - d[j]) 表示第i个数变成j的消耗);

数据较水,只保证是上升序列即可。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 2005
ll dp[MAXN][MAXN];
ll dmin[MAXN][MAXN];
ll d[MAXN], o[MAXN];
int main()
{
   int n;
   scanf("%d", &n);
   repu(i, 1, n + 1) { scanf("%I64d", &d[i]); o[i] = d[i]; }
   sort(d + 1, d + n + 1);
   int l = 1;
   repu(i, 2, n + 1) if(d[i] != d[l]) d[++l] = d[i];

   repu(i, 1, l + 1) {
       dp[1][i] = (ll)abs((int)o[1] - (int)d[i]);
       if(i == 1) dmin[1][i] = dp[1][i];
       else dmin[1][i] = min(dp[1][i], dmin[1][i - 1]);
   }
   ll t = 0;
   repu(i, 2, n + 1) {
     dp[i][1] = dp[i - 1][1] + (ll)abs((int)o[i] - (int)d[1]);
     dmin[i][1] = dp[i][1];
     repu(j, 2, l + 1) {
        dp[i][j] = dmin[i - 1][j] + (ll)abs((int)o[i] - (int)d[j]);
        dmin[i][j] = min(dp[i][j], dmin[i][j - 1]);
     }
   }
   printf("%I64d\n", dmin[n][l]);
   return 0;
}
View Code

 

posted on 2015-05-08 13:57  井_中窥月  阅读(273)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3