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 ...
随机推荐
- python_001
一.python开发1.python基础 -基础 -基本数据类型 -函数 -面向对象2.网络编程3.web框架 -用于写网站4.设计模式 + 算法
- linux系统镜像iso文件下载
linux系统镜像iso文件下载 首先你要选择你想要的linux版本,常见版本有CentOS,Ubuntu.选择一个你需要的. 有两个镜像站推荐: 网易镜像站:http://mirrors.163.c ...
- It's strange. I felt less lonely when I didnt know you.
feasible:adj. 可行的 bypass: v. 绕开,避开 eclipse: n. 月食 raw: adj. 生的 foresee:v. 预见 premier:n. 总理 ,adj: 首要的 ...
- REACT--》fetch---基本使用
[WangQI]---fetch---基本使用 一.fetch fetch是一种XMLHttpRequest的一种替代方案,在工作当中除了用ajax获取后台数据外我们还可以使用fetch.axio ...
- 多线程07-Monitor.TryEnter
); Console.WriteLine())) { Console.WriteLine ...
- 触摸板PCB制作-TM12
1.布局: 使 PSoC 与Sensor之间的距离保持最小化是一个不错的做法. 通常将 PSoC 与其他组件一起贴装到底层,而将 CapSense Sensor置于顶层上. Sensor和栅格地层位 ...
- 面试一个 3 年 Java 程序员,一个问题都不会!
大家周末愉快,当你看到这篇文章的时候,事情已经过去几天了. 刚从洽谈室走出来,心情很复杂! 栈长面试过很多人,不乏知识渊博.技能顶尖的选手,但从未遇到过工作了三年,却一个问题都答不上来.. 这场史无前 ...
- 封装和private,this,super关键字的简单应用
1.将成员变量用private修饰 2.提供对应的getxx()和setxx()方法 public class Student { private String name; private int a ...
- Winfrom中数据的双向绑定(使用INotifyPropertyChanged)
在WPF中新建项目是自动实现了INotifyPropertyChanged接口,用于数据绑定时非常的方便在winfrom中也可以实现INotifyPropertyChanged接口 将需要绑定的字段写 ...
- VS2012 改C# 模版
原始文件位置: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache\CSharp\Co ...