Assignment

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

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]

给n个数的序列,找出其中有多少[i , j] 满足最大值和最小值的差小于k

枚举左端,

if(last>0&&abs(num[i-1]-num[last])<k),last记录的是右端最近不满足情况的那个数,假设 i-1到last不满足条件,则说明在i-1<=  x  <=last-1必定有  num[x] - num[last] >= k.    所以判断第i个数时先判断i-1,num[i-1]-num[last])<k 则说明必定存在i<=  x  <=last-1不满足条件,则跳过过这次i判断. (感觉这不是什么正常套路)

官方解:

1. 枚举左端,二分右端,ST求最值

2. 用单调队列维护最值

如果没这个判断则超时,数据真大 - - !!

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib> using namespace std; long long num[101000]; long long s(long long i)
{
return (1+i)*i/2;
} int main()
{
int t, n, k, minx, maxx, last;
long long int re;
//freopen("1002.txt","r",stdin);
scanf("%d", &t);
while(t--)
{
re = 0;
scanf("%d%d", &n, &k);
for(int i=1; i<=n; i++)
{
scanf("%I64d", num+i);
}
last = -1;
for(int i=1; i<=n; i++)
{
if(last>0&&abs(num[i-1]-num[last])<k)
{
continue;
}
minx = num[i];
maxx = num[i];
int j;
for(j=i+1; j<=n; j++)
{
if(abs(num[j]-minx)>=k || abs(num[j]-maxx)>=k)
{
break;
}
else
{
if(num[j]<minx) minx = num[j];
if(num[j]>maxx) maxx = num[j];
}
}
if(last<0)
{
re += s(j-i);
last = j;
}
else if(j>last)
{
re += (s(j-i) - s(last-i));
last = j;
}
}
cout<<re<<endl;
}
return 0;
}< RMQ: #include <cstdio>
#include <iostream> using namespace std; int p[200005];
int dp1[200005][30],dp2[200005][30]; void RMQ_init(int n)
{
for(int i=1; i<=n; i++)
{
dp1[i][0]=p[i];
dp2[i][0]=p[i];
}
for(int j=1; (1<<j)<=n; j++)
{
for(int i=1; i+(1<<j)-1<=n; i++)
{
dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
}
}
} int rmq(int x,int y)
{
int k=0;
while((1<<(k+1))<=y-x+1)k++;
return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
} int main()
{
int T,n,k;
//freopen("1002.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(int i = 1; i <= n; i++)
scanf("%d",&p[i]); long long int num = 0;
RMQ_init(n); int temp = 1;
for(int i = 1; i <= n; i++)
{
while(rmq(temp,i) >= k && temp <i)
temp++;
num += (i-temp+1);
}
printf("%I64d\n",num);
}
return 0;

  

2015 多校联赛 ——HDU5289(二分+ST)的更多相关文章

  1. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  3. 2015 多校联赛 ——HDU5371(manacher + 枚举)

    Sample Input 1 10 2 3 4 4 3 2 2 3 4 4   Sample Output Case #1: 9 要求找出一段数字. 将其分成3部分,第①和第②部分成回文字串,第②和第 ...

  4. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  5. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 2015 多校联赛 ——HDU5294(最短路,最小切割)

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

  7. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  8. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  9. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. Linux学习--进程创建

    进程创建 在Linux系统下,自己可以创建进程: 当进程执行时,它会被装载进虚拟内存,为程序变量分配空间,并把相关信息添到 task_struct里. 进程内存布局分为四个不同的段: • 文本段,包含 ...

  2. highcharts 具体参数详解

    <script type="text/javascript" src="js/jquery.min.js"></script> < ...

  3. OO第一次阶段性总结

    经过三次作业的历练之后终于来到了写博客这一周.回顾开学来的这一个月,令我印象最深刻也是最累的一门课就是OO了.虽然上学期学过一部分Java,但这学期开学就来的OO作业还是让我在第二周就开始熬夜了.不过 ...

  4. 16-TypeScript装饰器模式

    在客户端脚本中,有一个类通常有一个方法需要执行一些操作,当我们需要扩展新功能,增加一些操作代码时,通常需要修改类中方法的代码,这种方式违背了开闭的原则. 装饰器模式可以动态的给类增加一些额外的职责.基 ...

  5. 利用java反射读写csv中的数据

      前一段有个需求需要将从数据库读取到的信息保存到csv文件中,在实现该需求的时候发现资料比较少,经过收集反射和csv相关资料,最终得到了如下程序.  1.在使用java反射读取csv文件数据时,先通 ...

  6. Mego开发文档 - 数据注释建模

    数据注释建模 Mego框架使用一组约定来基于CLR类来构建模型.您可以指定其他配置来补充或覆盖通过约定发现的内容. 在 Mego 中所有的数据对象必须要有主键.这里需要声明与EF不同的是框架只支持数据 ...

  7. mySql一个字段的值模糊匹配多表多列的查询

    1.dao层/** * 分页查询点卡集合信息 * @param tid 游戏类型编号 * @param gid 游戏编号 * @param searchInfo 包括(点卡名称,游戏名称,点卡面值,游 ...

  8. kubernetes入门(04)kubernetes的核心概念(1)

    一.ReplicationController/ReplicaSet 在Kubernetes集群中,ReplicationController能够确保在任意时刻,指定数量的Pod副本正在运行.如果Po ...

  9. zuul入门(5)zuul 处理异常

    Object accessToken = request.getParameter("accessToken"); if(accessToken==null) { // 设置zuu ...

  10. Spring Security 入门(3-11)Spring Security 的使用-自定义登录验证和回调地址

    配置文件 security-ns.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...