UVa 1471 Defense Line 防线
来源:互联网 发布:linux文件内容复制 编辑:程序博客网 时间:2024/05/01 17:00
前面的博文有写过LIS O(nlogn)的做法,今天再复习一下。
戳这里->http://blog.csdn.net/x_1023/article/details/70259967
首先考虑一个潜能数组c的作用:
c[i]表示长度为i的最优上升子序列的末尾值(即末尾值的最小值,这样决策一定最优,因前面部分没有后效性)。
于是每次统计答案并更新c数组即可,以下为一个更方便写的板子,运用了lower_bound返回不小于值的特性和INF,写法更简洁:
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<algorithm>using namespace std;const int maxn=5005;int n,s[maxn],c[maxn];int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",s+i); int len=0; memset(c+1,0x3f,sizeof(int)*n); for(int i=1;i<=n;i++) { int pos=lower_bound(c+1,c+n+1,s[i])-c; c[pos]=s[i]; } printf("%d",lower_bound(c+1,c+n+1,0x3f3f3f3f)-c-1); return 0;}
考虑这道题,刘汝佳上的方法太过于麻烦,类似于一个单调队列的思想,不过是二维所以需要set胡搞。
简洁的做法可以利用LIS的思想。
首先扫两遍,计算出每个节点向前和向后能延伸的最大长度,然后将前节点的向前长度和后节点向后长度一拼。朴素做法是枚举两个节点,为
枚举前后节点且枚举无后效性的题目用LIS思想有奇效。
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<algorithm>using namespace std;const int maxn=200005;int n,ans;int s[maxn],c[maxn],l[maxn],r[maxn];int main(){ int T;cin>>T; while(T--) { ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",s+i); l[0]=r[n+1]=0; for(int i=1;i<=n;i++) { if(s[i-1]<s[i])l[i]=l[i-1]+1; else l[i]=1; } for(int i=n;i;i--) { if(s[i+1]>s[i])r[i]=r[i+1]+1; else r[i]=1; } memset(c+1,0x3f,sizeof(int)*n); for(int i=1;i<=n;i++) { int len=lower_bound(c+1,c+n+1,s[i])-c; ans=max(ans,len-1+r[i]); c[l[i]]=min(c[l[i]],s[i]); } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- UVa 1471 Defense Line 防线
- UVA - 1471 Defense Lines 防线
- 防线Defense Lines
- uva 1471Defense Lines
- uva 1471 Defense Lines
- UVA 1471 Defense Lines
- UVA 1471 Defense Lines
- UVA - 1471 Defense Lines
- UVa 1471 防线 set/LIS
- 高效(防线,uva 1471)
- uva 1471 - Defense Lines(高效)
- UVA 1471(p242)----Defense Lines
- UVa OJ 1471 - Defense Lines
- uva 1471 防线(defences lines)
- Uva 1471 Defense Lines(线段树)
- UVA 1471 - Defense Lines(扫描+二分)
- UVA 1471 Defense Lines 单调队列优化
- uva 1471 Defense Lines (降低复杂度)
- shiro自定义realm无法使用注解
- Educational Codeforces Round 30 Balanced Substring 前缀和
- [Molar Mass, ACM/ICPC Seoul 2007, UVA1586]
- 矩阵乘法(Strassen 算法实现)
- 设计全局ER模型 数据库系统原理(2007版) 课程代码4735 笔记
- UVa 1471 Defense Line 防线
- 二叉树-中序遍历
- Spring框架的注释问题
- Java基础之逐步击破----九九乘法表
- 网站图片应如何SEO优化?
- SpringMVC单文件文件上传
- 网络初接触
- 计蒜客 加减乘除 带括号(栈实现和二叉树两种实现)
- hdu1062