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. python之静态属性、类方法、静态方法

    静态属性.类方法.静态方法 1. 静态属性:在函数前加@property,将函数逻辑”封装“成数据属性,外部直接调用函数名,如同调用属性一样.这个函数是可以调用类和实例的属性的,    静态属性的作用 ...

  2. CSS的引入方式和样式

    CSS的引入方式和样式 一.样式 行内样式 内接样式 外接样式(1.链接式 2.导入式) <!--行内样式--> <div> <p style="color: ...

  3. NOIP2018提高组金牌训练营——字符串专题

    NOIP2018提高组金牌训练营——字符串专题 1154 回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式.   a|bb|aabaa - 3 个 ...

  4. (转载)spring 之间的远程调用-Spring Http调用的实现

    原文:https://www.cnblogs.com/lewisat/p/6132082.html 1:Spring Http设计思想 最近在研究公司自己的一套rpc远程调用框架,看到其内部实现的设计 ...

  5. springboot项目封装为docker镜像

    1.本次镜像的基础镜像是:https://www.cnblogs.com/JoeyWong/p/9173265.html 2.将打包好的项目文件放在与Dockerfile同级的目录下 3.Docker ...

  6. POJ 2154

    这题的时间卡的.... 必须用欧拉来优化,而且要加素数表.最重要是,因为最后结果要/n,而数据很大,所以,必须在之前就先/n了,否则会爆数据. #include <iostream> #i ...

  7. VS2008执行MFC程序,提示microsoft incremental linker已停止工作解决方法

    事实上这边是由于设置有问题.详细的解决方式例如以下: 第一步:点击项目->"你的文件"属性->配置属性->链接器->启用增量链接   将  是(/INCRE ...

  8. JavaScript常用的api

    打印日志 console.log 类型判断 第一种方式var type = Object.prototype.toString.call(list);console.log(type);第二种方式ty ...

  9. sicily 1137 河床 (二分分治)

    <计算机算法设计与分析>啃书中... 有点看不进书,就来刷个水题吧,刚开始看错题了还. 注意:是所有测量点相差均不大于di而不是相邻两点... //1137.河床 #include < ...

  10. C#调用GPG命令进行加密解密文件操作

    public void GPG() { string password = "1234567890"; System.Diagnostics.ProcessStartInfo ps ...