Assignment

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

Total Submission(s): 557    Accepted Submission(s): 280

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
 
Recommend
We have carefully selected several similar problems for you:  5299 5298 5297 5296 5295 

rmq+二分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
typedef __int64 ll;
const int mod=1e9+7; using namespace std; #define N 100007 int a[N],n;
int dpmin[N][25],dpmax[N][25];
int k; inline bool judge(int le,int ri)
{
int kk=log2((ri-le+1)*1.0);
int mi=min(dpmin[le][kk],dpmin[ri-(1<<kk)+1][kk]);
int ma=max(dpmax[le][kk],dpmax[ri-(1<<kk)+1][kk]);
return ma-mi<k;
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
dpmin[i][0]=dpmax[i][0]=a[i]; for(j=1;(1<<j)<=n;j++)
for(i=1;i+(1<<j)-1<=n;i++)
{
int p=1<<(j-1);
dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+p][j-1]);
dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+p][j-1]);
} __int64 ans=0;
int le,ri,p;
for(i=1;i<=n;i++)
{
le=i;
ri=n;
while(le<=ri)
{
int mid=(le+ri)>>1;
if(judge(i,mid))
{
p=mid;
le=mid+1;
}
else
ri=mid-1;
}
ans+=p-i+1;
}
printf("%I64d\n",ans);
}
return 0;
}

单调队列

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8 using namespace std;
const int mod=1e9+7; #define INF 0x3f3f3f3f const int N=100005; int mique[N],maque[N],mihead,mahead,mitail,matail;
int n,a[N],k; int pre,now;
__int64 ans; inline void miinque(int i)
{
while(mihead<mitail&&a[i]<a[mique[mitail-1]]) mitail--;
mique[mitail++]=i;
} inline void mainque(int i)
{
while(mahead<matail&&a[i]>a[maque[matail-1]]) matail--;
maque[matail++]=i;
} void outque(int pos)
{
if(a[maque[mahead]]-a[mique[mihead]]>=k)
{
int nowlen=pos-now-1;
int prelen=pre-now;
ans+=(__int64)(nowlen+1)*nowlen/2;
if(prelen>=1)
ans-=(__int64)(prelen+1)*prelen/2;
pre=pos-1;
}
while(a[maque[mahead]]-a[mique[mihead]]>=k)
if(mique[mihead]<maque[mahead])
{
now=mique[mihead]+1;
mihead++;
}
else
{
now=maque[mahead]+1;
mahead++;
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
mihead=mahead=mitail=matail=0;
pre=now=1;
ans=0;
for(i=1;i<=n;i++)
{
miinque(i);
mainque(i);
outque(i);
}
if(pre<n)
{
int nowlen=n-now;
int prelen=pre-now;
ans+=(__int64)(nowlen+1)*(nowlen)/2;
if(prelen>=1)
ans-=(__int64)(prelen+1)*(prelen)/2;
pre=n-1;
} printf("%I64d\n",ans+n);
}
return 0;
}

HDU 5089 Assignment(rmq+二分 或 单调队列)的更多相关文章

  1. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  2. BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列

    BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列 题意: 分析: 拆成链,二分答案,奇偶两个单调队列维护最大子段和,记录方案. 代码: #include <cstdio&g ...

  3. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  4. [NOIP2017普及组]跳房子(二分,单调队列优化dp)

    [NOIP2017普及组]跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 nn 个格子, ...

  5. 感染(low)bfs 、感染(mid) 二分、感染(high) 二分 + 维护单调 队列去除无用的点

    感染(low) Description n户人家住在一条直线上,从左往右依次编号为1,2,-,n.起初,有m户人家感染了COVID-19,而接下来的每天感染的人家都会感染他家左右两家的人,问t天后总共 ...

  6. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  7. codeforces 251A Points on Line(二分or单调队列)

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  8. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  9. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

随机推荐

  1. 一个Lucene.Net的Demo

    今天突然想来看一下全文检索,于是就了解了一下Lucene.Net,然后把公司目前的产品表拿来练手,写了这么个Demo. 先看一下Demo的代码 public class ProductReposito ...

  2. Linux下使用vi命令后退出方式

    退出Vi         当编辑完文件,准备退出Vi返回到shell时,可以使用以下几种方法之一.         在命令模式中,连按两次大写字母Z,若当前编辑的文件曾被修改过,则Vi保存该文件后退出 ...

  3. Android典型界面设计-访网易新闻实现双导航tab切换

    一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...

  4. cc.Component

    组件入口函数1: onLoad: 组件加载的时候调用, 保证了你可以获取到场景中的其他节点,以及节点关联的资源数据;2: start: 也就是第一次执行 update 之前触发;3: update(d ...

  5. 在single模式修改密码

    当前是在CentOS 6.5 的命令行模式下 开机后在这个界面按随意键 接着来到新界面中,在当前选项中按下e键: 接着来到新界面,选择第二个,按下e键: 在这里我们输入single接着回车: 接着回到 ...

  6. 大页(Huge Page)简单介绍

    x86(包括x86-32和x86-64)架构的CPU默认使用4KB大小的内存页面(getconf PAGESIZE),但是它们也支持较大的内存页,如x86-64系统就支持2MB大小的大页(huge p ...

  7. blocking and nonblocking assign

    key word: 仿真建模  clock采样block/nonblock blocking时,有时候clk会sample edge后的data: nobocking时,clk sample 以前的d ...

  8. 数据结构实验6:C++实现二叉树类

    实验6 学号:     姓名:      专业:   6.1 实验目的 掌握二叉树的动态链表存储结构及表示. 掌握二叉树的三种遍历算法(递归和非递归两类). 运用二叉树三种遍历的方法求解有关问题. 6 ...

  9. Task(TPL)简单的实现Winform(WPF)异步

    很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...

  10. https://www.cnblogs.com/freeflying/p/9950374.html

    https://www.cnblogs.com/freeflying/p/9950374.html