AcWing 896. 最长上升子序列 II
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int>arr(n);
for (int i = ; i < n; ++i)
cin >> arr[i];
vector<int>stk;//模拟堆栈
stk.push_back(arr[]);
for (int i = ; i < n; ++i) {
if (arr[i] > stk.back())//如果该元素大于栈顶元素,将该元素入栈
stk.push_back(arr[i]);
else//替换掉第一个大于或者等于这个数字的那个数
*lower_bound(stk.begin(), stk.end(), arr[i]) = arr[i];
}
cout << stk.size() << endl;
return ;
}
#include <iostream>
#include <algorithm>
using namespace std;
const int N = ;
int n;
int a[N];
int q[N];//所有不同长度下上升序列的结尾的最小值(也就是,第一个数的最小)
int main() {
scanf("%d", &n);
for (int i = ; i < n; i ++ ) scanf("%d", &a[i]);
int len = ;//q里面的元素个数 ,最开始为0,一个都没有
q[] = -2e9;
for (int i = ; i < n; i ++ ) {//枚举每个数字
int l = , r = len;
while (l < r) { //二分出来小于a[i] 之前的 数字中最大的数字
int mid = l + r + >> ;
if (q[mid] < a[i]) l = mid;
else r = mid - ;
}
len = max(len, r + );//r找的是可以接在哪个长度的后面
q[r + ] = a[i];
}
printf("%d\n", len);
return ;
}
AcWing 896. 最长上升子序列 II的更多相关文章
- AcWing 895. 最长上升子序列
//设上升序列的最后一个数字为第i个,那么就以第i-1个位分类标准, //i-1可以没有,也可以是在数组中下标为1,下标为2 //一直到下标为i-1的数字 #include <iostream& ...
- AcWing 897. 最长公共子序列
#include <iostream> #include <algorithm> using namespace std; ; int n, m; char a[N], b[N ...
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
- Java实现 LeetCode 522 最长特殊序列 II(查找最长的非子序列的长度)
522. 最长特殊序列 II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些 ...
- UVA10599:Robots(II)(最长上升子序列)
Your company provides robots that can be used to pick up litter from fields after sporting events an ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- P1439 【模板】最长公共子序列
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...
- [HAOI2007]上升序列(最长上升子序列)
题目描述 对于一个给定的 S=\{a_1,a_2,a_3,…,a_n\}S={a1,a2,a3,…,an} ,若有 P=\{a_{x_1},a_{x_2},a_{x_3},…,a_{x_m}\ ...
- HDOJ1025(最长上升子序列)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- sql注入文件写入和读取
系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...
- PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 8,DA=6,则 A 的“6 部分”PA 是 66,因为 A 中有 ...
- Npoi常用操作方法介绍
1.ShiftRows(startRow,endRow,moveRows) 将开始行到结束行向上或者向下移动moveRows行,moveRows为正数向下移动,为负数向上移动(向上移动,会把之前的行覆 ...
- 服务起不来,查看ps axj 看服务是否为守护进程(TPGID 为-1)
在linux命令行中输入: ps axj 查看服务进程的 TPGID 字段的值是否为-1 ,为-1表示为守护进程 不为-1表示不是守护进程,服务启动不起来,或者启动起来后又被杀死了
- Linux C++ 单链表添加,删除,输出,逆序操作
/*单链表操作*/#include <iostream>using namespace std; class Node{ public: Node(){ next=0; } Node(in ...
- centos7最小版配置
配置启用dns cd /etc/sysconfig/network-scripts/ vi ifcfg-ens33 # 修改ONBOOT为yes ONBOOT=yes 重启系统 reboot 安装ne ...
- linux-crond_计划任务
定时计划任务 主要文件介绍: [root@nginx ~]# ll /etc/cron* -d drwxr-xr-x. 2 root root 21 7月 11 20:28 /etc/cron.d d ...
- Keras 回归 拟合 收集
案例1 from keras.models import Sequential from keras.layers import Dense, LSTM, Activation from keras. ...
- 第一个安卓app——计算器
几天前,我花了一天时间,结合这段时间所学知识开发出了一个简单的计算器,它由两个TextView和23个Button组成,代码会放在文章结尾. TextView TextView:上面一个TextVie ...
- 02-SV数据类型
1.数据类型 内建数据类型:逻辑(logic)类型.双状态数据类型(bit,byte,shortint,int,longint).四状态数据类型(integer,time,real) 其他:定宽数组. ...