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 ...
随机推荐
- Luogu P5363 [SDOI2019]移动金币
话说这题放在智推里好久了的说,再不写掉对不起自己233 首先你要知道一个叫做阶梯Nim的东西,具体的可以看这篇博客 那么我们发现这和这道题的关系就很明显了,我们把两个金币之间的距离看作阶梯Nim的每一 ...
- 【新特性速递】CSS3动画增强
FineUIPro/Mvc/Core的下个版本(v6.1.0),我们对多个地方的CSS3动画进行了增强,使得用户体验更好. 1. 树控件启用EnableSingleExpand时,使得展开动画和折叠其 ...
- 如何把任意网站制作成RSS
如何把任意网站制作成RSS 参照一下链接,多试几次就掌握了. 参考链接:https://feed43.com/step-by-step.html
- python接口自动化4-常用取token值方法
前言 在接口测试中我们经常是需要一个登陆token,或者获取其他用到的参数来关联下一个接口用到的参数.这里介绍一些本人常用的方法. 一.简介 不过在哪里我们也是能实现自动化api测试的,我们都知道to ...
- python在字节流中对int24的转换
python在字节流中对int24的转换 概述 最近在写项目的过程中,需要对从串口中读取的数据进行处理,本来用C写完了,但是却一直拿不到正确的数据包,可能是因为自己太菜了.后来用了python重新写了 ...
- Go gRPC Hello World
概述 开始 gRPC 了,这篇文章学习使用 gRPC,输出一个 Hello World. 用 Go 实现 gRPC 的服务端. 用 Go 实现 gRPC 的客户端. gRPC 支持 4 类服务方法,咱 ...
- EasyUIDataGrid列标题换行显示
有时候表格标题字数太多,而宽度有限,就会导致一部分列的标题显示不出来 这时候,加入如下css代码即可将标题换行显示 .datagrid-header-row .datagrid-cell span { ...
- laravel模型中非静态方法也能静态调用的原理
刚开始用laravel模型时,为了方便一直写静态方法,进行数据库操作. <?php namespace App\Models; use Illuminate\Database\Eloquent\ ...
- 解决 Visual Studio 符号加载不完全问题
解决 Visual Studio 符号加载不完全问题 工具 - 选项 - 搜索 "符号" - 选上服务器 | 加载所有符号, 之后符号就会显示完全
- 2019-11-25-win10-uwp-发布旁加载自动更新
原文:2019-11-25-win10-uwp-发布旁加载自动更新 title author date CreateTime categories win10 uwp 发布旁加载自动更新 lindex ...