hdu 3530 Subsequence

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3530

Subsequence

Description

There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.

Input

There are multiple test cases.
For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range $[1, 100000]$. m and k are in the range $[0, 1000000]$. The second line has n integers, which are all in the range $[0, 1000000]$.
Proceed to the end of file.

Output

For each test case, print the length of the subsequence on a single line.

Sample Input

5 0 0
1 1 1 1 1
5 0 3
1 2 3 4 5

Sample Output

5
4

RMQ线段树。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::min;
10 using std::max;
11 #define sz(c) (int)(c).size()
12 #define all(c) (c).begin(), (c).end()
13 #define iter(c) decltype((c).begin())
14 #define cls(arr,val) memset(arr,val,sizeof(arr))
15 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
16 #define rep(i, n) for (int i = 1; i <= (int)(n); i++)
17 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
18 #define pb(e) push_back(e)
19 #define mp(a, b) make_pair(a, b)
20 #define mid ((l+r)>>1)
21 #define lc (root<<1)
22 #define rc (root<<1|1)
23 const int Max_N = 100010;
24 const int INF = 0x3f3f3f3f;
25 typedef unsigned long long ull;
26 int n, m, k, tmin, tmax;
27 struct SegTree {
28     struct Node { int max, min; }seg[Max_N << 2];
29     inline void built(int root, int l, int r) {
30         if (l == r) {
31             scanf("%d", &seg[root].max), seg[root].min = seg[root].max;
32             return;
33         }
34         built(lc, l, mid);
35         built(rc, mid + 1, r);
36         seg[root].max = max(seg[lc].max, seg[rc].max);
37         seg[root].min = min(seg[lc].min, seg[rc].min);
38     }
39     inline void query(int root, int l, int r, int x, int y) {
40         if (x > r || y < l) return;
41         if (x <= l && y >= r) {
42             tmin = min(tmin, seg[root].min);
43             tmax = max(tmax, seg[root].max);
44             return;
45         }
46         query(lc, l, mid, x, y);
47         query(rc, mid + 1, r, x, y);
48     }
49     inline int query(int l, int r) {
50         tmin = INF, tmax = -INF;
51         query(1, 1, n, l, r);
52         return tmax - tmin;
53     }
54 }seg;
55 int main() {
56 #ifdef LOCAL
57     freopen("in.txt", "r", stdin);
58     freopen("out.txt", "w+", stdout);
59 #endif
60     int l, r, ans;
61     while (~scanf("%d %d %d", &n, &m, &k)) {
62         l = 1, ans = 0;
63         seg.built(1, 1, n);
64         rep(i, n) {
65             r = i;
66             if (l > r) continue;
67             while (seg.query(l, r) > k) l++;
68             if (seg.query(l, r) >= m && seg.query(l, r) <= k) ans = max(ans, r - l + 1);
69         }
70         printf("%d\n", ans);
71     }
72     return 0;
73 }

时间: 2024-10-11 21:44:10

hdu 3530 Subsequence的相关文章

HDU 3530 Subsequence (dp+单调队列)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3530 题意: 找一个最长的区间,区间最大值与最小值的差 大于等于小于等于k 分析: 维护最大值与最小值,然后最大的最大值与最小的最小值的差是不是大于y,大于y谁在前面删除谁,记录起点. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #inc

HDU 3530 Subsequence(单调队列)

Problem Description There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and n

HDU 3530 单调队列

Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3995    Accepted Submission(s): 1308 Problem Description There is a sequence of integers. Your task is to find the longest subsequenc

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/7398272.html 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][1] = dp[i-1][1] 如果第i个字符为1,则dp[i][1

hdu 3530 (单调队列)

Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4441    Accepted Submission(s): 1457 Problem Description There is a sequence of integers. Your task is to find the longest subsequenc

HDU 6155 Subsequence Count 线段树维护矩阵

Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries on the string. There are two types of querie

HDU Common Subsequence (dp)

Common Subsequence Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description A subsequence of a

hdu 6155 -&#160;Subsequence Count

话说这题比赛时候过的好少,连题都没读TOT 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][1] = dp[i-1][1] 如果第i个字符为1,则dp[i][1] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][0] = dp[i-1][0] 显然这是线性递推,我们考虑如何用矩阵表示这种递推关系. 下面分别

hdu 3530 单调队列水题

给你一个数列找到最长的子序列   中的最大值减最小值值m   k之间 建立两个单调队列   一个递增    一个递减    当两个队首满足情况是就进行比较 找到最大值 当不满足是旧的移动队首      怎样移??? 移动队首id较小的一个 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int max(int a,int b) { return a>b?a:b