如何判断一个元素是否一定在LIS中?设f[i]为以ai结尾的LIS长度,g[i]为以ai开头的LIS长度,若f[i]+g[i]-1==总LIS,那么i就一定在LIS中出现

显然只出现一次的元素一定是必选,剩下的就是可选了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector> using namespace std; const int MAXN=; vector<int> vec[MAXN],A,B; int tmp[MAXN],mx[MAXN];
int a[MAXN],f[MAXN],g[MAXN],n;
int b[MAXN];
int t[MAXN],t2[MAXN];
inline void upmax(int &x,int y){x=max(x,y);}
void update(int x,int w){for(int i=x;i<=n;i+=i&-i)upmax(t[i],w);}
int query(int x){int _=;for(int i=x;i;i-=i&-i)upmax(_,t[i]);return _;}
void update2(int x,int w){for(int i=n-x+;i<=n;i+=i&-i)upmax(t2[i],w);}
int query2(int x){int _=;for(int i=n-x+;i;i-=i&-i)upmax(_,t2[i]);return _;}
int main(){
n=rd();
for(int i=;i<=n;i++)tmp[i]=a[i]=rd();
sort(tmp+,tmp++n);
int tot=unique(tmp+,tmp++n)--tmp;
for(int i=;i<=n;i++)a[i]=lower_bound(tmp+,tmp++tot,a[i])-tmp;
int mxf=;
for(int i=;i<=n;i++){
f[i]=query(a[i]-)+;
update(a[i],f[i]);
upmax(mxf,f[i]);
}
for(int i=n;i>=;i--){
g[i]=query2(a[i]+)+;
update2(a[i],g[i]);
}
for(int i=;i<=n;i++){
if(f[i]+g[i]!=mxf+)continue;
vec[f[i]].push_back(i);
}
for(int i=;i<=n;i++){
int s=vec[i].size();
if(s==)continue;
if(s==){A.push_back(vec[i][]);continue;}
for(int j=;j<s;j++)B.push_back(vec[i][j]);
}
sort(A.begin(),A.end());
sort(B.begin(),B.end());
vector<int>::iterator it;
printf("A:");
for(it=B.begin();it!=B.end();it++)printf("%d ",*it);
putchar('\n');
printf("B:");
for(it=A.begin();it!=A.end();it++)printf("%d ",*it); }

[51Nod] 1218 最长递增子序列 V2的更多相关文章

  1. [51Nod 1218] 最长递增子序列 V2 (LIS)

    传送门 Description 数组A包含N个整数.设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可 ...

  2. 51nod 1218 最长递增子序列 V2(dp + 思维)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 题解:先要确定这些点是不是属于最长递增序列然后再确定这 ...

  3. 51nod 1218 最长递增子序列 V2——LIS+思路(套路)

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...

  4. 51nod 1218 最长递增子序列 | 思维题

    51nod 1218 最长递增子序列 题面 给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中. 题解 YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它 ...

  5. 51nod 1134 最长递增子序列

    题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...

  6. 51nod 1376 最长递增子序列的数量(线段树)

    51nod 1376 最长递增子序列的数量 数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递 ...

  7. 51Nod 1376 最长递增子序列的数量 —— LIS、线段树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376 1376 最长递增子序列的数量 基准时间限制:1 秒 空 ...

  8. LCS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  9. 51NOD 1376 最长递增子序列的数量 [CDQ分治]

    1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...

随机推荐

  1. sublime配置nodejs运行调试js

    node.js调试javascript的配置 1. 首先到 nodejs.org 下载 Node.js 安装包并安装.2. 打开 Sublime Text 编辑器.选择菜单 Tools --> ...

  2. Unity 打包PC和安卓的路径注意事项

    if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...

  3. luogu P2709 小B的询问 最简单的莫队

    块内按右端点sort,块外按左端点sort 话说我刚开始这么修改... inline )*(c[a[i]]-),--c[a[i]];} inline )*(c[a[i]]+),++c[a[i]];} ...

  4. Net Core下通过Proxy 模式

    Net Core下通过Proxy 模式 NET Core下的WCF客户端也是开源的,这次发布.NET Core 2.0,同时也发布了 WCF for .NET Core 2.0.0, 本文介绍在.NE ...

  5. E. Selling Souvenirs 不会做

    http://codeforces.com/contest/808/problem/E 不理解为什么dp = {cost, cnt1, cnt2}可以 而dp = {cost, cnt1, cnt2, ...

  6. Linux 运维培训笔记

    2018/01/05 权限管理:sudoers文件 KAIFA_ADMINS           ALL=(OP1)            KAIFACMD 用户(大写)                ...

  7. BI的意义

    BI系统建设的价值,有可能不值钱,也有可能价值数千万,就看我们大家好用了没.”所以,BI系统建设的收获,终究还是因企业而异的,再归根,便是与企业的文化,与企业的人,尤其是管理层是极为相关的了. 商业智 ...

  8. nodejs 实践:express 最佳实践(二) 中间件

    express 最佳实践(二):中间件 第一篇 express 最佳实践(一):项目结构 express 中最重要的就是中间件了,可以说中间件组成了express,中间件就是 express 的核心. ...

  9. 记录下这周的mysql调优工作

    这周一至周四基本都在做mysql的测试和调优工作,包括erlang端对mysql的写入测试,到今天为止暂且告一段落,下周先做下其他的开发.    测试环境    使用的测试环境是aliyun的杭州节点 ...

  10. 使用Calendar来获取当前日期和时间

    1 package com.java.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 6 pub ...