ZOJ 3349 Special Subsequence
2015-10-03 11:09
561 查看
Special Subsequence
Time Limit: 5000msMemory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 3349
64-bit integer IO format: %lld Java class name: Main
There a sequence S with n integers , and A is a special subsequence that satisfies |Ai-Ai-1| <= d ( 0 <i<=|A|))
Now your task is to find the longest special subsequence of a certain sequence S
Input
There are no more than 15 cases , process till the end-of-file
The first line of each case contains two integer n and d ( 1<=n<=100000 , 0<=d<=100000000) as in the description.
The second line contains exact n integers , which consist the sequnece S .Each integer is in the range [0,100000000] .There is blank between each integer.
There is a blank line between two cases
Output
For each case , print the maximum length of special subsequence you can get.
Sample Input
5 2 1 4 3 6 5 5 0 1 2 3 4 5
Sample Output
3 1
Source
ZOJ Monthly, June 2010Author
CHEN, Zhangyi解题:动态规划+线段树优化
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; int tree[maxn<<2],Lisan[maxn],a[maxn],tot,n,d; int query(int L,int R,int lt,int rt,int v){ if(lt <= L && rt >= R) return tree[v]; int mid = (L + R)>>1,ret = 0; if(lt <= mid) ret = query(L,mid,lt,rt,v<<1); if(rt > mid) ret = max(ret,query(mid + 1,R,lt,rt,v<<1|1)); return ret; } void update(int L,int R,int pos,int val,int v){ if(L == R){ tree[v] = max(tree[v],val); return; } int mid = (L + R)>>1; if(pos <= mid) update(L,mid,pos,val,v<<1); if(pos > mid) update(mid + 1,R,pos,val,v<<1|1); tree[v] = max(tree[v<<1],tree[v<<1|1]); } int main(){ while(~scanf("%d%d",&n,&d)){ memset(tree,0,sizeof tree); for(int i = 0; i < n; ++i){ scanf("%d",a + i); Lisan[i] = a[i]; } sort(Lisan,Lisan + n); tot = unique(Lisan,Lisan + n) - Lisan; int ret = 0; for(int i = 0; i < n; ++i){ int L = max(0,(int)(lower_bound(Lisan,Lisan + tot,a[i] - d) - Lisan + 1)); int R = min(tot,(int)(upper_bound(Lisan,Lisan + tot,a[i] + d) - Lisan)); int tmp = 1,pos = lower_bound(Lisan,Lisan + tot,a[i]) - Lisan + 1; if(L <= R) tmp = query(0,tot,L,R,1) + 1; ret = max(ret,tmp); update(0,tot,pos,tmp,1); } printf("%d\n",ret); } return 0; }
View Code
相关文章推荐
- UVa694 - The Collatz Sequence:Time limit exceeded
- qemu guest agent安装研究
- OC学习笔记之Foundation框架NSNumber、NSValue和NSDate(转)
- Repeated DNA Sequences
- HDU 1159 & POJ 1458 Common Subsequence(LCS 最长公共子序列O(nlogn))
- ucGUI学习笔记之对话框
- 128. Longest Consecutive Sequence (List, Queue)
- iOS的实时搜索:UISearchBar与UISearchController
- UI:修改模拟器类型、多线程理解
- UVALive 6432 Influence // 暴力dfs
- ZOJ 3820 Building Fire Stations
- Debian Stable (Jessie 8.1) 的normal.mod not found
- hdu 5493 Queue(线段树)
- unique_ptr
- 多个线程操作StringBuffer和StringBuilder
- 用g++ 编译 ffmpeg 编译出现 error: 'UINT64_C' was not declared in this scope 或 missing -D__STDC_CONSTANT_MACROS
- codeforces 466D Increase Sequence DP
- [LeetCode]Longest Consecutive Sequence
- String,StringBuffer与StringBuilder的区别
- iOS:UIView、UIControl、UIButton、UILabel简单的属性和方法常识