1551: Longest Increasing Subsequence Again

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 267  Solved: 112
[Submit][Status][Web Board]

Description

Give
you a numeric sequence. If you can demolish arbitrary amount of
numbers, what is the length of the longest increasing sequence, which is
made up of consecutive numbers? It sounds like
Longest Increasing Subsequence at first sight. So, there is another
limitation: the numbers you deleted must be consecutive.

Input

There are several test cases.
For each test case, the first line of input contains the length of
sequence N(1≤N≤10^4). The second line contains the elements of
sequence——N positive integers not larger than 10^4.

Output

For each the case,
output one integer per line, denoting the length of the longest
increasing sequence of consecutive numbers, which is achievable by
demolishing some(may be zero) consecutive numbers.

Sample Input

7
1 7 3 5 9 4 8
6
2 3 100 4 4 5

Sample Output

4
4 题意:给出一个序列,删除任意一段连续的数(也可以不删除),删完后最长严格递增子段(序列必须是连续的)最长。
题解:对付这种题的能力不行啊,看了题解做的.
分析:dp[i][0]代表未删除时以i结尾最长连续上升子序列长度,
dp[i][1]代表删除时以i结尾最长连续上升子序列长度
不删除时很好处理,递推过去就好了,所以可以预处理 dp[i][0]
dp[i][1]可以从dp[i][0]转移过来
1.dp[i][1] = dp[i-1][1]+1 (a[i]>a[i-1])
2.dp[i][1] = dp[j][0]+1 (a[i]>a[j]&&i>j) 这里找dp[j][0]可以利用线段树维护.
这里的更新过程和查询与此题有异曲同工之妙 http://www.cnblogs.com/liyinggang/p/5656485.html
/**分析:dp[i][0]代表未删除时以i结尾最长连续上升子序列长度,
dp[i][1]代表删除时以i结尾最长连续上升子序列长度
不删除时很好处理,递推过去就好了,所以可以预处理 dp[i][0]
dp[i][1]可以从dp[i][0]转移过来
1.dp[i][1] = dp[i-1][1]+1 (a[i]>a[i-1])
2.dp[i][1] = dp[j][0]+1 (a[i]>a[j]&&i>j)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define N 10005
int dp[N][],a[N],b[N];
int tree[N<<],MAX;
void pushup(int idx)
{
tree[idx] = max(tree[idx<<],tree[idx<<|]);
}
void build(int l,int r,int idx)
{
if(l==r)
{
tree[idx] = ;
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
pushup(idx);
}
void update(int pos,int v,int l,int r,int idx)
{
if(l==r)
{
tree[idx] = max(tree[idx],v);
return;
}
int mid = (l+r)>>;
if(pos<=mid) update(pos,v,l,mid,idx<<);
else update(pos,v,mid+,r,idx<<|);
pushup(idx);
}
void query(int l,int r,int L,int R,int idx)
{
if(l>=L&&R>=r)
{
MAX = max(MAX,tree[idx]);
return;
}
int mid = (l+r)>>;
if(mid>=L) query(l,mid,L,R,idx<<);
if(mid<R) query(mid+,r,L,R,idx<<|);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(b+,b++n);
int cnt = unique(b+,b++n)-b;
for(int i=; i<=n; i++) a[i] = lower_bound(b+,b+cnt,a[i]) - b;
for(int i=; i<=n; i++)
{
dp[i][] = ;
if (i > && a[i]>a[i - ])
dp[i][] = dp[i - ][] + ;
}
build(,n,);
int ans = -;
for(int i=; i<=n; i++)
{
dp[i][] = ;
if(i>&&a[i]>)
{
if(a[i]>a[i-]) dp[i][] = dp[i-][]+;
MAX = -;
query(,n,,a[i]-,); /**找到最大的 dp[j][0] 满足 a[j]<a[i] && j<i,有必要解释一下这里
为什么是更新1~a[i]-1呢?因为我们找的是按照大小排名来的数,而不是,比如说 1 3 5 6 4
假设我找的是 a[5] ,所以我应该忽略中间的 5 6 ,直接找 1 3 .而我们的枚举顺序也保证了 j<i 这个条件.
个人觉得很巧妙.
*/
dp[i][] = max(dp[i][],MAX+);
}
update(a[i],dp[i][],,n,);/**
更新过程同样巧妙,也是更新的按照大小排名来的顺序,不要与下标弄混了
*/
ans = max(ans,max(dp[i][],dp[i][]));
}
printf("%d\n",ans);
}
return ;
}

csu 1551(线段树+DP)的更多相关文章

  1. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  2. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  4. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  5. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  6. HDU4521+线段树+dp

    题意:在一个序列中找出最长的某个序列.找出的序列满足题中的条件. 关键:对于 第 i 个位置上的数,要知道与之相隔至少d的位置上的数的大小.可以利用线段树进行统计,查询.更新的时候利用dp的思想. / ...

  7. Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

  8. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

  9. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

随机推荐

  1. [Wf2011]Chips Challenge

    两个条件都不太好处理 每行放置的个数实际很小,枚举最多放x 但还是不好放 考虑所有位置先都放上,然后删除最少使得合法 为了凑所有的位置都考虑到,把它当最大流 但是删除最少,所以最小费用 行列相关,左行 ...

  2. 万圣节后的早晨&&九数码游戏——双向广搜

    https://www.luogu.org/problemnew/show/P1778 https://www.luogu.org/problemnew/show/P2578 双向广搜. 有固定起点终 ...

  3. Linux用户创建及权限管理

    作业一: 1,新建用户natasha,uid为1000,gid为555,备注信息为“master” useradd natasha            vim /etc/passwd         ...

  4. php网摘收藏

    1.thinkphp3.2.3开发手册: http://document.thinkphp.cn/manual_3_2.html 2.ThinkPHP3.2.3的函数汇总:http://www.thi ...

  5. eclipse+myeclipse 使用技巧备忘

    myeclipse 导入多模块maven项目 https://blog.csdn.net/jack85986370/article/details/51371853 maven项目在eclipse的l ...

  6. echart图跟随屏幕自适应变化

    var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...

  7. HTML+css零碎小知识

    1.设置了float浮动的元素和绝对定位position:absolute的元素会脱离正常的文档流.但是设置absolute的元素不会占据空间,相当于隐形了.   2.相对定位position:rel ...

  8. maven 依赖、聚合和继承 (转)

    Maven 插件和仓库 Maven 本质上是一个插件框架,它的核心并不执行任何具体的构建任务,仅仅定义了抽象的生命周期,所有这些任务都交给插件来完成的.每个插件都能完成至少一个任务,每个任务即是一个功 ...

  9. 条件转化,2-sat BZOJ 1997

    http://www.lydsy.com/JudgeOnline/problem.php?id=1997 1997: [Hnoi2010]Planar Time Limit: 10 Sec  Memo ...

  10. Python学习笔记(补充)Split 用法

    >>> u = "www.doiido.com.cn" #使用默认分隔符 >>> print u.split() ['www.doiido.co ...