Codeforces Zip-line 650D 345Div1D(LIS)
传送门
大意:给出一个序列,求修改一个数过后的最长上升子序列。
思路:可以用主席树在线搞,也可以用树状数组离线搞,明显后者好写得多。我们首先读取所有的询问,然后就把询问绑在给出的位置,然后我们正向做一遍LIS,反向做一遍LDS,然后就可以解决这个问题了。
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAXN 400005
using namespace std;
inline void GET(int &n) {
char c; n = 0;
do c = getchar(); while('0' > c || c > '9');
do n=n*10+c-'0',c=getchar();while('0' <= c && c <= '9');
}
vector<int> q[MAXN];
int lm[MAXN << 1], rm[MAXN << 1], B[MAXN << 1], a[MAXN];
int n, m, N, ask[MAXN], ans[MAXN], f[MAXN], g[MAXN], cnt[MAXN], qid[MAXN];
inline void gmax(int &a, int b) { if(a < b) a = b; }
inline void A2l(int x, int v) {
for(; x <= N; x += x&-x) gmax(lm[x], v);
}
inline void A2r(int x, int v) {
for(; x > 0; x -= x&-x) gmax(rm[x], v);
}
inline int Qml(int x, int v = 0) {
for(; x > 0; x -= x&-x) gmax(v, lm[x]); return v;
}
inline int Qmr(int x, int v = 0) {
for(; x <= N; x += x&-x) gmax(v, rm[x]); return v;
}
inline int BS(int p) {
int l = 1, r = N, mid, ans = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(B[mid] >= p) { ans = mid; r=mid-1; }
else l = mid+1;
}
return ans;
}
int main() {
GET(n); GET(m); int id;
for(int i = 1; i <= n; ++ i) {
GET(a[i]); B[++ N] = a[i];
}
for(int i = 1; i <= m; ++ i) {
GET(qid[i]); GET(ask[i]);
q[qid[i]].push_back(i);
B[++ N] = ask[i];
ans[i] = 1;
}
sort(B + 1, B + N + 1);
N = unique(B + 1, B + N + 1) - B-1;
for(int i = 1; i <= n; ++ i)
a[i] = BS(a[i]);
for(int i = 1; i <= m; ++ i)
ask[i] = BS(ask[i]);
int lgst = 0;
for(int i = 1; i <= n; ++ i) {
for(auto j : q[i]) ans[j] += Qml(ask[j]-1);
f[i] = Qml(a[i]-1) + 1;
A2l(a[i], f[i]); lgst = max(lgst, f[i]);
}
for(int i = n; i > 0; -- i) {
for(auto j : q[i]) ans[j] += Qmr(ask[j]+1);
g[i] = Qmr(a[i]+1) + 1; A2r(a[i], g[i]);
}
for(int i = 1; i <= n; ++ i) if(f[i] + g[i] == lgst + 1) ++ cnt[ f[i] ];
for(int i = 1; i <= m; ++ i)
if(f[qid[i]] + g[qid[i]] == lgst + 1 && 1 == cnt[ f[qid[i]] ]) printf("%d\n", max(ans[i], lgst-1));
else printf("%d\n", max(ans[i], lgst));
return 0;
}
Codeforces Zip-line 650D 345Div1D(LIS)的更多相关文章
- Codeforces Gym101246H:``North-East''(LIS+思维)
http://codeforces.com/gym/101246/problem/H 题意:在二维平面上有n个点,从最左下角的点出发,每次走只能走在当前的点的右上角的点(xj > xi, yj ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 1304D. Shortest and Longest LIS 代码(构造 贪心)
https://codeforces.com/contest/1304/problem/D #include<bits/stdc++.h> using namespace std; voi ...
- Codeforces 1304D. Shortest and Longest LIS
根据题目,我们可以找最短的LIS和最长的LIS,找最短LIS时,可以将每一个increase序列分成一组,从左到右将最大的还未选择的数字填写进去,不同组之间一定不会存在s[i]<s[j]的情况, ...
- CodeForces 7C Line
ax+by+c=0可以转化为ax+by=-c: 可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1 此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z= ...
- codeforces 629D 树状数组+LIS
题意:n个圆柱形蛋糕,给你半径 r 和高度 h,一个蛋糕只能放在一个体积比它小而且序号小于它的蛋糕上面,问你这样形成的上升序列中,体积和最大是多少 分析:根据他们的体积进行离散化,然后建树状数组,按照 ...
- CodeForces - 650D:Zip-line (LIS & DP)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
- codeforces #345 (Div. 1) D. Zip-line (线段树+最长上升子序列)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long a ...
随机推荐
- Redis(二) 扩展
事务multi ... exec 之间的操作先进入等待队列,到exec时一起执行 事物的所有操作结果都是一起返回的,所以前一条指令的结果无法作为后一条指令的参数 ...
- 【Android端APP 安装包检查】安装包检查具体内容及实现方法
一.安装包检查的具体包含内容有哪些? 1.安装包检查的一般内容包括: 安装包基本信息检查: 文件大小: xx MB 包名: com.xx 名称: xx 本次安装包证书与外网证书对比一致性:是 版本号 ...
- extern extern “C”用法详解
1.extern 修饰一个变量,告诉编译器这个变量在其他地方定义,编译器不会给出变量未定义的警告. extern tells the compiler that the variable is def ...
- Firefox每次刷新时自动清空缓存的设置方法
当我们开发网页应用时候,为了保证每次看到的页面是最新的,需要在刷新页面时清除页面缓存. 如果每次都手动清除比较麻烦,好在多数浏览器都支持自动清除缓存的功能. IE下我们可以将缓存设置为"每次 ...
- How to build the Robotics Library from source code on Windows
The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...
- android中ColorStateList及StateListDrawable设置Selector
写过android的代码相信大家对Selector并不陌生吧,下面来看看这段xml文件是如何定义的 <?xml version="1.0" encoding="ut ...
- softmax分类器+cross entropy损失函数的求导
softmax是logisitic regression在多酚类问题上的推广,\(W=[w_1,w_2,...,w_c]\)为各个类的权重因子,\(b\)为各类的门槛值.不要想象成超平面,否则很难理解 ...
- Ubuntu 15.10 x64 安装 Android SDK
操作系统:Ubuntu 15.10 x64 目标:安装 Android SDK 本文最后更新时间:2015-11-3 安装32位库文件 2013年9月的iPhone 5s是第一款64位手机,而Andr ...
- 把文件打成zip或然rar下载 (详询请加qq:2085920154)
//文件打包下载 public static HttpServletResponse downLoadFiles(List<File> files, HttpServletRequest ...
- ASP.NET中的XML和JSON
一.DOM简介 1.XML 定义:XML是一种跨语言.跨平台的数据储存格式 2.什么是DOM DOM(document object model)文档对象模型:是一种允许程序或脚本动态的访问更新文档内 ...