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. 团队作业7——第二次项目冲刺(Beta版本12.10)

    项目每个成员的进展.存在问题.接下来两天的安排. 已完成的内容:头像功能原型设计.头像裁剪功能.头像上传功能.测试 计划完成的内容:头像功能测试.bug修复 每个人的工作 (有work item 的I ...

  2. mysql命令行大全

      1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  3. 【编程开发】PHP---面向对象

    面向对象编程 类:在现实世界中,任何事物都有种类的概念:车 类是由特征和行为构成的. 特征:都是不动的,从出厂的时候就已经内置好了(属性) 行为:一种动的状态.(方法(函数)) 行为依赖于这些特征,而 ...

  4. django启动uwsgi报错

    查看uwsgi.log *** Starting uWSGI 2.0.17 (64bit) on [Thu Apr 5 17:46:15 2018] *** compiled with version ...

  5. 前端双引号单引号,正则反向引用,js比较jq

    1.js,jq,css,html属性必须双,如果同时出现需要嵌套使用,属性的规范是双但是也可以用单测试有效 单引号现象举例:jq中获取元素标签是单引号:$('input').click:弹出也是单引号 ...

  6. Java KeyTool command

    Create a new key: keytool -genkey -alias keyAlias -keyalg RSA  -validity 1000 -keystore d:\keyPath\k ...

  7. OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)

    授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...

  8. Linux网络配置(setup)

    新装的Linux系统,想要快捷的网络配置,首选setup命令. 1.设置setup为中文. echo LANG="zh_CN.UTF-8" > /etc/sysconfig/ ...

  9. python flask框架 数据库的使用

    #coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # ...

  10. Vue框架之双向绑定事件

    Vue框架之双向绑定事件 首先介绍下Vue框架的语法 vue通过 {{temp}} 来渲染变量 {{count+100}} # 求和 v-text # 为标签插入text文本 v-html # 为标签 ...