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 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
题意:n个数,m个查询,查询下标x,y之间数的最小插值。
分析:
这个题关键是a[i]的值为【1,2000】,所以,我们可以开一个num[a[i]]保存a[i]的次数,我们查询a[x]~a[y]之间的最小差值,枚举num[min(a[x]~a[y],max(a[x]~a[y])】的之间,如果一个数出现》=二次,直接输出0,没有的话,记录差值(已经拍好序)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define N 100010
#define ll long long
using namespace std;
int a[N],num[1005];
int main()
{
int t;
int n,m;
scanf("%d",&t);
int kase=0;
while(t--)
{
kase++;
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Case %d:\n",kase);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
int minn=(100000),maxx=-100000;
for(int j=x;j<=y;j++)
{
maxx=max(maxx,a[j]);
minn=min(minn,a[j]);
num[a[j]]++;
}
int s=-1;
int ans=10000;
// cout<<minn<<endl;
for(int i=minn;i<=maxx;i++)
{
//cout<<i<<" "<<num[i]<<endl;
if(num[i]>=2)
{
ans=0;
break;
}
if(num[i]==1&&s==-1)
{
s=i;
continue;
}
if(s!=-1&&num[i]==1)
{
//cout<<i<<" "<<num[i]<<" "<<ans<<endl;
ans=min(ans,abs(i-s));
s=i;
}
}
memset(num,0,sizeof(num));
printf("%d\n",ans);
}
}
return 0;
}