HDOJ 5289 Assignment 单调队列
来源:互联网 发布:靠谱韩代淘宝 豆瓣 编辑:程序博客网 时间:2024/06/01 10:40
维护一个递增的和递减的单调队列
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
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]
Source
2015 Multi-University Training Contest 1
Recommend
We have carefully selected several similar problems for you: 5299 5298 5297 5296 5295
/* ***********************************************Author :CKbossCreated Time :2015年07月21日 星期二 12时36分35秒File Name :1002.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;typedef long long int LL;const int maxn=100100;struct Node{ int val,pos;};int n,K;int a[maxn];// q1 dizheng q2 dijiandeque<Node> q1,q2;LL ans;int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; scanf("%d",&T_T); while(T_T--) { ans=0; while(!q1.empty()) q1.pop_back(); while(!q2.empty()) q2.pop_back(); scanf("%d%d",&n,&K); for(int i=0;i<n;i++) scanf("%d",a+i); int head=0; for(int i=0;i<n;i++) { Node node = (Node){a[i],i}; /// push q1 tail dizheng while(!q1.empty()) { Node b = q1.back(); if(b.val<node.val) q1.pop_back(); else break; } q1.push_back(node); /// push q2 tail dijian while(!q2.empty()) { Node b = q2.back(); if(b.val>node.val) q2.pop_back(); else break; } q2.push_back(node); if(i==0) ans++; else { /// bijiao head while(true) { Node big = q1.front(); Node small = q2.front(); if(big.val-small.val<K) break; else { if(small.pos<big.pos) { head=small.pos+1; q2.pop_front(); } else { head=big.pos+1; q1.pop_front(); } } } ans+=i-head+1; } } cout<<ans<<endl; } return 0;}
0 0
- HDOJ 5289 Assignment 单调队列
- HDOJ 5289 Assignment 【RMQ 二分 || 单调队列】
- hdoj.5289 Assignment【单调队列】 2015/07/25
- HDU 5289 Assignment(单调队列)
- Hdu-5289 Assignment (二分+RMQ || 单调队列)
- Assignment ZQOJ 25691 单调队列
- HDU5289 Assignment RMQ / 单调队列
- 【多校第一场】【单调队列】HDU 5289 Assignment
- [多校2015.01.1002 单调队列] hdu 5289 Assignment
- hdu 5289 Assignment(RMQ,单调队列,multiset)
- HDOJ-3415(单调队列)
- hdoj--3530 Subsequence(单调队列)
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- hdu5289(2015多校1)--Assignment(单调队列)
- HDU 5089 Assignment(rmq+二分 或 单调队列)
- hdu5289(2015多校1)--Assignment(单调队列)
- 20141002 【 单调队列 】 hdoj 3530 Subsequence
- 5.编写一个java程序,该程序有两个类:Tank(用于刻画坦克)和Fight(主类)。 已知坦克类如下:
- Ubuntu系统常用操作指令说明
- 那个青春
- 修改Velocity over Lifetime
- 关于如何改变thinkphp中自定义的成功界面、错误界面、异常界面
- HDOJ 5289 Assignment 单调队列
- hdu 5288 OO’s Sequence 2015 Multi-University Training Contest 1
- cocos2dx自学之 动作特效与场景切换特效
- Table View Controller注意事项
- A*算法——第二种
- 海量数据相似度计算之simhash短文本查找
- vmware装各种系统
- 6.对学生成绩进行统计计算,参加考试的有6名学生,考试成绩分别为94.5,89.0,79.5,64.5,81.5,73.5,显示考试的总分和平均分,之后显示大于考试平均分的成绩信息。请写出实现上述功能
- hdoj 4552 怪盗基德的挑战书 【KMP 求所有前缀在原串种出现的次数之和】