Round Table Meeting
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
int main()
{
int n, q;
int x, y;
cin >> n >> q;
int arr[2 * n + 1];
for(int i = 1; i <= n; i++)
{
cin >> arr[i];
arr[i + n] = arr[i];
}
for(int k = 0; k < q; k++)
{
cin >> x >> y;
int dist = INT_MAX;
int minDist = INT_MAX;
int posx = -1;
int posy = -1;
if(x == y)
{
minDist = 0;
}
else
{
for(int i = 1; i <= 2 * n; i++)
{
if(arr[i] == x)
{
posx = i;
}
else if(arr[i] == y)
{
posy = i;
}
if(posx != -1 && posy != -1)
{
dist = abs(posy - posx);
if(dist < minDist)
minDist = dist;
}
if(minDist <= 1)
break;
}
}
if(minDist <= 1)
{
cout << "0\n";
}
else
{
cout << ceil(minDist / 2) << endl;
}
}
return 0;
}
#include <cmath>
#include <climits>
using namespace std;
int main()
{
int n, q;
int x, y;
cin >> n >> q;
int arr[2 * n + 1];
for(int i = 1; i <= n; i++)
{
cin >> arr[i];
arr[i + n] = arr[i];
}
for(int k = 0; k < q; k++)
{
cin >> x >> y;
int dist = INT_MAX;
int minDist = INT_MAX;
int posx = -1;
int posy = -1;
if(x == y)
{
minDist = 0;
}
else
{
for(int i = 1; i <= 2 * n; i++)
{
if(arr[i] == x)
{
posx = i;
}
else if(arr[i] == y)
{
posy = i;
}
if(posx != -1 && posy != -1)
{
dist = abs(posy - posx);
if(dist < minDist)
minDist = dist;
}
if(minDist <= 1)
break;
}
}
if(minDist <= 1)
{
cout << "0\n";
}
else
{
cout << ceil(minDist / 2) << endl;
}
}
return 0;
}
Comments
Post a Comment