洛谷 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

7
1
3
2
4
5
3
9

Sample Output

3

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.

 
题目大意:
给定长度为N的序列A,构造一个长度为N 的序列B。
要求B非严格单调,并最小化花费(Ai-Bi的绝对值)
 
题意分析:
马上想到动归。
思路不是特别好想,状态转移方程也不是特别好设。
首先我们需要明确,在满足花费最小化的前提下,一定存在一种构造B的方案,使得B中的每个数都是A序列中的。
可以证明:
假设结论针对于N=K-1成立,那么对于数列N=K,在满足单调性的情况下,可以令Bk=Ak,命题仍成立。
否则的话,令Bk=Bk-1,命题也成立,也就是说可以层层递推下去,一直到N=1的情况。
而显然N=1的情况下命题是成立的。
证毕,命题成立。
我们只是证明这种情况(B中所有元素都是A的一部分)存不存在,并不是在证明只要存在就一定是这种情况。
所以才有了上面的证明过程,证明出这种情况可以存在,为之后的解题过程提供了思路基础。
回到本题。
这道题是构造类型的动态规划。我的思路是,设F[I][J]为完成前i个数的构造,其中Bi=j时,S的最小值。
根据刚刚证明的命题,我们可以考虑把A的数据离散化之后存到B中,离散化的功用是降低时间复杂度,这样一个O(N3)的算法就会被我们降成O(N2)。
这样就比较完美了,通过动归求出非严格单调递减之后,仿照这个思路再求一遍非严格单调递增,答案可求。
 
AC CODE:
#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的更多相关文章

  1. POJ3666Making the Grade[DP 离散化 LIS相关]

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 2994 ...

  2. A-Making the Grade(POJ 3666)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4656   Accepted: 2206 ...

  3. poj 3666 Making the Grade(dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  5. POJ 3666 Making the Grade (动态规划)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  6. poj3666 Making the grade【线性dp】

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:10187   Accepted: 4724 ...

  7. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. [poj 3666] Making the Grade (离散化 线性dp)

    今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...

  9. 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 ...

随机推荐

  1. C++ TCP客户端网络消息发送接收同步实现

    废话不多说, 直入主题, 我们在写客户单的时候希望在哪里发消息出去,然后在哪里返回消息(同步), 然后继续往下运行-, 而不是在这里发送了一个消息给服务端, 在另一个地方接受消息(异步) , 也不知道 ...

  2. ubuntu串口连接linux车机设备

    一.用到的命令或者程序 1.dmesg命令 2.minicom软件 二.开搞 1.安装minicom sudo apt-get install minicom 2.查看串口信息 dmesg | gre ...

  3. 某企业用友U8+中勒索病毒后数据修复及重新实施过程记录

    近期某客户中了勒索病毒,虽然前期多次提醒客户注意异地备份,但始终未执行,导致悲剧. 经过几天的努力,该客户信息系统已基本恢复正常运行,现将相关过程记录如下,作为警示. 方案抉择 交赎金解密:风险过高, ...

  4. 大话设计模式Python实现-备忘录模式

    备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态 下面是一个备忘录模式的demo: #! ...

  5. Unity C# CSV文件解析与加载(已更新移动端处理方式)

    在游戏开发过程中,经常要用到Excel编辑各类数据,如果可以直接用Excel支持的文件格式来读取数据,修改将非常便捷. Excel支持导出CSV类型的文件,这类文件不仅可以用Excel直接打开修改,即 ...

  6. oracle排序子句的特殊写法与ORA-01785错误

    刚刚写的SQL语句在执行的时候报[ORA-01785: ORDER BY item must be the number of a SELECT-list expression]错误,于是自己百度了一 ...

  7. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(二)

    接着上一篇 直接贴代码了: using System; using System.Collections.Generic; using System.Data.Entity; using System ...

  8. MySQL优化常见Extra分析——慢查询优化

    数据准备: create table user ( id int primary key, name ), sex ), index(name) )engine=innodb; 数据说明:用户表:id ...

  9. [Zabbix] 安装MySQL5.7, 部署Zabbix到CentOS 7日记

    安装环境:CentOS7 64位,安装MySQL5.7 一.安装 MySQL 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads ...

  10. Java面试题:Java中的集合及其继承关系

    关于集合的体系是每个人都应该烂熟于心的,尤其是对我们经常使用的List,Map的原理更该如此.这里我们看这张图即可: 1.List.Set.Map是否继承自Collection接口? List.Set ...