http://lightoj.com/volume_showproblem.php?problem=1100
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.
思路:
当输入x,y代表相应的区间时,我们初始化一个b数组,记录x,y之间原数组中元素出现的次数;
然后枚举0-1000这个区间,如果存在b数组内值大于等于2,那么答案是0,跳出循环
如果为1,判断即可,特判ok为0的时候
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+66;
const int INF=0x3f3f3f3f;
int a[N], b[N];
int main()
{
int t;
cin>>t;
int num=0;
while(t--)
{
num++;
printf("Case %d:\n", num);
int n,m;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>a[i];
}
while(m--)
{
int x,y;
cin>>x>>y;
memset(b, 0, sizeof(b));
int ok=0;
int minn=INF;
for(int i=x;i<=y;i++){
b[a[i]]++;
}
for(int i=0;i<1001;i++){
if(!b[i])continue;
if(b[i]>=2){
minn=0;
break;
}
if(b[i]==1&&ok!=0)
{
minn=min(minn,i-ok);
}
ok=i;
}
cout<<minn<<endl;
}
}
}