LightOJ 1100 Again Array Queries【技巧暴力】

参看资料:

https://blog.csdn.net/helloiamclh/article/details/49431025?utm_source=blogxgwz1


题目:

Given an array with n integers, and you are given two indices i and j (i ≠ j) in the array. You have to find two integers in the range whose difference is minimum. You have to print this value. The array is indexed from 0 to n-1.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 10000). The next line contains space separated integers which form the array. These integers range in [1, 1000].

Each of the next q lines contains two integers i and j (0 ≤ i < j < n).

Output

For each test case, print the case number in a line. Then for each query, print the desired result.

Sample Input

2

5 3

10 2 3 12 7

0 2

0 4

2 4

2 1

1 2

0 1

Sample Output

Case 1:

1

1

4

Case 2:

1

题目大意:

        n个数据,q个询问。每个询问给定 l ,r ,求区间 [ l , r ] 之内 任意两个数 的 差的绝对值 的 最小值。

解题思路:

        暴力暴力暴力,用set装区间内的元素,以为不考虑重复元素的差值,错的离谱,。

        用到桶的思想,桶内有两个元素,则最小差值为 0 ;否则,不断找寻最小距离的两个数。

实现代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAXN 100010
#define MAX(a,b) a>b?a:b
#define MIN(a,b)a>b?b:a
#define INF 0xfffffff
using namespace std;
int a[MAXN],v[MAXN];
int fab(int a){
	return a>0?a:-a;
}

int main(){
	int t,i,n,k,j,l,r;
	int cas=0;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		printf("Case %d:\n",++cas);
		while(k--){
			int ans=INF;
			scanf("%d%d",&l,&r);
			if(r-l+1>1000){
				printf("0\n");
				continue;
			}
			memset(v,0,sizeof(v));
			for(i=l;i<r;i++){
				if(v[a[i]]){
					ans=0;
					break;
				}
				v[a[i]]=1;
				for(j=i+1;j<=r;j++)
					ans=MIN(ans,fab(a[i]-a[j]));
			}
			printf("%d\n",ans);
		}
    }
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值