POJ3666Making the Grade[DP 离散化 LIS相关]
| 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 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
和那道CF#371(div.2)E一样,只是不严格单增单减各一遍
//
// main.cpp
// poj3666
//
// Created by Candy on 9/22/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,INF=1e9+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,a[N],mp[N],k,ans=INF;
int f[N][N];
void dp(){
sort(mp+,mp++n);
for(int i=;i<=n;i++){
int mn=INF;
for(int j=;j<=k;j++){
mn=min(mn,f[i-][j]);
f[i][j]=mn+abs(a[i]-mp[j]);
}
}
for(int j=;j<=k;j++) ans=min(ans,f[n][j]); for(int i=;i<=n;i++){
int mn=INF;
for(int j=k;j>=;j--){
mn=min(mn,f[i-][j]);
f[i][j]=mn+abs(a[i]-mp[j]);
}
}
for(int j=;j<=k;j++) ans=min(ans,f[n][j]);
}
int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
a[i]=mp[i]=read();
}
k=unique(mp+,mp++n)-mp-;
dp();
printf("%d",ans);
return ;
}
POJ3666Making the Grade[DP 离散化 LIS相关]的更多相关文章
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- 【题解】Making The Grade(DP+结论)
[题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...
- CF13C Sequence(DP+离散化)
题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...
- B. Once Again... 解析(思維、DP、LIS、矩陣冪)
Codeforce 582 B. Once Again... 解析(思維.DP.LIS.矩陣冪) 今天我們來看看CF582B 題目連結 題目 給你一個長度為\(n\)的數列\(a\),求\(a\)循環 ...
- POJ3666 Making the Grade [DP,离散化]
题目传送门 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9090 Accepted: ...
- poj 3666 Making the Grade(dp离散化)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7068 Accepted: 3265 ...
- poj3666 Making the Grade(基础dp + 离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
随机推荐
- [deviceone开发]-纳豆项目源码开源
一.简介 已上线的纳豆项目开源.这个App做的非常好,细节处理的很流畅,使用了很多小的动画效果,极力推荐,很多功能可以从这个应用中获取代码和灵感. 二.效果图 三.相关下载 androi ...
- [DeviceOne开发]-地区选择
一.简介 该demo主要通过do_ComboBox和do_Picker的selectChanged事件,实现省市县三级联动的功能 二.效果图 三.源码地址 https://github.com/do- ...
- Android布局优化策略
我们要知道布局是否合理,可以通过Hierarchy Viewer这个工具.打开Hierarchy Viewer(定位到tools/目录下,直接执行hierarchyviewer的命令,选定需要查看的P ...
- XMPP学习——2、用户登录
最近在学习XMPP的使用,打算完成一个完整较为完整地Demo示例,通过这个示例掌握xmpp的使用与开发.同时打算在这个示例中学习使用一下其他的开源类库,在此作为记录学习. 包括服务器端--Openfi ...
- Android--ListView下拉刷新
整理了下以前写的小项目,ListView的下拉刷新,虽然小但还是想纪念下..适合新手看,大神略过... 效果图: 代码: 实体类 package com.example.listviewre ...
- AndroidAnnotations配置--Android studio
1.引入对android-apt的依赖.在全局build.gradle中文件中添加以下代码.(Project目录下的build.gradle) repositories { mavenCentral( ...
- Android java传递int类型数组给C
接着前面的文章<Android java传递int类型数据给C><Android java传递string类型数据给C>,继续实践 实现public native int[] ...
- 网络热恋之NSURLSession
// // ViewController.m // NSURLSession代理简介 #import "ViewController.h" @interface ViewContr ...
- Struts
Struts是基于MVC的框架,它进一步的对MVC进行了封装. MVC 概念 MVC全名是Model View Controller,是模型(model)—视图(view)—控制器(controlle ...
- #一周五# win10通用平台,无处不在的Xamarin,msbuild开源,MVP卢建晖的Asp.NET 5系列 (视频)
又到周五,本周博主的大部分时间都花在深圳了.最近winhec的消息太多了,我只想补充一点,就是winhec时隔7年之后回归,大多数的媒体都还在沿用之前的“硬件工程大会(Hardware Enginee ...