牛客 最大值减去最小值小于或等于 num 的子数组数量
题目大意
分析
代码如下
- #include <bits/stdc++.h>
- using namespace std;
- #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
- #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
- #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
- #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
- #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
- #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
- #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
- #define pr(x) cout << #x << " = " << x << " "
- #define prln(x) cout << #x << " = " << x << endl
- #define LOWBIT(x) ((x)&(-x))
- #define ALL(x) x.begin(),x.end()
- #define INS(x) inserter(x,x.begin())
- #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
- #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
- #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
- #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
- #define ms0(a) memset(a,0,sizeof(a))
- #define msI(a) memset(a,0x3f,sizeof(a))
- #define msM(a) memset(a,-1,sizeof(a))
- #define MP make_pair
- #define PB push_back
- #define ft first
- #define sd second
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &p) {
- in >> p.first >> p.second;
- return in;
- }
- template<typename T>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v)
- in >> x;
- return in;
- }
- template<typename T>
- ostream &operator<<(ostream &out, vector<T> &v) {
- Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
- return out;
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
- out << "[" << p.first << ", " << p.second << "]" << "\n";
- return out;
- }
- inline int gc(){
- static const int BUF = 1e7;
- static char buf[BUF], *bg = buf + BUF, *ed = bg;
- if(bg == ed) fread(bg = buf, , BUF, stdin);
- return *bg++;
- }
- inline int ri(){
- int x = , f = , c = gc();
- for(; c<||c>; f = c=='-'?-:f, c=gc());
- for(; c>&&c<; x = x* + c - , c=gc());
- return x*f;
- }
- template<class T>
- inline string toString(T x) {
- ostringstream sout;
- sout << x;
- return sout.str();
- }
- inline int toInt(string s) {
- int v;
- istringstream sin(s);
- sin >> v;
- return v;
- }
- //min <= aim <= max
- template<typename T>
- inline bool BETWEEN(const T aim, const T min, const T max) {
- return min <= aim && aim <= max;
- }
- typedef long long LL;
- typedef unsigned long long uLL;
- typedef vector< int > VI;
- typedef vector< bool > VB;
- typedef vector< char > VC;
- typedef vector< double > VD;
- typedef vector< string > VS;
- typedef vector< LL > VL;
- typedef vector< VI > VVI;
- typedef vector< VB > VVB;
- typedef vector< VS > VVS;
- typedef vector< VL > VVL;
- typedef vector< VVI > VVVI;
- typedef vector< VVL > VVVL;
- typedef pair< int, int > PII;
- typedef pair< LL, LL > PLL;
- typedef pair< int, string > PIS;
- typedef pair< string, int > PSI;
- typedef pair< string, string > PSS;
- typedef pair< double, double > PDD;
- typedef vector< PII > VPII;
- typedef vector< PLL > VPLL;
- typedef vector< VPII > VVPII;
- typedef vector< VPLL > VVPLL;
- typedef vector< VS > VVS;
- typedef map< int, int > MII;
- typedef unordered_map< int, int > uMII;
- typedef map< LL, LL > MLL;
- typedef map< string, int > MSI;
- typedef map< int, string > MIS;
- typedef set< int > SI;
- typedef stack< int > SKI;
- typedef deque< int > DQI;
- typedef queue< int > QI;
- typedef priority_queue< int > PQIMax;
- typedef priority_queue< int, VI, greater< int > > PQIMin;
- const double EPS = 1e-;
- const LL inf = 0x7fffffff;
- const LL infLL = 0x7fffffffffffffffLL;
- const LL mod = 1e9 + ;
- const int maxN = 1e6 + ;
- const LL ONE = ;
- const LL evenBits = 0xaaaaaaaaaaaaaaaa;
- const LL oddBits = 0x5555555555555555;
- int N, num, arr[maxN];
- LL ans;
- DQI minDQ, maxDQ;
- int p1 = , p2 = ;
- int main(){
- //freopen("MyOutput.txt","w",stdout);
- //freopen("input.txt","r",stdin);
- //INIT();
- scanf("%d%d", &N, &num);
- For(i, , N) scanf("%d", &arr[i]);
- while(p1 <= N && p2 <= N) {
- while(!maxDQ.empty() && arr[maxDQ.back()] <= arr[p2]) maxDQ.pop_back();
- maxDQ.PB(p2);
- while(!minDQ.empty() && arr[minDQ.back()] >= arr[p2]) minDQ.pop_back();
- minDQ.PB(p2);
- if(arr[maxDQ.front()] - arr[minDQ.front()] <= num) ++p2;
- else {
- if(maxDQ.front() == p1) maxDQ.pop_front();
- if(minDQ.front() == p1) minDQ.pop_front();
- ans += p2 - p1;
- ++p1;
- }
- }
- while(p1 < p2) {
- ans += p2 - p1;
- ++p1;
- }
- printf("%lld\n", ans);
- return ;
- }
牛客 最大值减去最小值小于或等于 num 的子数组数量的更多相关文章
- 最大值减去最小值小于或等于num的子数组数量
[说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“最大值减去最小值小于或等于num的子数组数量”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路, ...
- 左神算法书籍《程序员代码面试指南》——1_10最大值减去最小值小于或等于num的子数组数量
[题目]给定数组arr和整数num,共返回有多少个子数组满足如下情况:max(arr[i.j]) - min(arr[i.j]) <= num max(arfi.j])表示子数组ar[ij]中的 ...
- 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)
第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...
- [程序员代码面试指南]栈和队列-最大值减去最小值 小于或等于num 的子数组的数量(单调队列)
题目 给定数组arr和整数num,求数组的子数组中有多少个的满足"最大值减去最小值<=num". 解题思路 分析题目,有结论: 如果数组arr[i...j]满足条件,则它的每 ...
- 【队列】最大值减去最小值小于等于num的子数组数量
摘自<程序员代码面试指南> 题目: 给定数组 arr 和整数 num, 共返回有多少个⼦数组满⾜如下情况:max(arr[i...j]) - min(arr[i...j]) <= n ...
- 栈和队列----最大值减去最小值小于等于num的子数组的数量
最大值减去最小值小于等于num的子数组的数量 给定数组arr和整数 num,共返回有多少个数组满足下列情况: max(arr[i..j])-min(arr[i..j])<=num.其中max(a ...
- 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量
题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...
- 算法总结之 最大值减去最小值或等于num的子数组数量
给定数组arr和整数num,共返回有多少个子数组满足 <= num 数组长度N 时间复杂度O(N) package TT; import java.util.LinkedList; pu ...
- [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 ...
随机推荐
- (8)C++ 内存模型与名称空间
一.单独编译 头文件 不要将函数定义或者变量声明放到头文件中,引入多个文件时可能会造成同一个函数定义多次 引入头文件 #include "文件名" File1.h #ifndef ...
- WebGPU学习(九):学习“fractalCube”示例
大家好,本文学习Chrome->webgpu-samplers->fractalCube示例. 上一篇博文: WebGPU学习(八):学习"texturedCube"示 ...
- ubuntu16安装,配置前端开发环境
1.安装ubuntu 使用usio制作U盘安装工具 2.安装搜狗输入法 3.安装QQ 4.安装nodejs node-v0.12.4 node-v0.12.4.tar.gz root@ubunt ...
- ORA-06550/PLS-00103
原因是单引号‘是需要加转义字符的(即‘—>“)
- Hello cnblogs!
console.log('Hello cnblogs')!
- Java不可变对象
在创建状态后无法更改其状态的对象称为不可变对象.一个对象不可变的类称为不可变类.不变的对象可以由程序的不同区域共享而不用担心其状态改变. 不可变对象本质上是线程安全的. 示例 以下代码创建了不可变类的 ...
- Tomcat启动脚本(2)catalina.bat
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...
- python基础【第七篇】
字典 列表可以存储大量的数据类型,但是只能按照顺序存储,数据与数据之间关联性不强. 所以咱们需要引入一种容器型的数据类型,解决上面的问题,这就需要dict字典. 字典(dict)是python中唯⼀的 ...
- 用dialog包制作窗口
#!/bin/bash temp=$(mktemp -t test.XXXXXX) temp2=$(mktemp -t test.XXXXXX) function diskspace { df -k ...
- java获取网页源代码并写入本地文件中
import java.io.*; import java.net.*; public class URLDemo { public static void main(String args[]){ ...