Subsequence

一时失言乱红尘 2022-08-03 05:15 133阅读 0赞

Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5092 Accepted Submission(s): 1691

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 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

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

Sample Output

  1. 5
  2. 4

Source

2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[100005];
  7. int main()
  8. {
  9. int n,m,k,i,now,ans;
  10. while(~scanf("%d%d%d",&n,&m,&k))
  11. {
  12. for(i=1;i<=n;i++)
  13. scanf("%d",&a[i]);
  14. deque<int>q;
  15. q.clear();
  16. deque<int>Q;
  17. Q.clear();
  18. now=1,ans=0;
  19. for(i=1;i<=n;i++)
  20. {
  21. while(!q.empty()&&a[i]<a[q.back()])//单调递增序列
  22. q.pop_back();
  23. q.push_back(i);
  24. while(!Q.empty()&&a[i]>a[Q.back()])//单调递减序列
  25. Q.pop_back();
  26. Q.push_back(i);
  27. while(!q.empty()&&!Q.empty()&&a[Q.front()]-a[q.front()]>k)
  28. {
  29. if(q.front()<Q.front())
  30. {
  31. now=q.front()+1;
  32. q.pop_front();
  33. }
  34. else
  35. {
  36. now=Q.front()+1;
  37. Q.pop_front();
  38. }
  39. }
  40. if(a[Q.front()]-a[q.front()]>=m)
  41. {
  42. if(ans<i-now+1)
  43. ans=i-now+1;
  44. // cout<<"ans= "<<ans<<endl;
  45. }
  46. /*
  47. 这一题用两个队列维护区间
  48. 一个单调递增序列一个单调递减序列
  49. 两个头的值相减就是区间的范围
  50. 所以每次插入一个值的时候必须让这个值遵循规则
  51. now只能在数列元素加一的时候才能动!
  52. */
  53. }
  54. printf("%d\n",ans);
  55. }
  56. return 0;
  57. }
  58. /*
  59. 8 2 6
  60. 1 3 8 5 2 7 5 2
  61. */

发表评论

表情:
评论列表 (有 0 条评论,133人围观)

还没有评论,来说两句吧...

相关阅读