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 ...
随机推荐
- Vue nodejs商城-订单模块
一.订单列表渲染 新建OrderConfirm.vue订单确认页面,添加路由 src/router/index.js添加路由 import OrderConfirm from '@/views/Ord ...
- selenium之Xpath定位
1. 绝对定位: driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从 ...
- python之selectors
selectors是select模块的包装器,ptython文档建议大部分情况使用selectors而不是直接使用selectors 样例代码如下 # -*- coding: utf-8 -*- __ ...
- 解方程(hash,秦九韶算法)
题目描述 已知多项式方程: a0+a1x+a2x2+⋯+anxn=0 求这个方程在 [1,m]内的整数解(n 和 m 均为正整数). 输入输出格式 输入格式: 共 n+2 行. 第一行包含 2个整数 ...
- [SCOI2009]windy数(数位DP)
题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? 输入输出格式 输 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水
链接:https://www.nowcoder.com/acm/contest/90/L 来源:牛客网 1.题目描述 -- 在一个风雨交加的夜晚,来自异世界的不愿透露姓名的TMK同学获得了两种超强药水 ...
- 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形
1. 在用python爬取小说的时候, 发现在内容里每次换行都有\r\n(即回车, 换行)出现. 此时可以采用 s.replace('\\r\\n','') , 其中s为字符串类型. 2. 在爬取完 ...
- VS2013入门驱动配置测试
准备工作: VS2013 WDK8.1 DbgView InstDrv VS2013+WDK8.1是绝配,意思是这两个版本结合最方便,安装后无需任何改动直接写代码,自动生成模板,省去了设置一些参数繁琐 ...
- ABAP术语-Interface
Interface 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/22/1077086.html Information tool that ...
- linux 2.6升级Python2.7 ./configure 报错问题
升级2.7.3使用命令./configure --prefix=/usr/local/python2.7.3时,出现以下错误:checking build system type... x86_64- ...