维护一个递增的和递减的单调队列

Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 78    Accepted Submission(s): 40

Problem Description
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the
ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.
 
Input
In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less
than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability.
 
Output
For each test,output the number of groups.
 
Sample Input
  1. 2
  2. 4 2
  3. 3 1 2 4
  4. 10 5
  5. 0 3 4 5 2 1 6 7 8 9
 
Sample Output
  1. 5
  2. 28
  3. Hint
  4. First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
  5.  
 
Source
 
Recommend
We have carefully selected several similar problems for you:  5299 5298 

pid=5297" style="color:rgb(26,92,200); text-decoration:none">5297 5296 5295 

 

  1. /* ***********************************************
  2. Author :CKboss
  3. Created Time :2015年07月21日 星期二 12时36分35秒
  4. File Name :1002.cpp
  5. ************************************************ */
  6.  
  7. #include <iostream>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <string>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <vector>
  15. #include <queue>
  16. #include <set>
  17. #include <map>
  18.  
  19. using namespace std;
  20.  
  21. typedef long long int LL;
  22. const int maxn=100100;
  23.  
  24. struct Node
  25. {
  26. int val,pos;
  27. };
  28.  
  29. int n,K;
  30. int a[maxn];
  31. // q1 dizheng q2 dijian
  32. deque<Node> q1,q2;
  33.  
  34. LL ans;
  35.  
  36. int main()
  37. {
  38. //freopen("in.txt","r",stdin);
  39. //freopen("out.txt","w",stdout);
  40.  
  41. int T_T;
  42. scanf("%d",&T_T);
  43. while(T_T--)
  44. {
  45. ans=0;
  46. while(!q1.empty()) q1.pop_back();
  47. while(!q2.empty()) q2.pop_back();
  48.  
  49. scanf("%d%d",&n,&K);
  50. for(int i=0;i<n;i++) scanf("%d",a+i);
  51.  
  52. int head=0;
  53. for(int i=0;i<n;i++)
  54. {
  55. Node node = (Node){a[i],i};
  56.  
  57. /// push q1 tail dizheng
  58. while(!q1.empty())
  59. {
  60. Node b = q1.back();
  61. if(b.val<node.val) q1.pop_back();
  62. else break;
  63. }
  64. q1.push_back(node);
  65.  
  66. /// push q2 tail dijian
  67. while(!q2.empty())
  68. {
  69. Node b = q2.back();
  70. if(b.val>node.val) q2.pop_back();
  71. else break;
  72. }
  73. q2.push_back(node);
  74.  
  75. if(i==0) ans++;
  76. else
  77. {
  78. /// bijiao head
  79. while(true)
  80. {
  81. Node big = q1.front();
  82. Node small = q2.front();
  83.  
  84. if(big.val-small.val<K) break;
  85. else
  86. {
  87. if(small.pos<big.pos)
  88. {
  89. head=small.pos+1; q2.pop_front();
  90. }
  91. else
  92. {
  93. head=big.pos+1; q1.pop_front();
  94. }
  95. }
  96. }
  97. ans+=i-head+1;
  98. }
  99. }
  100.  
  101. cout<<ans<<endl;
  102. }
  103.  
  104. return 0;
  105. }

HDOJ 5289 Assignment 单调队列的更多相关文章

  1. HDU 5289 Assignment(单调队列)

    题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...

  2. 二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment

    题目传送门 /* 题意:问有几个区间最大值-最小值 < k 解法1:枚举左端点,二分右端点,用RMQ(或树状数组)求区间最值,O(nlog(n))复杂度 解法2:用单调队列维护最值,O(n)复杂 ...

  3. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  4. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

  5. HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)

    Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...

  6. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  7. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...

  8. hdu5289(2015多校1)--Assignment(单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  9. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

随机推荐

  1. Multiline ComboBox

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. HSSFWorkbook 创建Excel文件

    1.项目代码实例 @Override public OutputStream exportAucLotData(String id, String password, OutputStream out ...

  3. LinuxMint19/LMDE3安装后的设置

    LinuxMint 安装后进行一些设置. 1.设置主板时间为本地时间,默认为UTC时间,与Windows不同,如果双系统,时间有时差. sudo timedatectl set-local-rtc t ...

  4. jquery使用jsonp进行跨域调用

    关于JSONP的概念和为什么要使用JSONP网上已经有很多教程,这一节主要演示下在JQUERY中的ajax方法怎样通过JSONP进行远程调用 首先介绍下$.ajax的参数 type:请求方式 GET/ ...

  5. 要点Java20 java.util.Collections

    java.util.Collections 集合帮助类 演示样例程序(JUnit演示) 排序 @Test public void testSort() { List<Integer> de ...

  6. IIS服务器支持.apk文件下载

    随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法下载此文件 ...

  7. [转]Clean Code Principles: Be a Better Programmer

    原文:https://www.webcodegeeks.com/web-development/clean-code-principles-better-programmer/ ----------- ...

  8. Visual Studio 2015年预览设置: 辅助安装程序说明

    本文介绍了第三方应用程序安装辅助安装的 Visual Studio 2015年预览时安装的说明.如果您安装了多设备开发功能,您需要使用其他第三方软件来处理这些项目.辅助安装程序允许您将部署到您的计算机 ...

  9. import MySQLdb UserWarning

    Finished processing dependencies for MySQL-python==1.2.5 ╭─haoke@haokedeMBP ~/ProgramFiles/MySQL-pyt ...

  10. wait & waitpid 以及子进程传给父进程的信号分析

    wait() 和 waitpid() wait() 系统调用挂起调用进程的执行直到有一个孩子终止.调用 wait(&status) 等价于: waitpid(-1, &status, ...