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 ...
随机推荐
- appium--python启动appium服务
前戏 前面我们都是在cmd下通过输入appium加端口号来启动服务的,在我们做自动化的时候,我们当然不希望我们手动启动appium服务,而是希望通过脚本自动启动appium服务. 我们可以使用subp ...
- 安装Microsoft Office 2010 提示需要安装MSXML版本6.10.1129.0 的解决方案
1.先按照https://blog.csdn.net/c_lanxiaofang/article/details/80410868进行处理安装 2.安装vison2010,如果还存在提示未安装MSXM ...
- Paper | Feedback Networks
目录 读后总结 动机 故事 ConvLSTM图像分类网络 损失函数 与Episodic Curriculum Learning的结合 实验方法 发表在2017年CVPR. 读后总结 这篇论文旨在说明: ...
- 再谈Token认证,如何快速方便获取用户信息
前面我写了一篇<Token认证,如何快速方便获取用户信息>的文章,引起了各位读者的积极参与,除了文章中我提出的三种方式,各位读者大佬们也贡献了其他多种实现方式. 今天决定基于大家提供的思路 ...
- 公式推导【BACF//ICCV2017】
HK Galoogahi, A Fagg, S Lucey. Learning Background-Aware Correlation Filters for Visual Tracking[C]. ...
- ubuntu 安装rocketmq
RocketMQ环境要求 ) 64bit OS,linux/Unix/Max ) 64bit JDK 1.8+ ) Maven 3.2.x ) Git 一.下载并构建 git clone https: ...
- python中easydict的简单使用
easydict的作用:EasyDict可以使得以属性的方式去访问字典的值! 1. 实例1:获取字典的值 2. 实例2: 设置属性 3. 在深度学习中往往利用easydict建立一个全局的变量
- python threading ThreadPoolExecutor源码解析
future: 未来对象,或task的返回容器 1. 当submit后: def submit(self, fn, *args, **kwargs): with self._shutdown_lock ...
- Java生鲜电商平台-积分,优惠券,会员折扣,签到、预售、拼团、砍价、秒杀及抽奖等促销模块架构设计
Java生鲜电商平台-积分,优惠券,会员折扣,签到.预售.拼团.砍价.秒杀及抽奖等促销模块架构设计 说明:本标题列举了所有目前社会上常见的促销方案,目前贴出实际的业务运营手段以及架构设计,包括业务说明 ...
- ANDROID培训准备资料之Service
在讨论Service 之前,我们需要了解两点,非常重要的两点 (1) Service 不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中 (2) Service也不 ...