51nod 1218 最长递增子序列

题面

给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中。

题解

YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它开头的LIS长度 - 1 = n,那么这个元素可能在LIS中。

那么什么时候它一定在呢?就是它在LIS中的位置“无可替代”的时候,即:设以它结尾的LIS长度为x,以任何其它元素(不可能在LIS中的元素除外)结尾的LIS长度均不为x。

然后就做出来了!

#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
bool read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
else if(c == EOF) return 0;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
return 1;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 50005;
int n, a[N], s[N], cnt, lis[N], lds[N], type[N], tot[N];
int main(){
read(n);
for(int i = 1; i <= n; i++)
read(a[i]);
for(int i = 1; i <= n; i++){
if(!cnt || a[i] > s[cnt]) s[++cnt] = a[i], lis[i] = cnt;
else {
int pos = lower_bound(s + 1, s + cnt + 1, a[i]) - s;
s[pos] = a[i];
lis[i] = pos;
}
}
cnt = 0;
for(int i = 1; i <= n; i++)
a[i] = -a[i];
for(int i = n; i; i--){
if(!cnt || a[i] > s[cnt]) s[++cnt] = a[i], lds[i] = cnt;
else {
int pos = lower_bound(s + 1, s + cnt + 1, a[i]) - s;
s[pos] = a[i];
lds[i] = pos;
}
}
for(int i = 1; i <= n; i++)
if(lis[i] + lds[i] != cnt + 1) type[i] = 1;
else tot[lis[i]]++;
putchar('A'), putchar(':');
for(int i = 1; i <= n; i++)
if(!type[i] && tot[lis[i]] > 1)
write(i), space;
enter;
putchar('B'), putchar(':');
for(int i = 1; i <= n; i++)
if(!type[i] && tot[lis[i]] == 1)
write(i), space;
enter;
return 0;
}

51nod 1218 最长递增子序列 | 思维题的更多相关文章

  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 最长递增子序列 V2

    如何判断一个元素是否一定在LIS中?设f[i]为以ai结尾的LIS长度,g[i]为以ai开头的LIS长度,若f[i]+g[i]-1==总LIS,那么i就一定在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. 51nod 1134最长递增子序列

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...

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

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

随机推荐

  1. 求二维数组最大子数组的和。郭林林&胡潇丹

    求二维数组子数组的最大值,开始思路不太清晰.先从最简单的开始. 以2*2的简单数组为例找规律, 假设最大数为a[0][0],则summax=a[0][0],比较a[0][0]+a[0][1].a[0] ...

  2. 比较语义分割的几种结构:FCN,UNET,SegNet,PSPNet和Deeplab

    简介 语义分割:给图像的每个像素点标注类别.通常认为这个类别与邻近像素类别有关,同时也和这个像素点归属的整体类别有关.利用图像分类的网络结构,可以利用不同层次的特征向量来满足判定需求.现有算法的主要区 ...

  3. Python处理PDF和Word文档常用的方法

    Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...

  4. PHP正则表达式匹配俄文字符

    之前弄过匹配中文的 见 http://www.cnblogs.com/toumingbai/p/4688433.html preg_match_all("/([\x{0400}-\x{04F ...

  5. pwd命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/gnail_oug/article/details/70664458 pwd是Print Working Directory ...

  6. javascript常用方法和技巧

    浏览器变编辑器 data:text/html, <style type=;right:;bottom:;left:;}</style><div id="e" ...

  7. idea最常使用的快捷键

    撤销 反撤销 : Ctrl+Z / Ctrl+Shift+Z 删除一行 : Ctrl+Y 跳到实现类 : Ctrl+Alt+B 重命名文件:   shift+F6 控制台放大缩小: ctrl+shif ...

  8. Java-URLEncoder.encode 什么时候才是必须的

    当你希望把一段 URL 当成另一个 URL 的参数时,比如:当用户点击交易的按钮时你发现未登录就跳转到 login 页面同时带上一个参数记录在登录之前用户是希望访问的那个交易页面,这样在登录完成之后再 ...

  9. 常用DB2命令

    建库 db2 territory CN on 建库到指定位置 db2 create database OADB on D: using codeset GBK territory CN 列出所有数据库 ...

  10. Beta版发布 - 感谢有你们

    在本次Beta版开发的过程中,很感谢组长王航对我的信任,让我统筹大家的工作任务和进度,使我对项目管理有了深刻的理解. 我也要感谢邹双黛,在beta版开发中因为邹双黛的帮助,我对于文字类工作已经越来越得 ...