Codeforces Round #345 Div.1 D.Zip-line 动态最长上升子序列
题意概述:
给出一个长度为N的序列和M组询问,问假设把某个位置的值改成另一个给出的值之后,序列的最长上升子序列的长度。
N,M<=400000.
分析:
考虑某个位置的值改动后这个位置和最长上升子序列(lis)的关系:
1、这个位置包含在lis中(这种情况答案可能+1,可计算经过这个点的lis来等效决策)。
2、这个位置不包含在lis中,那么需要看是否任意的lis都经过这个位置。如果是的话此决策的结果在原来长度基础上-1,否则就等于原来的长度。
有了大体思路,接下来想想维护。
任务1:对于任意位置x,求包含这个点的lis长度,令g1(i)表示1->N方向长度为i的lis的最长上升子序列的最后一个值的最小值,g2(i)表示N->1方向长度为i的最长下降子序列的最后一个值的最大值(更新就不说了,基础),如果知道位置x状态下恰好未更新的g1,g2数组内容,就同更新一样用前后以这个点结尾的最长序列长度相加-1就是此决策的结果。
任务2:这个实际上是重点,对于任意位置x,知道的东西和任务1一样,同时知道是否存在一对g1(a),g2(b)满足a+b=len(len是原序列lis长度)&&g1(a)<g2(b),如果存在那么此决策的结果为原来答案,否则为原来答案-1。
如果要强行在线的话三棵主席树维护一下两个g数组和一个vis数组(vis(x)表示对于当前状态来说g1(x),g2(len-x)是否合法,当前状态是否合法实际上就是看vis的所有值的和是否不为0)然后随便乱搞就可以了。
然而。。。我选择离线,询问按照位置排序,正反扫一遍序列处理第一种情况,然后再正着扫一遍把第二种情况用一个数组处理了就可以了。能离线何苦去在线码主席树呢(手动滑稽)(手动链表或者vector都可以帮忙离线)?!!
然而我还是弄了半天,污浊的机房CO2,QAQ
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=; int N,M,h[maxn];
struct que{
int id,pos,v;
friend bool operator < (que a,que b){
return a.pos<b.pos;
}
}q[maxn];
int g1[maxn],g2[maxn],cnt1,cnt2,ans[maxn],len,vis[maxn];
struct mlink{
static const int max_sz=;
int np,w[maxn],first[maxn],next[maxn];
mlink(){
np=,w[]=next[]=;
memset(first,,sizeof(first));
}
void ins(int i,int x) { w[++np]=x,next[np]=first[i],first[i]=np; }
void del(int i) { first[i]=next[first[i]]; }
int val(int i) { return w[first[i]]; }
}dd; void data_in()
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++) scanf("%d",&h[i]);
for(int i=;i<=M;i++){
scanf("%d%d",&q[i].pos,&q[i].v);
q[i].id=i;
}
sort(q+,q+M+);
}
bool cmp(int x,int y) { return x>y; }
void work()
{
for(int i=;i<=N;i++){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]=lower_bound(g1+,g1+cnt1+,q[j].v)-g1,j++;
int x=lower_bound(g1+,g1+cnt1+,h[i])-g1;
if(x>cnt1) cnt1=x;
g1[x]=h[i];
}
for(int i=N;i>=;i--){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]+=lower_bound(g2+,g2+cnt2+,q[j].v,cmp)-g2-,j++;
int x=lower_bound(g2+,g2+cnt2+,h[i],cmp)-g2;
if(x>cnt2) cnt2=x;
g2[x]=h[i];
dd.ins(x,g2[x]);
}
memset(g1,,sizeof(g1)); len=cnt1,cnt1=;
int x=lower_bound(g2+,g2+cnt2+,h[],cmp)-g2,sum=;
dd.del(x); g2[x]=dd.val(x);
if(g2[len]) vis[]=,sum++;
for(int i=;i<=N;i++){
int j=lower_bound(q+,q+M+,(que){,i,})-q;
while(j<=M&&q[j].pos==i)
ans[q[j].id]=max(ans[q[j].id],len-(sum==)),j++;
int x1=lower_bound(g1+,g1+cnt1+,h[i])-g1;
if(x1>cnt1) cnt1=x1; g1[x1]=h[i];
int x2=lower_bound(g2+,g2+cnt2+,h[i+],cmp)-g2;
dd.del(x2); g2[x2]=dd.val(x2);
if(!g2[x2]) cnt2--;
if((x1==len||g1[x1]<g2[len-x1])&&!vis[x1]) vis[x1]=,sum++;//请注意这一句的第一个条件
if(g1[len-x2]>=g2[x2]&&vis[len-x2]) vis[len-x2]=,sum--;
}
for(int i=;i<=M;i++) printf("%d\n",ans[i]);
}
int main()
{
data_in();
work();
return ;
}
Codeforces Round #345 Div.1 D.Zip-line 动态最长上升子序列的更多相关文章
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
- Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力
B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...
- Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】
A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...
随机推荐
- sharepoint2013配置开发环境
- Ajax全接触(1)
Ajax全称:Asynchronous JavaScript and XML(异步的JavaScript和XML) .Ajax不是某种编程语言 是一种在无需重新加载整个网页的情况之下能够更新部分网页的 ...
- 辨析:Object与Instance都是对象,概念上没有区别。
Object与Instance有重要的区别:Object是客观世界中存在的实体:Instance是将Object虚拟到计算机世界的实例,它的生存方式是可运行的代码,它的生存环境是计算机中的内存资源,生 ...
- [SHOI2015]脑洞治疗仪(恶心的线段树,区间最大子段和)
题目描述: 曾经发明了自动刷题机的发明家 SHTSC 又公开了他的新发明:脑洞治疗仪--一种可以治疗他因为发明而日益增大的脑洞的神秘装置. 为了简单起见,我们将大脑视作一个 01 序列.11代表这个位 ...
- ABAP术语-Logical Lock
Logical Lock 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/03/1088323.html Program logic that ...
- java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testSelect], {ExactMatcher:fDisplayName=testSelect(cool.zsn.Dao.UserMapperTest)], {LeadingIdentifierMatcher:fClassName=cool.zsn
@Before:每次调用类中的方法,都会先执行@Before下的方法 @Before下的方法应该是 public : @Before public void init() { application ...
- 构建高可靠hadoop集群之0-hadoop用户向导
本文翻译自:http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsUserGuide.html 基于2.8. ...
- python的爬虫代理设置
现在网站大部分都是反爬虫技术,最简单就是加代理,写了一个代理小程序. # -*- coding: utf-8 -*- #__author__ = "雨轩恋i" #__date__ ...
- windows和Ubuntu下安装mongodb
windows 下载 mongodb官网下载压缩版安装包:下载地址:https://www.mongodb.com/download-center/community 注意选择版本(目前windows ...
- Windows和Linux系统下,虚拟环境安装的全面说明和详细步骤
虚拟环境的创建和使用 用途: 1.在同一台电脑安装同一个包的不同版本 2.记录项目所用的所有的包的版本,方便部署. 如何使用: 1.创建虚拟环境 mkvirtualenv 虚拟环境名 -p pyt ...