CodeForces 650D Zip-line(最长上升子序列)
来源:互联网 发布:上位机用什么编程 编辑:程序博客网 时间:2024/06/07 16:43
题意:有一个n个整数组成的序列,现在有m个询问,每次询问改变一个位置上的最长上升子序列长度(n,m<1e5)。
思路:假设原始序列的lis长度为k,那么可以确定的是修改一个数后新的lis只有可能是k+1,k,k-1这三种。
假设我们已经求出了原始序列中以第i个元素结束的最长上升子序列长度lisL[i]和以第I个元素结束的最长上升子序列长度lisR[i]
以及假如修改后以第i个元素结束的最长上升子序列长度L[I]和以第I个元素结束的最长上升子序列长度R[i]
现在需要分情况讨论(假设修改的是第I个位置):
k+1:当L[i]+ R[i] -1= k + 1的时候新lis为k+1,也就是说这时候修改位置的元素可以插到原来整个序列的某一个lis序列中。
k:当L[i]+ R[i] -1= k 或者是 原序列中存在一个不经过第i个位置的lis序列。
k-1:其余情况答案是k-1,其实这也就是说原序列中所有lis序列都经过第I个位置,但是第i个位置被修改了以后新序列变短了。
#include<bits/stdc++.h>#define eps 1e-6#define LL long long#define pii pair<int, int>#define pb push_back#define mp make_pair//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int MAXN = 500000;const int INF = 0x3f3f3f3f;struct Query {int id;int pos, val;int l, r;bool operator < (const Query& Q) const {return pos < Q.pos;}} q[MAXN];int n, m, h[MAXN];int lisL[MAXN], lisR[MAXN];int g[MAXN];int maxlen;int ans[MAXN];int posCnt[MAXN];void get_lisL() {maxlen = 0;int pointer = 1;for (int i = 1; i <= n; i++) g[i] = INF;for (int i = 1; i <= n; i++) {while (pointer <= m && q[pointer].pos == i) {int pos = lower_bound(g+1, g+n+1, q[pointer].val) - g;q[pointer].l = pos;pointer++;}int pos = lower_bound(g+1, g+n+1, h[i]) - g;lisL[i] = pos;g[pos] = h[i];maxlen = max(pos, maxlen);}}void get_lisR() {int pointer = m;for (int i = 1; i <= n; i++) g[i] = INF;for (int i = n; i > 0; i--) {while (pointer > 0 && q[pointer].pos == i) {int pos = lower_bound(g+1, g+n+1, -q[pointer].val) - g;q[pointer].r = pos;pointer--;}int pos = lower_bound(g+1, g+n+1, -h[i]) - g;lisR[i] = pos;g[pos] = -h[i];}}int get_posCnt() {for (int i = 1; i <= n; i++) {if (lisL[i]+lisR[i] > maxlen)posCnt[lisL[i]]++;}}int main(){ //freopen("input.txt", "r", stdin);scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++)scanf("%d", &h[i]);for (int i = 1; i <= m; i++) {scanf("%d%d", &q[i].pos, &q[i].val);q[i].id = i;}sort(q+1, q+m+1);get_lisL();get_lisR();get_posCnt();for (int i = 1; i <= m; i++) {int pos = q[i].pos;if (q[i].l+q[i].r > maxlen) ans[q[i].id] = q[i].l + q[i].r - 1;else if (lisL[pos]+lisR[pos] > maxlen && posCnt[lisL[pos]]==1)ans[q[i].id] = maxlen - 1;else ans[q[i].id] = maxlen;}for (int i = 1; i <= m; i++)printf("%d\n", ans[i]); return 0;}
0 0
- CodeForces 650D Zip-line(最长上升子序列)
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)
- Codeforces 10D LCIS 求最长公共上升子序列及输出这个子序列 dp
- #10 D. LCIS (dp+最长公共上升子序列)
- codeforces 650 D. Zip-line
- codeforces 650d Zip-line
- poj 2127 zoj 2432 codeforces 10D 最长上升公共子序列
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
- Codeforces Round #323 (Div. 2) D LIS 最长上升子序列
- Codeforces Beta Round #10-D. LCIS(最长上升公共子序列)
- A序列(最长上升子序列)
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- Android基础知识之控件系列(4)——CheckedTextView、Chronometer、DigitalClock类
- Extjs 打包 failed to find any files
- 最小二乘法
- ASM AMDU工具的使用
- 数据库SQL优化大总结之 百万级数据库优化方案
- CodeForces 650D Zip-line(最长上升子序列)
- SQL 优化原则
- Spring MVC之 注解
- 【ios】相册、定位等权限判断
- 带图片分享腾讯微博
- 交换两个变量的值,不创建中间变量。求函数返回参数二进制中 1 的个数
- 报错svn: E200014: Checksum mismatch for
- IE8-下trim()函数不可用的问题解决
- 为已存在数据的表加主键