题目链接:https://www.nowcoder.com/practice/5fe02eb175974e18b9a546812a17428e?tpId=101&tqId=33086&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意

  略。

分析

  利用滑动窗口和两个双端队列维护窗口内最大值和最小值,以每个窗口左边界为基准,看它的窗口右边界最多能到多远,窗口长度即为窗口右边界的贡献。

代码如下

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
  6. #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
  7. #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,0x3f,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef long long LL;
  95. typedef unsigned long long uLL;
  96. typedef vector< int > VI;
  97. typedef vector< bool > VB;
  98. typedef vector< char > VC;
  99. typedef vector< double > VD;
  100. typedef vector< string > VS;
  101. typedef vector< LL > VL;
  102. typedef vector< VI > VVI;
  103. typedef vector< VB > VVB;
  104. typedef vector< VS > VVS;
  105. typedef vector< VL > VVL;
  106. typedef vector< VVI > VVVI;
  107. typedef vector< VVL > VVVL;
  108. typedef pair< int, int > PII;
  109. typedef pair< LL, LL > PLL;
  110. typedef pair< int, string > PIS;
  111. typedef pair< string, int > PSI;
  112. typedef pair< string, string > PSS;
  113. typedef pair< double, double > PDD;
  114. typedef vector< PII > VPII;
  115. typedef vector< PLL > VPLL;
  116. typedef vector< VPII > VVPII;
  117. typedef vector< VPLL > VVPLL;
  118. typedef vector< VS > VVS;
  119. typedef map< int, int > MII;
  120. typedef unordered_map< int, int > uMII;
  121. typedef map< LL, LL > MLL;
  122. typedef map< string, int > MSI;
  123. typedef map< int, string > MIS;
  124. typedef set< int > SI;
  125. typedef stack< int > SKI;
  126. typedef deque< int > DQI;
  127. typedef queue< int > QI;
  128. typedef priority_queue< int > PQIMax;
  129. typedef priority_queue< int, VI, greater< int > > PQIMin;
  130. const double EPS = 1e-;
  131. const LL inf = 0x7fffffff;
  132. const LL infLL = 0x7fffffffffffffffLL;
  133. const LL mod = 1e9 + ;
  134. const int maxN = 1e6 + ;
  135. const LL ONE = ;
  136. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  137. const LL oddBits = 0x5555555555555555;
  138.  
  139. int N, num, arr[maxN];
  140. LL ans;
  141. DQI minDQ, maxDQ;
  142. int p1 = , p2 = ;
  143.  
  144. int main(){
  145. //freopen("MyOutput.txt","w",stdout);
  146. //freopen("input.txt","r",stdin);
  147. //INIT();
  148. scanf("%d%d", &N, &num);
  149. For(i, , N) scanf("%d", &arr[i]);
  150.  
  151. while(p1 <= N && p2 <= N) {
  152. while(!maxDQ.empty() && arr[maxDQ.back()] <= arr[p2]) maxDQ.pop_back();
  153. maxDQ.PB(p2);
  154. while(!minDQ.empty() && arr[minDQ.back()] >= arr[p2]) minDQ.pop_back();
  155. minDQ.PB(p2);
  156.  
  157. if(arr[maxDQ.front()] - arr[minDQ.front()] <= num) ++p2;
  158. else {
  159. if(maxDQ.front() == p1) maxDQ.pop_front();
  160. if(minDQ.front() == p1) minDQ.pop_front();
  161. ans += p2 - p1;
  162. ++p1;
  163. }
  164. }
  165. while(p1 < p2) {
  166. ans += p2 - p1;
  167. ++p1;
  168. }
  169.  
  170. printf("%lld\n", ans);
  171. return ;
  172. }

牛客 最大值减去最小值小于或等于 num 的子数组数量的更多相关文章

  1. 最大值减去最小值小于或等于num的子数组数量

    [说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“最大值减去最小值小于或等于num的子数组数量”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路, ...

  2. 左神算法书籍《程序员代码面试指南》——1_10最大值减去最小值小于或等于num的子数组数量

    [题目]给定数组arr和整数num,共返回有多少个子数组满足如下情况:max(arr[i.j]) - min(arr[i.j]) <= num max(arfi.j])表示子数组ar[ij]中的 ...

  3. 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)

    第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...

  4. [程序员代码面试指南]栈和队列-最大值减去最小值 小于或等于num 的子数组的数量(单调队列)

    题目 给定数组arr和整数num,求数组的子数组中有多少个的满足"最大值减去最小值<=num". 解题思路 分析题目,有结论: 如果数组arr[i...j]满足条件,则它的每 ...

  5. 【队列】最大值减去最小值小于等于num的子数组数量

    摘自<程序员代码面试指南> 题目: 给定数组 arr 和整数 num, 共返回有多少个⼦数组满⾜如下情况:max(arr[i...j]) - min(arr[i...j]) <= n ...

  6. 栈和队列----最大值减去最小值小于等于num的子数组的数量

    最大值减去最小值小于等于num的子数组的数量 给定数组arr和整数 num,共返回有多少个数组满足下列情况: max(arr[i..j])-min(arr[i..j])<=num.其中max(a ...

  7. 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量

    题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...

  8. 算法总结之 最大值减去最小值或等于num的子数组数量

    给定数组arr和整数num,共返回有多少个子数组满足  <= num 数组长度N    时间复杂度O(N) package TT; import java.util.LinkedList; pu ...

  9. [LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

随机推荐

  1. (8)C++ 内存模型与名称空间

    一.单独编译 头文件 不要将函数定义或者变量声明放到头文件中,引入多个文件时可能会造成同一个函数定义多次 引入头文件 #include "文件名" File1.h #ifndef ...

  2. WebGPU学习(九):学习“fractalCube”示例

    大家好,本文学习Chrome->webgpu-samplers->fractalCube示例. 上一篇博文: WebGPU学习(八):学习"texturedCube"示 ...

  3. ubuntu16安装,配置前端开发环境

    1.安装ubuntu    使用usio制作U盘安装工具 2.安装搜狗输入法 3.安装QQ 4.安装nodejs node-v0.12.4 node-v0.12.4.tar.gz root@ubunt ...

  4. ORA-06550/PLS-00103

    原因是单引号‘是需要加转义字符的(即‘—>“)

  5. Hello cnblogs!

    console.log('Hello cnblogs')!

  6. Java不可变对象

    在创建状态后无法更改其状态的对象称为不可变对象.一个对象不可变的类称为不可变类.不变的对象可以由程序的不同区域共享而不用担心其状态改变. 不可变对象本质上是线程安全的. 示例 以下代码创建了不可变类的 ...

  7. Tomcat启动脚本(2)catalina.bat

    @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...

  8. python基础【第七篇】

    字典 列表可以存储大量的数据类型,但是只能按照顺序存储,数据与数据之间关联性不强. 所以咱们需要引入一种容器型的数据类型,解决上面的问题,这就需要dict字典. 字典(dict)是python中唯⼀的 ...

  9. 用dialog包制作窗口

    #!/bin/bash temp=$(mktemp -t test.XXXXXX) temp2=$(mktemp -t test.XXXXXX) function diskspace { df -k ...

  10. java获取网页源代码并写入本地文件中

    import java.io.*; import java.net.*; public class URLDemo { public static void main(String args[]){ ...