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 ...
随机推荐
- Oracle之视图
Oracle之视图 2018.9.12 由于视图的数据与表数据互相关联,所以切记谨慎操作 建立视图 使用下面sql语句来完成视图的创建 create or replace view 视图名 as se ...
- Xtrabackup备份与恢复MySQL
1.innobackupex备份原理 .innobackupex启动并fork一个进程启动xtrabackup,然后等待xtrabackup备份InnoDB文件; .xtrabackup备份时存在两个 ...
- UIlable上下居中
- (void)setView{ self.chooseImg = [[UIImageView alloc] initWithFrame:CGRectMake(TO_LEFT_D, (ROW_HEIG ...
- Python学习——01Linux基础之常用基本命令
做Linux要知道两件事: 首先知道自己处在什么位置(桌面……) 区分大小写 pwd:查看当前所在目录 “/”代表:根目录 Cd: cd( ...
- 八、USB驱动分析
学习目标:分析USB驱动源码结构. 一.Windows下USB驱动理论问题 1. 当usb设备接入PC时,右下角弹出"发现AAA",并弹出对话框,提示安装驱动程序.没有驱动程序,W ...
- queue消息队列
class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out class queue. ...
- windows环境下,用python绘图库matplotlib绘图时中文乱码问题
1.下载中文字体(看自己爱好就行)下面这个举例: SimHei - Free Font Downloadwww.fontpalace.co 2.下载之后,打开即可安装,将字体安装进windows系统 ...
- python2.7入门---文件I/O&简单用户交互
这篇文章开始之前,我们先来看下python中的输出方法.最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写 ...
- vue2.0 Axios 的简单用法
安装 使用 npm: $ npm install axios 使用 bower: $ bower install axios 使用 cdn: <script src="https:// ...
- WebSocket 的使用
Java 控制台程序实现类似广播功能 服务器端代码 添加 maven 依赖 <dependency> <groupId>javax.websocket</groupId& ...