HDU 5289

发现枚举+二分更好!

我做得复杂了,竟然两个都是从左边开始枚举。找最大最小用ST算法吧。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;

const int MAX=100050;

int f_min[MAX][20];
int f_max[MAX][20];
int num[MAX],n,dif;

void pre_init(){
	int k=(int)(log((double)n)/log(2.0));
	for(int i=0;i<n;i++) f_min[i][0]=num[i],f_max[i][0]=num[i];
	for(int j=1;j<=k;j++){
		for(int i=0;i + (1 << j) - 1<n;i++){
			f_min[i][j]=min(f_min[i][j-1],f_min[i+(1<<(j-1))][j-1]);
			f_max[i][j]=max(f_max[i][j-1],f_max[i+(1<<(j-1))][j-1]);
		}
	}
}

int find_min(int i,int j){
	int k = (int)(log(double(j-i+1)) / log(2.0));
	return min(f_min[i][k], f_min[j - (1<<k) + 1][k]);
}

int find_max(int i,int j){
	int k = (int)(log(double(j-i+1)) / log(2.0));
	return max(f_max[i][k], f_max[j - (1<<k) + 1][k]);
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&dif);
		for(int i=0;i<n;i++)
			scanf("%d",&num[i]);
		pre_init();
		LL ans=0;
		int l=0,r=0;
		int dmin=num[0],umax=num[0]; bool flag=true;
		while(l<n&&r<n){
			if(flag){
				while(r<n){
					r++;
					if(r==n){
						r--;
						break;
					}
					dmin=find_min(l,r);
					umax=find_max(l,r);
					if(umax-dmin>=dif){
						r--;
						break;
					}
				}
			//	cout<<"true:"<<l<<" "<<r<<endl;
				flag=false;
				ans+=r-l+1;
			}
			else{
				while(l<=r){
					l++;
					if(l>r){
						break;
					}
					if(dmin!=find_min(l,r)||umax!=find_max(l,r)){
						break;
					}
					else{
						ans+=r-l+1;
					}
				}
			//	cout<<"false:"<<l<<" "<<r<<endl;
				flag=true;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

  

时间: 2024-10-10 23:07:03

HDU 5289的相关文章

HDU 5289 Assignment(二分+RMQ-ST)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3813    Accepted Submission(s): 1771 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered

hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 思路:求出区间的最大最小值,只要他们的差值小于k,那么这个区间就符合要求,但是由于n较大,用暴力一定超时,所以就要用别的方法了:而RMQ是可以求区间的最值的,而且预处理的复杂度只有O(nlogn),而查询只是O(1)处理,这样相对来说节约了时间,再根据右端点来二分枚举左端点(其实不用二分好像更快,估

hdu 5289 Assignment 二分+rmq

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 169 Problem Description Tom owns a company and he is

HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 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 tas

HDU 5289 Assignment(RMQ 单调(双端)队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 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 tas

hdu 5289 Assignment 【ST算法】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:求满足最大值减最小值小于k的区间的数目. 枚举左端点,二分右端点,用st算法求区间最值 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #incl

HDU 5289 尺取

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 f

hdu 5289(二分+RMQ) Assignment

题意: 给一个序列,然后求出连续的序列中最大和最小值之差小于k的个数. 思路 二分+ST 枚举每一个位置.二分下标,找一个最大的区间满足区间内最大最小值相差小于k,当前这个位置对于答案的贡献就是这个区间长度. 求一个静态数组的区间最大最小值,用ST算法就好了. 参考code: /* #pragma warning (disable: 4786) #pragma comment (linker, "/STACK:0x800000") */ #include <cassert>

HDU 5289 Assignment(单调队列)

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 fro