Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 297    Accepted Submission(s): 152

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
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
 
Sample Output
5
28
 
Hint

First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]

 
Source
 
解题:线段树+尺取法 或者 单调队列 然而我不会单调队列 也可以用st表
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct node {
int lt,rt,minv,maxv;
} tree[maxn<<];
int d[maxn];
inline void pushup(int v) {
tree[v].minv = min(tree[v<<].minv,tree[v<<|].minv);
tree[v].maxv = max(tree[v<<].maxv,tree[v<<|].maxv);
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
tree[v].minv = tree[v].maxv = d[lt];
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
int query(int lt,int rt,int v,bool qmax) {
if(lt <= tree[v].lt && rt >= tree[v].rt)
return qmax?tree[v].maxv:tree[v].minv;
int ret = qmax?INT_MIN:INT_MAX;
if(lt <= tree[v<<].rt)
ret = qmax?max(ret,query(lt,rt,v<<,qmax)):min(ret,query(lt,rt,v<<,qmax));
if(rt >= tree[v<<|].lt)
ret = qmax? max(ret,query(lt,rt,v<<|,qmax)):min(ret,query(lt,rt,v<<|,qmax));
return ret;
}
int main() {
int n,m,kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i)
scanf("%d",d+i);
build(,n-,);
int low = ,high = ;
LL ret = ;
while(low <= high && high < n) {
int minv = query(low,high,,false);
int maxv = query(low,high,,true);
if(maxv - minv < m) ret += high-low+;
if(maxv - minv >= m) low++;
else high++;
}
cout<<ret<<endl;
}
return ;
}

2015 Multi-University Training Contest 1 Assignment的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  2. 2015 Multi-University Training Contest 1(7/12)

    2015 Multi-University Training Contest 1 A.OO's Sequence 计算每个数的贡献 找出第\(i\)个数左边最靠右的因子位置\(lp\)和右边最靠左的因 ...

  3. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...

  4. 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】

    2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...

  5. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  6. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  7. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  8. hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)

    OO's Sequence                                                          Time Limit: 4000/2000 MS (Jav ...

  9. HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

随机推荐

  1. 编写高性能的javascript代码(持续更新)

    参考资料: Vanilla JS——世界上最轻量的JavaScript框架(没有之一) http://segmentfault.com/a/1190000000355277 探索高效jQuery的奥秘 ...

  2. NOIP2016 天天爱跑步 (树上差分+dfs)

    题目大意:给你一颗树,树上每个点都有一个观察员,他们仅会在 w[i] 时刻出现,观察正在跑步的玩家 一共有m个玩家,他们分别从节点 s[i] 同时出发,以每秒跑一条边的速度,沿着到 t[i] 的唯一路 ...

  3. Python socket通信之FTP

    Python中利用socket进行server端和client端通信是网络编程的基础,是最简单的传输范例. (懂网络的请自动跳过这一部分) 首先,要想通信,必须建立连接,建立连接的过程,需要clien ...

  4. django-7-django模型系统

    <<<常用的模型字段类型>>>https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-type ...

  5. IDEA Maven Web项目 clone到本地导入到Eclipse中,启动服务器的时候会出现这个错误:SEVERE: Exception starting filter [hiddenHttpMethodFilter]

    背景(Background): 我将一个IDEA的maven web项目clone到本地,并导入到Eclipse中. I imported a MAVEN WEB project which was ...

  6. [luogu] P3294 [SCOI2016]背单词 (贪心)

    题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,"我怎么样才能快点学完,然后去玩三国杀呢?".这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他 ...

  7. 20130907.Git学习记录

    1.任何文件在Git内都只有三种状态: ①已提交(committed):已提交表示该文件已经被安全地保存在本地数据库中了: ②已修改(modified):已修改表示修改了某个文件,但还没有提交保存: ...

  8. unity 支持圆形、切倒角和虚化UGUI Shader

    // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt ...

  9. js基础——事件绑定(事件监听)

    JavaScript事件一共有三种监听方法分别如下: 1.事件监听一夹杂在html标签内 <div id="box" onClick="alert('HELLO W ...

  10. 第九章 Servlet API

    第九章 Servlet API Servlet API 定义了Servlet和服务器之间的一个标准接口,这使得Servlet具有跨应用服务器的特性,通过使用Servlet API,开发人员不必关心服务 ...