Assignment

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

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]

 
Author
FZUACM
 
Source
题目大意是给出一个长度为N的数组,定义f(l,r)=MAX{ai |  l<=i<=r}-MIN{ai    |  l<=i<=r}
给出个k值,求有多少个不同的(l,r)使得 f(l,r)<k
之所以找到这道源于今天玲珑的一道B,别人说是HDU原题,所以找到看看。
比赛时我想到的就是从没没实战过得尺取法,自认为没问题,却一直WA,当然最后发现是a[maxn],a[minn]手贱打成了maxn,minn,
由于样例太弱以至于没看出来,可惜本来可以A两道题的。
存在更有的对数级算法,涉及到不熟练的ST类算法,日后再看看吧。
 
正文:
对于我们要寻找这个区间[l,r],我们不妨先把r固定住然后找到l的最大范围,接着统计一下加上去,
这个思路的正确性在于,当固定住r时,往左一直走,走到一个不满足条件的位置时,显然其往左的所有位置都是非法的,
换句话说当区间[l,r]不满足条件时,所有大于等于这个区间的区间都不会满足条件。
当然您也可以固定住l,大同小异。
每当循环到一个r时,会出现两种情况,一是加入a[r]之后当前的尺子仍满足条件,我们维护一个L表示尺子的最左端, 此时应执行s+=i-L;
二是加入这个元素之后尺子不满足条件了,为了使尺子再次满足条件,我们需要对L进行操作,找到一个最小的L使其再次满足条件,
显然L最差就是这个位置,即L=r,此时得差值为0一定<k,循环完所有的r,输出答案即可。
用了scanf,280ms水过也不算很慢
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define eps 1e-12
int a[];
int main()
{
int n,k,i,t,j;
LL s=;
cin>>t;
while(t--){s=;
cin>>n>>k;
for(i=;i<=n;++i) scanf("%d",&a[i]);
s=n;
int maxn=,minn=,l=;
for(i=;i<=n;++i){
if(a[i]>=a[maxn]) maxn=i;
if(a[i]<=a[minn]) minn=i;
if(a[maxn]-a[minn]<k)
{
s+=i-l;
}
else
{
maxn=minn=i;
for(j=i;j>=;--j)
{
if(a[j]>=a[maxn])
{
if(a[j]-a[minn]>=k) break;
else maxn=j;
}
if(a[j]<=a[minn])
{
if(a[maxn]-a[j]>=k) break;
else minn=j;
} }
l=j+;
s+=i-l;
}
}cout<<s<<endl;
}
return ;
}

HDU 5289 尺取的更多相关文章

  1. hdu 5672 尺取还是挺好用的

    先简单介绍下尺取法 http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是在卡给定条件的时候 不断的改变下标 起点 终点 #inclu ...

  2. HDU 6205 尺取

    容易看出来,扩增一倍,找最长的区间就行了 /** @Date : 2017-09-11 12:43:11 * @FileName: 1012.cpp * @Platform: Windows * @A ...

  3. 【单调队列+尺取】HDU 3530 Subsequence

    acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...

  4. 【尺取】HDU String

    http://acm.hdu.edu.cn/showproblem.php?pid=5672 [题意] 给定一个小写英语字母组成的字符串,求这个字符串一共包含多少个至少有m个不同字母的连续子序列 [思 ...

  5. 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...

  6. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  7. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  8. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  9. HDU 5672 String 【尺取】

    <题目链接> 题目大意:给定一个只由26个小写字母组成的字符串,现在问你至少包含k个不同字母的连续子序列总数有多少. 解题分析:经仔细研究,我们发现,每次尺取到符合要求的最小区间,然后将区 ...

随机推荐

  1. 基于wtforms源码实现自定义form组件

    from flask import Flask,Markup,render_template,request,redirect from wtforms.form import Form from w ...

  2. python全栈开发从入门到放弃之socket并发编程多线程GIL

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  3. Delphi锁定鼠标 模拟左右键 静止一会自动隐藏鼠标

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. MyBatis For .NET学习-问题总结

    1. MyBatis在进行sqlserver与c# 类型转换时需要注意,sqlserver中dbtype为float时,c#需要使用double与之对应,而不能使用float或decimal 2. M ...

  5. OpenStack的架构详解

    OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云.小云提供可扩展的.灵活的 ...

  6. sparkSQL实战详解

    摘要   如果要想真正的掌握sparkSQL编程,首先要对sparkSQL的整体框架以及sparkSQL到底能帮助我们解决什么问题有一个整体的认识,然后就是对各个层级关系有一个清晰的认识后,才能真正的 ...

  7. JS正则表达式从入门到入土(8)—— REGEXP对象属性

    对象属性 常用对象属性主要有以下几种: 1.global: 是否全文搜索,默认false 2.ignore case:是否大小写敏感,默认是false 3.multiline:多行搜索,默认值是fal ...

  8. Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别

    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...

  9. 20145216史婧瑶《Java程序设计》第8周学习总结

    20145216 <Java程序设计>第8周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 java.util.logging 包提供了日志功能相关类与接口,不必额外配置 ...

  10. 20145301 《Java程序设计》第九周学习总结

    20145301 <Java程序设计>第八周学习总结 教材学习内容总结 整合数据库 JDBC JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作 ...