您的位置:首页 > 其它

Light oj--1100

2015-12-07 22:05 274 查看
Description

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

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

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[1010];
int a[101000];
int main(){
int t,cas;
int n,q,u,v;
cas=1;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Case %d:\n",cas++);
for(int i=1;i<=q;i++){
scanf("%d%d",&u,&v);
if(v-u>=1000){
printf("0\n");
continue;
}
int ans=0;
int d=1005;//设一个初始差值
int flag=0;//判断是否为第一个数
memset(num,0,sizeof(num));
for(int j=u;j<=v;j++){
num[a[j]]++;//在区间[u,v]中每一个数出现的次数
}
for(int k=1;k<=1000&&ans<=v-u;k++){
if(num[k]>1){
d=0;
break;
}
else if(num[k]==1){
ans++;
if(flag==0){
flag=k;//因为u!=v
}
else{
d=min(d,k-flag);
flag=k;//不断变换两个数
}

}

}
printf("%d\n",d);
}

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: