Time Limit: 3 second(s) | Memory Limit: 32 MB |
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 n 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 | 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.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
int vis[1010];
int num[100010];
int MIN(int a,int b){
return a<b?a:b;
}
int main()
{
int t,i,j,k=1,n,q;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
for(i=0;i<n;++i){
scanf("%d",&num[i]);
}
int a,b,min;
printf("Case %d:\n",k++);
while(q--){
scanf("%d%d",&a,&b);
if((b-a+1)>1000){
printf("%d\n",0);
}
else {
min=inf;
memset(vis,0,sizeof(vis));
for(i=a;i<=b;++i){
if(vis[num[i]]){
min=0;
break;
}
else {
vis[num[i]]=1;
for(j=a;j<i;++j){
min=MIN(min,abs(num[i]-num[j]));
}
}
}
printf("%d\n",min);
}
}
}
return 0;
}