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, ...
随机推荐
- SpringCloud 分布式事务解决方案
目录 TX-LCN分布式事务框架 TX-LCN分布式事务框架 随着互联化的蔓延,各种项目都逐渐向分布式服务做转换.如今微服务已经普遍存在,本地事务已经无法满足分布式的要求,由此分布式事务问题诞生. 分 ...
- 提高速度 history 的利用
history的介绍history是shell的内置命令,其内容在系统默认的shell的man手册中.history是显示在终端输入并执行的过命令,系统默认保留1000条.[root@localhos ...
- ubuntu允许mysql远程连接
ubuntu允许mysql远程连接 第一步: vim /etc/MySQL/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0 ...
- JS里面的call, apply以及bind
参考了这篇文章:http://www.tuicool.com/articles/EVF3Eb 给几个例子 function add(a,b) { alert(a+b); } function sub( ...
- C#遇见的函数
1.类Stopwatch 提供一组方法和属性,可用于准确地测量运行时间. 命名空间: System.Diagnostics Stopwatch timePerParse = Stopwatc ...
- JSP技术基础(动态网页基础)
前言:如果说html为静态网页基础,那么jsp就是动态网页基础,两者的区别就是jsp在html的前面多加了几行而已.当然,jsp里面对java的支持度更高.要明白,js只是嵌入在客户端的小程序小脚本而 ...
- mysql服务停止
mysql链接方式分为 tcp链接和 sock链接, 你刚才看到服务停止了还能链接 那种会员是 sock的会话模式 所以需要把所有链接mysql的进程结束掉,才能启动起来的 windows ...
- 使用Python与数据库交互
# -*- coding: utf-8 -*- """ Created on Sun Nov 18 19:25:01 2018 @author: wangm " ...
- Spring Boot中使用RSocket
1. 概述 RSocket应用层协议支持 Reactive Streams语义, 例如:用RSocket作为HTTP的一种替代方案.在本教程中, 我们将看到RSocket用在spring boot中, ...
- kubernetes之故障现场二,节点名称冲突
系列目录 问题描述:测试环境由于异常断电导致服务器重启一后,有一个节点的状态一直是NotReady.通过journalctl -f -u kubelet没有错误日志输出.通过tail /var/log ...