NOIP2013 花匠 DP 线段树优化
2016-04-11 14:06
288 查看
网上一堆题解,我写的是N^2优化的那种,nlogn,O(n)的那种能看懂,但是让我自己在赛场写,肯定没戏了
View Code
#include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=1e5+5; int a ,b ,n,cnt; int mx[2][N<<2]; int now; void add(int rt,int l,int r,int pos,int t){ if(l==r){ mx[now][rt]=max(mx[now][rt],t); return; } int m=(l+r)>>1; if(pos<=m)add(rt<<1,l,m,pos,t); else add(rt<<1|1,m+1,r,pos,t); mx[now][rt]=max(mx[now][rt<<1],mx[now][rt<<1|1]); } int query(int rt,int l,int r,int x,int y){ if(x<=l&&r<=y) return mx[now][rt]; int ans=0; int m=(l+r)>>1; if(x<=m)ans=max(ans,query(rt<<1,l,m,x,y)); if(y>m)ans=max(ans,query(rt<<1|1,m+1,r,x,y)); return ans; } int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+1+n); cnt=unique(b+1,b+1+n)-b-1; int pos=lower_bound(b+1,b+1+cnt,a[1])-b; int ans=1; now=0; add(1,1,cnt,pos,1); now=1; add(1,1,cnt,pos,1); for(int i=2;i<=n;++i){ pos=lower_bound(b+1,b+1+cnt,a[i])-b; if(pos!=1){ now=0; int tmp=query(1,1,cnt,1,pos-1)+1; ans=max(ans,tmp); now=1; add(1,1,cnt,pos,tmp); } if(pos!=cnt){ now=1; int tmp=query(1,1,cnt,pos+1,cnt)+1; ans=max(ans,tmp); now=0; add(1,1,cnt,pos,tmp); } } printf("%d\n",ans); return 0; }
View Code
相关文章推荐
- Java之浮点四则运算工具类-(double/float转化为BigDecimal)
- 原来zabbix监控进程与端口是如此的简单!
- 虚拟机centos6.5 下 FastDFS 5.05 分布式存储集群安装配置
- NOI2005 : 聪聪和可可
- 309. Best Time to Buy and Sell Stock with Cooldown
- maertSataDmorfnaideMdniF.295
- sql 用户角色
- netbeans for php常用快捷键
- 案例分析:基于消息的分布式架构
- 架构漫谈(三):如何做好架构之识别问题
- 选择排序法
- Axis2调用WebService
- 【js】:JS获得标签名的方法
- bootstrap 划分12列
- Android hotfix 方案
- Qt之等待提示框(QMovie播放gif图片,等待文字变化等)
- 使用Apache Ant编译打包运行Java项目
- OC 和swift混编网址
- 架构漫谈(二):认识概念是理解架构的基础
- 架构漫谈(一):什么是架构?