NOIP2013 花匠 DP 线段树优化
网上一堆题解,我写的是N^2优化的那种,nlogn,O(n)的那种能看懂,但是让我自己在赛场写,肯定没戏了
#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+;
int a[N],b[N],n,cnt;
int mx[][N<<];
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)>>;
if(pos<=m)add(rt<<,l,m,pos,t);
else add(rt<<|,m+,r,pos,t);
mx[now][rt]=max(mx[now][rt<<],mx[now][rt<<|]);
}
int query(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)
return mx[now][rt];
int ans=;
int m=(l+r)>>;
if(x<=m)ans=max(ans,query(rt<<,l,m,x,y));
if(y>m)ans=max(ans,query(rt<<|,m+,r,x,y));
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
cnt=unique(b+,b++n)-b-;
int pos=lower_bound(b+,b++cnt,a[])-b;
int ans=;
now=;
add(,,cnt,pos,);
now=;
add(,,cnt,pos,);
for(int i=;i<=n;++i){
pos=lower_bound(b+,b++cnt,a[i])-b;
if(pos!=){
now=;
int tmp=query(,,cnt,,pos-)+;
ans=max(ans,tmp);
now=;
add(,,cnt,pos,tmp);
}
if(pos!=cnt){
now=;
int tmp=query(,,cnt,pos+,cnt)+;
ans=max(ans,tmp);
now=;
add(,,cnt,pos,tmp);
}
}
printf("%d\n",ans);
return ;
}
NOIP2013 花匠 DP 线段树优化的更多相关文章
- [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)
题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...
- HDU4719-Oh My Holy FFF(DP线段树优化)
Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) T ...
- UVA-1322 Minimizing Maximizer (DP+线段树优化)
题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...
- zoj 3349 dp + 线段树优化
题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...
- 完美字符子串 单调队列预处理+DP线段树优化
题意:有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(注意不是子序列),使得该子串不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个数.如果你的子串是最长的,那 ...
- Contest20140906 ProblemA dp+线段树优化
Problem A 内存限制 256MB 时间限制 5S 程序文件名 A.pas/A.c/A.cpp 输入文件 A.in 输出文件 A.out 你有一片荒地,为了方便讨论,我们将这片荒地看成一条直线, ...
- POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4721 Accepted: 1593 D ...
- 题解 HDU 3698 Let the light guide us Dp + 线段树优化
http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...
- 省选模拟赛 4.26 T1 dp 线段树优化dp
LINK:T1 算是一道中档题 考试的时候脑残了 不仅没写优化 连暴力都打挂了. 容易发现一个性质 那就是同一格子不会被两种以上的颜色染.(颜色就三种. 通过这个性质就可以进行dp了.先按照左端点排序 ...
随机推荐
- linux修改时间 时区
查看时区:date -R 修改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.时间 1.查看时间和日期 date 2.设置时间和日期 ...
- jQuery中模拟用户操作
有时为了节省不想手动操作网页,但又想看到用户操作时的效果,可以用到jQuery提供的trigger方法.见下图代码 在不点击按钮时仍然想弹出this.value 我们只需要在后面加上.trigger( ...
- 遍历 DataSet
DataSet ds=new DataSet ; //获取dataset的第一张table,取其他table只须改下标 DataTable dt=ds.tables[]; //遍历行 foreach( ...
- php eval函数用法总结
可以在php.ini禁止此函数,结果失败了 eval定义和用法 eval() 函数把字符串按照 PHP 代码来计算. 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串 ...
- JavaScript 判断用户输入的邮箱及手机格式是否正确
JavaScript判断用户输入的邮箱格式是否正确.判断用户输入的手机号格式是否正确,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: /* * 功能:判断用户输入的邮箱格式是否正确 ...
- PHP学习笔记(5) - 选择一个合格的框架
作为一个合格的PHP开发框架,个人觉得起码需要满足以下几点. 一.上梁不正下梁歪,好的框架首先自身要有良好的编码规范和文件目录结构,代码要易于阅读理解. 二.为了可以更好地适应OOP,框架必须可以提供 ...
- 004.CleanUp方法
定义: Delphi procedure CleanUp; 类型 可见性 所在源码 所在单元 父类 procedure protected System.RegularExpressionsCore. ...
- map遍历的三种基础用法
java中遍历MAP的几种方法 Java代码 Map<String,String> map=new HashMap<String,String>(); map.put(& ...
- WPF 控件
****ScrollViewer**** 前台: <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalSc ...
- C#中的socket编程方法
服务器: 第一步,准备socket套接字:Socket sockfd = new Socket(AddressFamily.InterNetwork,SocketType.Stream,Protoco ...