[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 ...
随机推荐
- cf980E TheNumberGames (贪心+倍增)
由于是$2^i$,所以一定要尽量留下来编号大的点 我们干脆就让n号点做树根,它是一定要留的 然后如果要留i的话,i一直到根的路径也都要留.所以只要判断一下够不够把这个路径上还没有留的都留下来 记录下已 ...
- LeetCode 7最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- Windowd系统下Eclipse CDT+MinGW快速搭建C/C++开发环境
安装MinGW后,最简单的配置:Window -> Preferences -> C/C++ -> Build -> Environment添加Path : $PATH;D:\ ...
- C++ new动态数组初始化
strlen函数是不包括‘\0’的长度的,sizeof计算的结果才包括'\0'的长度: C++ new动态数组初始化void testnew( const char* str ) { if (!str ...
- Python常用模块-摘要算法(hashlib)
Python常用模块-摘要算法(hashlib) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MD5算法参数详解 1.十六进制md5算法摘要 #!/usr/bin/env p ...
- swift学习笔记3
1.在 Swift 中,枚举类型是一等(first-class)类型.它们采用了很多在传统上只被类(class)所支持的特性,例如计算型属性(computed properties),用于提供枚举值的 ...
- Tomcat——Linux下的安装和配置
Tomcat在Linux上的安装与配置 以下使用的Linux版本为: Redhat Enterprise Linux 7.0 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JD ...
- [python]python三元表达式另类实现方式
() variable = a if exper else b ()variable = (exper and [b] or [c])[] () variable = exper and b or c
- CF989C A Mist of Florescence (构造)
CF989C A Mist of Florescence solution: 作为一道构造题,这题确实十分符合构造的一些通性----(我们需要找到一些规律,然后无脑循环).个人认为这题规律很巧妙也很典 ...
- JAVA中Collection接口和Map接口的主要实现类
Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...