CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551
题意:
给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列的最长上升连续子序列最大。
题解:
1.对于要删除的的子序列而言,要么夹在答案序列中间,要么在外面(删与不删对答案都没影响)。所以总体而言,答案序列被分成左右两半。
2.用SL[i]记录从左边起以a[i]为结尾的最长上升连续子序列的长度, SR记录从右边起以a[i]为开始的最长上升连续子序列的长度。
3.枚举SR[i],用线段树找出最大的SL[x](x的下标小于i),即SL[x]和SR[x]构成一段完整的序列, 期间一直更新线段树。
学习之处:
1.线段树/树状数组的动态使用,即边查询边更新。
类似的题: http://blog.csdn.net/DOLFAMINGO/article/details/65643894
2.RMQ/线段树/树状数组的静态使用,即build()之后值进行查询操作。
相关的题:http://blog.csdn.net/DOLFAMINGO/article/details/68953809 http://blog.csdn.net/dolfamingo/article/details/70306529
线段树:
#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 eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int a[maxn], SL[maxn], SR[maxn], MAX[maxn<<];
int n; void update(int rt, int l, int r, int pos)
{
if(l==r)
{
MAX[rt] = max(MAX[rt], SL[pos]);
return;
} int mid = (l+r)>>;
if(a[pos]<=mid) update(rt*, l, mid, pos);
else update(rt*+ ,mid+, r, pos); MAX[rt] = max(MAX[rt*], MAX[rt*+]);
} int query(int rt, int l, int r, int x, int y)
{
if(x<=l && y>= r)
return MAX[rt]; int mid = (l+r)>>, ret = ;
if(x<=mid) ret = max(ret, query(rt*, l, mid, x, y));
if(y>=mid+) ret = max(ret, query(rt*+, mid+, r, x, y));
return ret;
} void solve()
{
for(int i = ; i<=n; i++)
scanf("%d",&a[i]); SL[] = SR[n] = ;
for(int i = ; i<=n; i++)
SL[i] = (a[i]>a[i-]?SL[i-]+:);
for(int i = n-; i>; i--)
SR[i] = (a[i]<a[i+]?SR[i+]+:); int ans = ;
for(int i = ; i<=n; i++)
{
int tmp = ;
if(a[i]>) tmp = query(, , , , a[i]-); ans = max(ans,SR[i]+tmp);
update(, , , i); }
printf("%d\n",ans);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
ms(SL,);
ms(SR,);
ms(MAX,);
solve();
}
}
树状数组:
#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 eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int a[maxn], SL[maxn], SR[maxn], c[maxn];
int n; int lowbit(int x)
{
return x&(-x);
} void add(int x, int d)
{
while(x<maxn)
{
c[x] = max(c[x],d);
x += lowbit(x);
}
} int sumc(int x)
{
int s = ;
while(x>)
{
s = max(s,c[x]);
x -= lowbit(x);
}
return s;
} void solve()
{
for(int i = ; i<=n; i++)
scanf("%d",&a[i]); SL[] = SR[n] = ;
for(int i = ; i<=n; i++)
SL[i] = (a[i]>a[i-]?SL[i-]+:);
for(int i = n-; i>; i--)
SR[i] = (a[i]<a[i+]?SR[i+]+:); int ans = ;
for(int i = ; i<=n; i++)
{
int tmp = ;
if(a[i]>) tmp = sumc(a[i]-); ans = max(ans,SR[i]+tmp);
add(a[i],SL[i]); }
printf("%d\n",ans);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
ms(SL,);
ms(SR,);
ms(c,);
solve();
}
}
CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和的更多相关文章
- CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 升级版:Uva 1471 题意: 让你求删除一段连续的子序列之后的LIS. 题 ...
- 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 ...
- FZU2013 A short problem —— 线段树/树状数组 + 前缀和
题目链接:https://vjudge.net/problem/FZU-2013 Problem 2013 A short problem Accept: 356 Submit: 1083Ti ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- Dynamic Programming | Set 3 (Longest Increasing Subsequence)
在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Opti ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- 推荐一款基于 AI 开发的 IDE 插件,帮助提升编码效率
最近在浏览技术社区的时候,发现了一款神奇 IDE 插件,官网称可以利用 AI 帮助程序员写代码,一下子吸引了我的好奇心.赶紧下载下来使用一番,感觉确实蛮神奇,可以火速提升编程效率. 这款插件叫做 ai ...
- 第一章spring boot简介
接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复黏贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot来让你更易上手, ...
- k8s之nginx-ingress、 Daemonset实现生产案例
上一篇中用node ip + 非80端口,访问k8s集群内部的服务.实际生产中更希望用node ip + 80端口的方式,访问k8s集群内的服务. # 修改mandatory.yaml中创建控制器部分 ...
- 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- Handler处理机制
handler缺点:如果要运送两种类型的数据(比如一个Bitmap,一个Object)就不能运送,但可以用Bunder来传输 * 使用handler的步骤: * 1.创建一个handl ...
- 更新tensorflow支持GPU时出错
sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...
- IDG | 四则运算表达式计算
分析 首先将中缀表达式转换为后缀表达式(逆波兰式),然后使用栈进行计算. 没有考虑括号.小数. 代码 import java.util.LinkedList; import java.util.Lis ...
- [Algorithm] Linked List Data Structure in JavaScript
A linked list is a collection of items where each item points to the next one in the list. Because o ...
- odoo生产物流
odoo生产从raw materials location 自动消耗物料,产成品进入到finish productslocation. 而odoo自动产生的MO[manufacture Order], ...
- Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...