hdu5289(2015多校1)--Assignment(单调队列)
Assignment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 503 Accepted Submission(s): 256
ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.
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.
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
5
28HintFirst Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
题目大意:给出一个数列,问当中存在多少连续子序列,子序列的最大值-最小值<k
O(n)的时间复杂度,。。,单调队列太牛叉
用单调队列维护最大值最小值,双指针,第一个第二个指针初始指向第一个数据,第一个指针按顺序不断向队尾加入数据,当最大值最小值的差大于等于k后,就意味着新加入的这个不能作用于当前第二个指针的位置。也就能计算出,以第二个指针位置開始的连续子序列的个数。最后统计总和。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std ;
#define LL __int64
deque <LL> deq1 , deq2 ;
//单调队列。deq1最大值,deq2最小值
LL a[100010] ;
int main() {
int t , n , i , j ;
LL k , ans ;
scanf("%d", &t) ;
while( t-- ) {
scanf("%d %I64d", &n, &k) ;
for(i = 0 ; i < n ; i++)
scanf("%I64d", &a[i]) ;
if(k == 0) {
printf("0\n") ;
continue ;
}
while( !deq1.empty() ) deq1.pop_back() ;
while( !deq2.empty() ) deq2.pop_back() ;
for(i = 0 , j = 0 , ans = 0; i < n ; i++) {//i在前。j在后
while( !deq1.empty() && deq1.back() < a[i] ) deq1.pop_back() ;
deq1.push_back(a[i]) ;
while( !deq2.empty() && deq2.back() > a[i] ) deq2.pop_back() ;
deq2.push_back(a[i]) ;
while( !deq1.empty() && !deq2.empty() && deq1.front() - deq2.front() >= k ) {
ans += (i-j) ;
//printf("%d %d,%I64d %I64d\n", i , j, deq1.front() , deq2.front() ) ;
if( deq1.front() == a[j] ) deq1.pop_front() ;
if( deq2.front() == a[j] ) deq2.pop_front() ;
j++ ;
}
}
while( j < n ) {
ans += (i-j) ;
j++ ;
}
printf("%I64d\n", ans) ;
}
return 0 ;
}
hdu5289(2015多校1)--Assignment(单调队列)的更多相关文章
- HDOJ 5289 Assignment 单调队列
维护一个递增的和递减的单调队列 Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5289 Assignment(单调队列)
题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...
- hdu5289 2015多校联合第一场1002 Assignment
题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...
- HDU 5089 Assignment(rmq+二分 或 单调队列)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 2015 多校联赛 ——HDU5289(二分+ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化
题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...
- 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 ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
随机推荐
- python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。
def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...
- C++ 字符串分割,并把子字符串转换成int型整数
主要涉及到string类的两个函数find和substr: find()函数的用法: 原型:size_t find ( const string& str, size_t pos = 0 ) ...
- python ratelimit使用
1.https://pypi.org/project/ratelimit/
- Python开发:模块
在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法,把这些定义存放在文 ...
- MarkdownPad 2 HTML 渲染错误解决办法
MarkdownPad 2 HTML 渲染错误解决办法 1. 安装SDK工具包 Awesomium 1.6.6 SDK 2. 安装渲染插件Microsoft’s DirectX End-User Ru ...
- Webstrom卡顿问题解决
1.设置node_modules 打开项目,新建node_modules空文件夹,然后右击选择Mark Directory as,选择Excluded. 2.设置ingore文件 files-> ...
- WordPress 多语言支持(本地化)
本博客将介绍WordPress的多语言制作 首先需要在wp-content目录下创建一个languages文件夹,用于存放语言包文件. 然后在模板目录下的functions.php,然后在代码中添加函 ...
- 九度oj 题目1467:二叉排序树
题目描述: 二叉排序树,也称为二叉查找树.可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值: 2. 若右子树 ...
- UTF-8转字典
NSString *textStr = ] forKey:[string substringToIndex:range.location]]; }]; NSLog(@"% ...
- BZOJ1922 [Sdoi2010]大陆争霸 【最短路】
题目 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的 克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭 的神曾·布拉泽,而克里斯国信仰象征光明和永恒的神斯普林 ...