Problem F: The Nearest Same Chocolate
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 271 Solved: 151
[ Submit][ Status][ Web Board]
Description
Now it is well-known that after the 40th ACM/ICPC, Diao Yang has found many girlfriends, and Diao Yang will buy much candies for them every day. But today, Diao Yang has no much money to buy any candies for his girlfriends. Because Diao Yang is always very nice to his girlfriends, these pretty girls decided to buy some chocolates for him today as return.
Then Girls went to a supermarket and chose many different chocolates. In order to distinguish these chocolates, they marked them by numbers. Any two chocolates chosen by the same girl were marked by the same number, and any two chocolates chosen by different girls were marked by different numbers. Then girls went back home, put their chocolates in a line on the table and gave Diao Yang to eat. But before Diao Yang ate, the girls gave him a problem: can you find the distance of the nearest two chocolates which were marked by the same girl? As Diao Yang is too hungry to solve this problem, he asks you for help.
Input
The first line contains an integer T indicating the total number of test cases.
In each test case, there are two lines. The first line is an integer n(), indicating there are chocolates. The second line contains n integers, indicating the marked number for each chocolate. All the integers will be no more than 100.
Output
For each test case, output the answer in one line. You can assume that there are at least two chocolates which were marked by the same girl.
Sample Input
282 3 8 1 5 3 4 562 3 2 1 3 1
Sample Output
32
HINT
#include<stdio.h>
#include<string.h>
int inf=9999999;
int main()
{
int pos[205];
int dis[205];
int a[205];
int T;
scanf("%d",&T);
while(T--)
{
int min=inf;
int n;
scanf("%d",&n);
for(int i=1;i<=205;i++)
{
pos[i]=-1;
dis[i]=inf;
}
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(pos[a[i]]==-1) pos[a[i]]=i;
else
{
int t;
t=i-pos[a[i]];
pos[a[i]]=i;
if(t<dis[a[i]])
dis[a[i]]=t;
}
}
for(int i=1;i<=n;i++)
if(min>dis[i])
min=dis[i];
printf("%d\n",min);
}
return 0;
}