[51Nod] 1218 最长递增子序列 V2
如何判断一个元素是否一定在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的更多相关文章
- [51Nod 1218] 最长递增子序列 V2 (LIS)
传送门 Description 数组A包含N个整数.设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可 ...
- 51nod 1218 最长递增子序列 V2(dp + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 题解:先要确定这些点是不是属于最长递增序列然后再确定这 ...
- 51nod 1218 最长递增子序列 V2——LIS+思路(套路)
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...
- 51nod 1218 最长递增子序列 | 思维题
51nod 1218 最长递增子序列 题面 给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中. 题解 YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它 ...
- 51nod 1134 最长递增子序列
题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...
- 51nod 1376 最长递增子序列的数量(线段树)
51nod 1376 最长递增子序列的数量 数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递 ...
- 51Nod 1376 最长递增子序列的数量 —— LIS、线段树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376 1376 最长递增子序列的数量 基准时间限制:1 秒 空 ...
- LCS 51Nod 1134 最长递增子序列
给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行:1个 ...
- 51NOD 1376 最长递增子序列的数量 [CDQ分治]
1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...
随机推荐
- P5346 【XR-1】柯南家族
题目地址:P5346 [XR-1]柯南家族 Q:官方题解会咕么? A:不会!(大雾 题解环节 首先,我们假设已经求出了 \(n\) 个人聪明程度的排名. \(op = 1\) 是可以 \(O(1)\) ...
- react-native-contact 安卓已测试,
1. 下载模块 npm install react-native-contacts --save 2.安卓配置: a.在android/settings.gradle include ':rea ...
- python进阶05 常用问题库(1)json os os.path模块
python进阶05 常用问题库(1)json os os.path模块 一.json模块(数据交互) web开发和爬虫开发都离不开数据交互,web开发是做网站后台的,要跟网站前端进行数据交互 1.什 ...
- POJ 3299
#include <iostream> #include "math.h" double e2h(double e) { return 0.5555*(e-10.0); ...
- Codeforces 1168C(二进制、dp)
要点 '&'操作暗示二进制上按位思考 对于y为1的位,要求x和y之间要至少有两个此位为1的(包含x.y),这样&起来才不是0.而这些位中只要存在一个是ok的即可 dp去求每个x的每个位 ...
- GZS与小公园(DFS)
Description 某天GZS漫步在学校新建的小公园,他发现那里建成了一些水池和小河道.我们暂且把它们统一看成水池.假设公园旁有一张小公园的地图,上面仅标识了此处是否是水池,你能帮GZS计算出该地 ...
- Aspose.word直接转pdf
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- webpack.config.js====CSS相关:css和scss配置loader
1. 安装: //loader加载器加载css和sass模块 cnpm install style-loader css-loader node-sass sass-loader --save-dev ...
- springboot使用schedule定时任务
定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...
- JavaScript中var a=b=c=d的发现
看了别人的博客随手记录下 先看一下以下的代码 var a=1,b=2,c=3; (function(){ var a=b=1; })(); console.log(a); console.log(b) ...