如何判断一个元素是否一定在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. SpringMVC重定向传递参数

    在SpringMVC的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到.但是,这样使用的是forward方式,浏览器的地址栏是不变 ...

  2. NET Core中使用Apworks

    NET Core中使用Apworks HAL,全称为Hypertext Application Language,它是一种简单的数据格式,它能以一种简单.统一的形式,在API中引入超链接特性,使得AP ...

  3. (转) Linux命令详解-date

    Linux命令详解-date 原文:https://www.cnblogs.com/Dodge/p/4278292.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到 ...

  4. Jenkins+Gitlab+Ansible自动化部署(四)

    接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html Jenkins应用 Jenkin ...

  5. 获取spring里的bean

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...

  6. java学习笔记(2)——数组

    1.创建数组: int[] a = new int[n];//数组长度n不要求为常数,一旦创建了数组,其大小不可改变 int[] a = {0,1,2,3};//也可这样定义 获得数组元素的个数:ar ...

  7. Redis key 键

    1.set key value    //设置.修改值 2.get key  //如果key不存在,返回nil,表示空. 3.type key   //返回key对应的value的数据类型 4.ren ...

  8. logback-spring.xml

    <?xml version="1.0" encoding="UTF-8"?><!--该日志将日志级别不同的log信息保存到不同的文件中--&g ...

  9. 192.168.28.168:3000打开网页无法调试localhost为前缀的接口

    最近我在IIS上发布.net Core API的时候发现 当我用localhost去打开我的web项目时 并且,ajax调用的接口前缀为localhost时候,运行正确 但是当我用192.168.28 ...

  10. htmlparse

    <html>    <head>        <style>                textarea{                width:800p ...