Lightoj 1100 - Again Array Queries (枚举剪枝)

1100 - Again Array Queries
PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

Given an array with n integers, and you are given twoindices i and j (i ≠ j) in the array. You have to find twointegers in the range whose difference is minimum. You have to print thisvalue. 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 n spaceseparated integers which form the array. These integers range in [1, 1000].

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

Output

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

Sample Input

Output for Sample Input

2

5 3

10 2 3 12 7

0 2

0 4

2 4

2 1

1 2

0 1

Case 1:

1

1

4

Case 2:

1

Notes

Dataset is huge, use faster I/O methods.



事后看这个题,简直被蠢哭了,当时还想线段树,真是脑子坏掉了


大意:求一个区间内任意两个数值最小差


思路:标记数字,如果在这个区间内存在两个相同的数字,那么最小值一定为0,直接退出。。。,否则枚举检查


ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#define MOD 1000000007
#define LL long long
#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];
int 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;
}



你可能感兴趣的:(Lightoj 1100 - Again Array Queries (枚举剪枝))