题目链接: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();
}
}

最新文章

  1. 用profile分析算法性能
  2. Leetcode Bulb Switcher
  3. CSS3 transform对普通元素的N多渲染影响
  4. IOS UICollectionView基础+UICollectionViewFlowLayout基础
  5. python file.tell() 在windows下需要注意的地方
  6. new char[]和new char()的区别
  7. PHP分页类库
  8. [转]Golang- import 导入包的语法
  9. M1: 创建UWP空项目
  10. SUID ,SGID ,Sticky
  11. Jordan Lecture Note-5: Kernels
  12. Weblogic8.1 的性能优化
  13. 简化的nginx多进程模型demo
  14. mysql 查询大量数据内存溢出
  15. 如何在自己的Activity中去控制EditText的焦点
  16. SQL递归查询知多少
  17. javaScript设计模式之----工厂模式
  18. Hbase的基本操作(CDH组件可用)
  19. Newtonsoft.Json高级用法,json序列号,model反序列化,支持序列化和反序列化DataTable,DataSet,Entity Framework和Entity,字符串
  20. 如何为运行的 ARM Linux 启用 LAD2.3 版本的诊断扩展

热门文章

  1. linux grep 查找文件内容
  2. 快速掌握分布式搜索引擎ElasticSearch(一)
  3. Jenkins-------初探
  4. Android Framework 记录之一
  5. Android设置TextView行间距(非行高)
  6. 【bootstrap】使用支持bootstrap的时间插件daterangepicker
  7. Activity的启动模式全解standard,singleTop,singleTask,singleInstance
  8. mac os+selenium2+chrome驱动+python3
  9. visual c++ 2013进行MySQL编程(ODBC) -- (一) 套装安装
  10. android RecycleView复杂多条目的布局