CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551
升级版:Uva 1471
题意:
让你求删除一段连续的子序列之后的LIS。
题解:
预处理:先求一个last[i],以a[i]为开始的合法最长上升子序列的长度。再求一个pre[i],以a[i]为结尾的合法最长上升子序列长度。
答案应该为
max(以j结束的合法最长上升子序列长度) + 以a[i]为开始的合法最长上升子序列长度。 其中j<a[i]。
1)通过树状数组维护区间最值。
2)通过LIS 维护。(详细请看http://www.cnblogs.com/denghaiquan/p/6761600.html)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ;
int a[maxn], pre[maxn], last[maxn], c[maxn];
int n;
int lowbit(int x){
return x&(-x);
}
int ask(int x)
{
int ans = ;
while(x>){
ans = max(ans, c[x]);
x-=lowbit(x);
}
return ans;
}
void updata(int x, int d)
{
while(x <= 1e4){
c[x] = max (c[x] , d);
x += lowbit(x);
}
}
void solve()
{
int ans = ;
ms(c, );
for(int i=;i<=n;i++) scanf("%d", &a[i]);
//last[i] 是第i个开始和合法后缀
last[n] = ;
for(int i = n-; i>;i--){
if(a[i] < a[i+]){
last[i] = last[i+] + ;
}else last[i] = ;
}
//pre[i] 是第i个结束的合法前缀
pre[] = ;
for(int i=;i<=n;i++){
if(a[i] > a[i-]){
pre[i] = pre[i-] + ;
}else pre[i] = ;
}
for(int i=;i<=n;i++){
ans = max(ans, last[i] + ask(a[i]-));
updata(a[i], pre[i]);
}
printf("%d\n", ans);
}
int main()
{
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
while(~scanf("%d", &n)){
solve();
}
return ;
}
升级版,应该数的范围在1e9所以无法用树状数组维护。只能用LIS或者set来维护。
CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)的更多相关文章
- CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- csu 1551: Longest Increasing Subsequence Again BIT + 思维
预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...
- CSUOJ 1551 Longest Increasing Subsequence Again
1551: Longest Increasing Subsequence Again Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 75 Solved ...
- HDU1087(树状数组求LIS)
题是水题,学习一下用树状数组求LIS. 先离散化一下,注意去重:然后就把a[i]作为下标,dp[i]作为值,max作为维护的运算插进树状数组即可. 如果是上升子序列,询问(a[i] - 1):如果是不 ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- HDU4991 Ordered Subsequence (树状数组优化DP)
dp[i][j]表示以a[i]结尾的长度为j的上升子序列个数. 方程:dp[i][j]=sum(dp[k][j-1]),a[k]<a[i],1<=k<i. 求解目标:sum(dp[k ...
- Codeforces 486E LIS of Sequence --树状数组求LIS
题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i] ...
- poj1631——树状数组求LIS
题目:http://poj.org/problem?id=1631 求LIS即可,我使用了树状数组. 代码如下: #include<iostream> #include<cstdio ...
随机推荐
- Pku2054 Color a Tree
有一个N个结点的有根树,1是这个树的根.现在要对这N个结点依次进行染色,每个结点染色要花费1个单位的时候,同时要满足一个结点仅在其父亲被染色后才可被染色,每个结点有个权值Ci,如果我们在第Ti时间对i ...
- 001--PowerDesigner连接MySQL
PowerDesigner连接MySQL(一) 博客地址:https://blog.csdn.net/codemonkey_king/article/details/53263597 https:// ...
- String类为什么是final
String 本身一个对象,对象在jvm启动的时候就要实例化和其他类调用就要实例化,第一性能,第二安全,因为string的高频繁的使用,如果被继承,哪里性能将会大大降低,因为不能被继承,换句话来说就比 ...
- Python - pycharm 代码自动补全
有很多人说是代码补全功能未打开,的确,代码补全功能确实要打开才能用,打开方法 file---->power save mode,把这个前面的√号去掉即可
- 3.Golang的包导入
1.golang的源码文件可以随意命名,但是属于同一个包的源文件必须声明 package base 2.golang的包引入规则 import ( "fmt" #系统包直接写名字 ...
- HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 (转)
阅读前提:本文分析的是源码,所以至少读者要熟悉它们的接口使用,同时,对于并发,读者至少要知道 CAS.ReentrantLock.UNSAFE 操作这几个基本的知识,文中不会对这些知识进行介绍.Jav ...
- 七层模型? IP ,TCP/UDP ,HTTP ,RTSP ,FTP 分别在哪层?
IP: 网络层TCP/UDP: 传输层HTTP.RTSP.FTP: 应用层协议
- P3064 [USACO12DEC]伊斯坦布尔的帮派 (模拟)
题目传送门 题意: 一片草地,每次可以只可以让一种牛占领,问你怎样安排牛的次序 最后剩下的是1号牛,并且输出其数量 思路: 看到n到100 ,所以可以(n^3)暴力,第一重遍历次序,第二枚举是哪只牛 ...
- Gorgeous Sequence(线段树)
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...