hdu5289(2015多校1)--Assignment(单调队列)
来源:互联网 发布:指定变号发短信软件 编辑:程序博客网 时间:2024/06/01 10:43
Assignment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 503 Accepted Submission(s): 256
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
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9
Sample Output
528HintFirst 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 __int64deque <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 ;}
1 0
- hdu5289(2015多校1)--Assignment(单调队列)
- hdu5289(2015多校1)--Assignment(单调队列)
- HDU5289 Assignment RMQ / 单调队列
- HDU5289(2015多校1)--Assignment
- hdu5289单调队列
- hdu5289 Assignment(多校第一场第二题:RMQ+找规律或单调队列+找规律)
- HDU5289 Assignment
- hdu5289 Assignment
- hdu5289 Assignment
- hdu5289 Assignment
- HDU5289 Assignment
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- HDU5289.Assignment(多校第二题)
- HDU5289:Assignment(二分 + ST表)
- HDOJ 5289 Assignment 单调队列
- HDU 5289 Assignment(单调队列)
- Assignment ZQOJ 25691 单调队列
- ios开发之-继承的实现运用
- 25人赛跑问题-得出前三名
- 黑马66期android学习笔记02-Android简单历史
- Python入门常见错误
- URL validation failed. The error could have been caused through the use of the browser's navigation
- hdu5289(2015多校1)--Assignment(单调队列)
- python 从两个配置文件读取数据,形成一个url思路代码
- 【转载】【MVC】对mvc的认识(载至网上的讨论片段)
- 黑马66期android学习笔记03-Android体系结构
- Single Number II
- 黑马66期android学习笔记04-Dalvik VM和JVM的比较
- 封装标签-1
- VS2008调试C++
- ios-A+B经典问题