[USACO08FEB]修路Making the Grade
[USACO08FEB]修路Making the Grade
比较难的dp,比赛时打的找LIS,然后其他的尽可能靠近,40分。
先举个例子
6
1 2 3 1 4 5
6
1 2 3 3 4 5
第4个1要么改成3,要么改成4,反正是数列中的数。
所以最优情况下,答案中的数都是原数列中有的。
b[]是a[]由小到大排序之后的数组
令f[i][j]表示使前i个数成为不减的最小花费,而且第i个的高度为b[j].
f[i][j]=min(f[i-1][k])+abs(a[i]-b[j]);1<=k<=n
k从1~n递增,一个显然的优化就是单调队列,递增的,每次取队首。
不增同理。
AC:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int f[][];
int q[];
int a[];
int b[];
int l,r;
int ans;
int n;
int m;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
in(n);
For(i,,n)
in(a[i]),b[i]=a[i];
sort(b+,b+n+);
m=unique(b+,b+n+)-b-;
For(i,,n)
{
r=;
For(j,,m)
{
while(r&&f[i-][j]<=f[i-][q[r]])r--;
q[++r]=j;
f[i][j]=f[i-][q[]]+abs(a[i]-b[j]);
}
}
ans=inf;
For(i,,m)
ans=min(ans,f[n][i]);
For(i,,n)
For(j,,n)
f[i][j]=;
For(i,,n)
{
r=;
For(j,,m)
{
while(l<=r&&f[i-][j]>=f[i-][q[r]])r--;
q[++r]=j;
f[i][j]=f[i-][q[]]+abs(a[i]-b[m]);
}
}
ans=min(ans,f[n][m]);
o(ans);
return ;
}
考场贪心骗分代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int n;
int Max;
int last;
int a[];
int d[];
bool b[];
int p[];
int f[];
int ans[];
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void LIS()
{
For(i,,n)
f[i]=,d[i]=;
For(i,,n)
{
For(j,,i-)
{
if(a[j]<=a[i])
{
if(f[j]+>f[i])
{
f[i]=f[j]+;
d[i]=j;
}
}
}
if(Max<f[i])
{
Max=f[i];
last=i;
}
}
int ft;
for(ft=last;d[ft]!=;ft=d[ft])
b[ft]=true;
for(int i=ft-;i>=;i--)
{
ans[]+=abs(p[i]-p[i+]);
p[i]=p[i+];
}
For(i,ft+,n)
if(!b[i])
{
ans[]+=abs(p[i]-p[i-]);
p[i]=p[i-];
}
} void LRS()
{
Max=;
For(i,,n)
f[i]=,b[i]=false,d[i]=;
For(i,,n)
{
For(j,,i-)
{
if(a[j]>=a[i])
{
if(f[j]+>f[i])
{
f[i]=f[j]+;
d[i]=j;
}
}
}
if(Max<f[i])
{
Max=f[i];
last=i;
}
}
int ft;
for(ft=last;d[ft]!=;ft=d[ft])
b[ft]=true;
for(int i=ft-;i>=;i--)
{
ans[]+=abs(p[i]-p[i+]);
p[i]=p[i+];
}
For(i,ft+,n)
if(!b[i])
{
ans[]+=abs(p[i]-p[i-]);
p[i]=p[i-];
}
} int main()
{
// freopen("grading.in","r",stdin);
// freopen("grading.out","w",stdout);
in(n);
For(i,,n)
in(a[i]),p[i]=a[i];
LIS();
For(i,,n)
p[i]=a[i];
LRS();
o(min(ans[],ans[]));
return ;
}
[USACO08FEB]修路Making the Grade的更多相关文章
- 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告
P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...
- 【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解
正常的没想到的DP和玄学贪心. 题目描述 A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
- luogu2893 [USACO08FEB]修路Making the Grade
ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...
- [USACO08FEB]修路Making the Grade 动态规划
对的\(n^3\)的程序调了一个月了,惊了... HSZ学oi\(\Longleftrightarrow\)闭眼学oi 要不是翻旧账还看不见.. 这是有\(n^2\)做法的. 参见LYD的书P244 ...
- P2893 [USACO08FEB]修路
直入主题. 农夫约翰想改造一条路,原来的路的每一段海拔是Ai,修理后是Bi花费|A_i–B_i|.我们要求修好的路是单调不升或者单调不降的.求最小花费. 数据范围:n<=2000,0≤ Ai ≤ ...
- 【贪心】bzoj1592: [Usaco2008 Feb]Making the Grade 路面修整
贪心的经典套路:替换思想:有点抽象 Description FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也 就是说,高度上升与高度下降的路段不能 ...
- USACO Making the Grade
洛谷 P2893 [USACO08FEB]修路Making the Grade https://www.luogu.org/problemnew/show/P2893 JDOJ 2566: USACO ...
- BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )
最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 ...
- 1592: [Usaco2008 Feb]Making the Grade 路面修整
1592: [Usaco2008 Feb]Making the Grade 路面修整 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 428 Solv ...
随机推荐
- debian9使用systemd部署etcd集群
在centos上,是可以直接使用yum安装etcd的: # yum list | grep etcd etcd.x86_64 3.2.9-3.el7 @extras 但是,在debian上却没有安装包 ...
- C++并发编程之std::future
简单地说,std::future 可以用来获取异步任务的结果,因此可以把它当成一种简单的线程间同步的手段.std::future 通常由某个 Provider 创建,你可以把 Provider 想象成 ...
- MySQL-->高级-->001-->MySQL备份与恢复测试
- python数据分析美国大选项目实战(三)
项目介绍 项目地址:https://www.kaggle.com/fivethirtyeight/2016-election-polls 包含了2015年11月至2016年11月期间对于2016美国大 ...
- python+正态分布+蒙特卡洛预测男女身高概率!
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- jQuery 实现 bootstrap 模态框 删除确认
思路: 点击删除按钮,通过jquery将删除操作的URL赋值到页面URL元素,并弹出会话框 用户点击确认,通过jquery获取URL,并发送删除请求至后台. 一.删除button <a clas ...
- C#的Struct
- Linux 基础知识(一) shell的&&和|| 简单使用
shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? == 1 时,表示执行失败. 有时候,下一条命令依赖前 ...
- li分两列显示
只要控制了li的宽度,利用浮动就能实现<style type="text/css"> .my ul { width: 210px; } .my li { width: ...
- [转载]Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)
http://brackets.io/ Brackets 是一个免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境 (IDE工具).该项目由 Adobe 创建和维 ...