hdu4521 dp+树状数组
来源:互联网 发布:淘宝页头150px图片 编辑:程序博客网 时间:2024/06/06 00:06
很容易想到是dp,类似于最长公共子序列n^2的求法,但是n^2的做法肯定会TLE的,就要想着优化。
很容易想到线段树,其实树状数组也是可以做的。做dp的时候才用顺推的方法,逐步去更新。
有一个坑点是d可能为0,然后就会TLE了,常见的处理方法,我们把所有的数都加一就好了。
// Created by Chenhongwei in 2015.// Copyright (c) 2015 Chenhongwei. All rights reserved.#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <queue>#include <cmath>#include <map>#include <set>#include <stack>#include <vector>#include <sstream>#include <algorithm>using namespace std;const int inf=1e9;const int maxn=1e5+1000;typedef long long ll;typedef unsigned long long ull;int a[maxn],c[maxn];int dp[maxn];int n,d,L;int lowbit(int x){return x&(-x);}void update(int x,int v){while(x<=L+10){c[x]=max(c[x],v);x+=lowbit(x);}}int maxv(int x){int ret=0;while(x>0){ret=max(ret,c[x]);x-=lowbit(x);}return ret;}int main(){//ios::sync_with_stdio(false);// freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d%d",&n,&d)!=EOF){memset(c,0,sizeof c);memset(dp,0,sizeof dp);int ans=1;L=0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]+=1;L=max(L,a[i]);}for(int i=1;i<=n;i++){dp[i]=max(dp[i],1);update(a[i],dp[i]);if(i+d+1<=n){dp[i+d+1]=max(maxv(a[i+d+1]-1)+1,dp[i+d+1]);ans=max(ans,dp[i+d+1]);}}cout<<ans<<endl;}return 0;}
0 0
- hdu4521 dp+树状数组
- hdu4521 线段树+dp
- hdu5125 dp+树状数组
- HDU5542(dp+树状数组)
- HDU5542(dp + 树状数组)
- hdu 3450 树状数组 + dp
- hdu 2836 树状数组 + DP
- hdu4455之树状数组+DP
- hdu4991 树状数组+DP
- 【HDU4991】dp 树状数组优化
- hdu 3450(树状数组+dp)
- hdu 2227 树状数组+dp
- hdu 4455(dp+树状数组)
- hdu5542 树状数组优化dp
- Sort It (树状数组+dp)
- lightoj 1085 树状数组+dp
- Kattis peaktram (树状数组 DP)
- Codeforces_611D:New_Year_and_Ancient_Prophecy(DP+树状数组)
- Android ndk 入门4 - C++实现
- 时间格式正则表达式
- [Mysql数据库] MySql 获取数据表中随机一条数据
- 安卓项目版本更新(保存xml数据)
- 数列分段
- hdu4521 dp+树状数组
- 单个angular页面只会加载第一个ng-app
- linux下bluetooth编程(三)HCI层编程
- JVM 并发性: 使用 Akka 构建 actor 应用程序
- 轮播图插件
- Ubuntu14.04下安装wineqq国际版和卸载QQ
- Matlab中统计矩阵中相同元素的个数的方法
- 冒泡和捕获小结
- linux下bluetooth编程(四)L2CAP层编程