ZOJ3349——Special Subsequence

Special Subsequence


Time Limit: 5 Seconds     
Memory Limit: 32768 KB



There a sequence S with n integers , and A is a special subsequence thatsatisfies |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 .Eachinteger 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

水dp+水线段树

哈哈今天的第一个1Y

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int dp[N], xis[N], arr[N];
int n, d, cnt;

struct node
 {
 	int l, r;
 	int val;
 }tree[N << 2];

 int BinSearch(int x)
 {
 	int l = 1, r = cnt, mid;
 	while (l <= r)
 	{
 		mid = (l + r) >> 1;
 		if (xis[mid] > x)
 		{
 			r = mid - 1;
 		}
 		else if (xis[mid] < x)
 		{
 			l = mid + 1;
 		}
 		else
 		{
 			break;
 		}
 	}
 	return mid;
 } 

int left_binsearch(int x)
{
	int l = 1, r = cnt, mid, ans = -1;
	while (l <= r)
	{
		mid = (l + r) >>1;
		if (xis[mid] >= x)
		{
			ans = mid;
			r = mid - 1;
		}
		else
		{
			l = mid + 1;
		}
	}
	return ans;
}

int right_binsearch(int x)
{
	int l = 1, r = cnt, mid, ans = -1;
	while (l <= r)
	{
		mid = (l + r) >>1;
		if (xis[mid] <= x)
		{
			ans = mid;
			l = mid + 1;
		}
		else
		{
			r = mid - 1;
		}
	}
	return ans;
}

 void build(int p, int l, int r)
 {
 	tree[p].l = l;
 	tree[p].r = r;
 	tree[p].val = -1;
 	if (l == r)
 	{
 		return;
 	}
 	int mid = (l + r) >> 1;
 	build(p << 1, l, mid);
 	build(p << 1 | 1, mid + 1, r);
 }

 void update(int p, int pos, int val)
 {
 	if (tree[p].l == tree[p].r)
 	{
 		tree[p].val = max(tree[p].val, val);
 		return;
 	}
 	int mid = (tree[p].l + tree[p].r) >> 1;
 	if (pos <= mid)
 	{
 		update(p << 1, pos, val);
 	}
 	else
 	{
 		update(p << 1 | 1, pos, val);
 	}
 	tree[p].val = max(tree[p << 1].val, tree[p << 1 | 1].val);
 }

 int query(int p, int l, int r)
 {
 	if (l <= tree[p].l && r >= tree[p].r)
 	{
 		return tree[p].val;
 	}
 	int mid = (tree[p].l + tree[p].r) >> 1;
 	if (r <= mid)
 	{
 		return query(p << 1, l, r);
 	}
 	else if (l > mid)
 	{
 		return query(p << 1 | 1, l, r);
 	}
 	else
 	{
 		return max(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r));
 	}
 }

int main()
{
	while(~scanf("%d%d", &n, &d))
	{
		cnt = 0;
		int ans = 0;
		memset (dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &arr[i]);
			xis[++cnt] = arr[i];
		}
		sort (xis + 1, xis + cnt + 1);
		cnt = unique(xis + 1, xis + cnt + 1) - xis - 1;
		build(1, 1, cnt);
		for (int i = 1; i <= n; ++i)
		{
			int cur = BinSearch(arr[i]);
			int l = left_binsearch(arr[i] - d);
			int r = right_binsearch(arr[i] + d);
			if (l < 0)
			{
				l = 1;
			}
			if (r < 0)
			{
				r = cnt;
			}
			int tmp = query(1, l, r);
			if (tmp == -1)
			{
				dp[i] = 1;
			}
			else
			{
				dp[i] = tmp + 1;
			}
			update(1, cur, dp[i]);
			ans = max(ans, dp[i]);
		}
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-10-16 14:38:42

ZOJ3349——Special Subsequence的相关文章

ZOJ 3349 Special Subsequence

Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 334964-bit integer IO format: %lld      Java class name: Main There a sequence S with n integers , and A is a special subsequence that satis

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

334. Increasing Triplet Subsequence

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k  such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else retur

[LeetCode] Increasing Triplet Subsequence 递增的三元子序列

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return

300. Longest Increasing Subsequence java solutions

Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than o

LeetCode | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列[Easy][Python][双指针] Problem LeetCode Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the