USACO Making the Grade
洛谷 P2893 [USACO08FEB]修路Making the Grade
https://www.luogu.org/problemnew/show/P2893
JDOJ 2566: USACO 2008 Feb Gold 1.Making the Grade
https://neooj.com:8082/oldoj/problem.php?id=2566
POJ Making the Grade
http://poj.org/problem?id=3666
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 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
1
3
2
4
5
3
9
Sample Output
HINT
OUTPUT DETAILS:
By changing the first 3 to 2 and the second 3 to 5 for a total cost of
|2-3|+|5-3| = 3 we get the nondecreasing sequence 1,2,2,4,5,5,9.
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,ans,a[],t[],b[];
int f[][],minf[][];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
t[i]=a[i];
}
sort(t+,t+n+);
int now=-;
for(int i=;i<=n;i++)
if(now!=t[i])
b[++m]=t[i],now=t[i];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
f[i][j]=minf[i-][j]+abs(a[i]-b[j]);
if(j==)
minf[i][j]=f[i][j];
else
minf[i][j]=min(minf[i][j-],f[i][j]);
}
ans=minf[n][m];
memset(f,,sizeof(f));
memset(minf,,sizeof(minf));
sort(b+,b+m+,cmp);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
f[i][j]=minf[i-][j]+abs(a[i]-b[j]);
if(j==)
minf[i][j]=f[i][j];
else
minf[i][j]=min(minf[i][j-],f[i][j]);
}
ans=min(ans,minf[n][m]);
printf("%d",ans);
return ;
}
USACO Making the Grade的更多相关文章
- POJ3666Making the Grade[DP 离散化 LIS相关]
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 2994 ...
- A-Making the Grade(POJ 3666)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4656 Accepted: 2206 ...
- poj 3666 Making the Grade(dp)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- POJ 3666 Making the Grade (动态规划)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- poj3666 Making the grade【线性dp】
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions:10187 Accepted: 4724 ...
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- [poj 3666] Making the Grade (离散化 线性dp)
今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
随机推荐
- ubuntu16 安装Configuring ttf-mscorefonts-installer,无法按到ok
作为Linux小白,完全不知道出现类似图形化界面按不到按钮到情况,解决方法非常简单 使用Tab键选择按钮
- CF-1155 D.Beautiful Array
题目大意:现在有一个数列,还有一个数字x,你可以将这个数列中的一段连续子序列同时乘以这个数字x(当然也可以不乘),然后问你最大子段和是多少 做法:dp,你懂的 #include<iostream ...
- Comet OJ CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)
Preface 在一个月黑风高的夜晚我这个蒟蒻正踌躇着打什么比赛好 是继续做一场AGC,还是去刷一场CF 然后,一道金光闪过(滑稽),我们的红太阳bzt给我指明了方向: 你太菜了,我知道有一场很水的比 ...
- 网络流 之 dinic 算法
网络流指的是:网络流(network-flows)是一种类比水流的解决问题方法.(类似于水管群,有一个源点(水无限多),和一个汇点,最大流就代表这个点水管群(边集)每秒最大能送道汇点的水量) 这个怎么 ...
- Rust从入门到放弃(1)—— hello,world
安装及环境配置 特点:安全,性能,并发 rust源配置 RLS安装 cargo rust管理工具,该工具可以愉快方便的管理rust工程 #!/bin/bash mkdir learn cd learn ...
- java函数式编程的形式
java中没有真正的函数变量: 一.所有的函数(拉姆达)表达式,都被解释为functional interface @FunctionalInterface interface GreetingSer ...
- 【04】Nginx:rewrite / if / return / set 和变量
写在前面的话 我们前面已经谈了编译安装,基本语法,日志处理,location 匹配,root / alias 的不同效果.这里我们主要谈谈 rewrite(重写)功能,顺便说说 nginx 中自带的变 ...
- 对try catch finally的理解
对try catch finally的理解1.finally 总是会运行的,即使在catch中thorw抛出异常了. 2.finally 在 return后没有结束,而是继续运行finally 2. ...
- Postman 调试请求Asp.Net Core3.0 WebApi几种常见的Get/Post/Put/Delete请求
这里就直接截图了,如下(很简单的操作): 1:Get几种请求 2:Post 3:Put 4:Delete 最后,虽然简单,代码还是给放一下(这里只是抛砖引玉的作用,自己可以根据自身的业务需要来做进一 ...
- C 数组、枚举类型enum
传递数组给函数 告诉编译器函数要接受一个指针 skip //函数声明,数组的长度无需声明,因为编译器不会对形式参数进行边界检查 void myFunction(int param[]) //或者 vo ...