Hi Grazyna from what side you wanted to calculate the largest time???
But if you want to calculate from the right side then what will be the answer? I think it will be 9.
That means if they walk left to right then the time will be 7. But if opposite then the maximum time is 9.
In this problem we must take the largest time and the shortest time.
try_try_try_try_&&&_try@try.com
This may be the address of success.
if (!flag)
{
if (lon % 2 == 0)
{
if (pos == mitad +1)
{
min = lon - pos;
flag = true;
}
if (pos == mitad)
{
min = mitad;
flag = true;
}
}
else
if (pos == mitad)
{
min = mitad -1;
flag = true;
}
}
if (!flag)
{
//im in the right side of the pole
if (pos > mitad)
//i want to know if im near the middle of the pole
if (dist1 > pos - mitad)
{
dist1 = pos - mitad;
min1 = pos;
}
//im in the left side of the pole
if (pos < mitad)
if (dist2 > mitad - pos)
{
dist2 = mitad - pos;
min2 = pos;
}
}
}
qsort((void*)vec,n,sizeof(vec[0]),ordena);
/*-------------------------------------------------------------------------*/
/*-----------------------------Maxium--------------------------------------*/
int menor = vec[0];
int mayor = vec[n-1];
if((lon-menor)>mayor)
max= lon-menor;
else
max= mayor;
if (!flag)
{
if (min1 > min2)
min = min2;
else
if (min2 > min1)
min = min1;
else
min = min1;
}
#include<iostream.h>
#include<stdlib.h>
using namespace std;
int ordena( const void *a, const void *b)
{
return( strcmp((char *)a,(char *)b) );
}
int main()
{
freopen("in1.txt","r",stdin);
freopen("out.txt","w",stdout);
int casos;
int mitad;
int min1,min2,min,max;
int mayor, menor;
int dist1,dist2;
int lon,n;
int pos;
bool flag;
int *vec;
//number of cases
scanf("%d",&casos);
while(casos--)
{
int min, max;
scanf(" %d %d",&lon,&n);
vec = new int [n];
if (lon %2 == 0)
mitad = lon/2;
else
mitad = lon/2 +1;
max=min=min1=min2=0;
flag = false;
dist1 = dist2 = lon;
//position of the ant
for(int i=0;i<n;i++)
{
scanf(" %d",&pos);
vec[i] = pos;
/*-----------------------------------------------------------------------*/
/*--------------------- Minium-------------------------------------------*/
if (!flag)
{
if (lon % 2 == 0)
{
if (pos == mitad +1)
{
min = lon - pos;
flag = true;
}
if (pos == mitad)
{
min = mitad;
flag = true;
}
}
else
if (pos == mitad)
{
min = mitad -1;
flag = true;
}
}
if (!flag)
{
//im in the right side of the pole
if (pos > mitad)
//i want to know if im near the middle of the pole
if (dist1 > pos - mitad)
{
dist1 = pos - mitad;
min1 = pos;
}
//im in the left side of the pole
if (pos < mitad)
if (dist2 > mitad - pos)
{
dist2 = mitad - pos;
min2 = pos;
}
}
}
qsort((void*)vec,n,sizeof(vec[0]),ordena);
/*-------------------------------------------------------------------------*/
/*-----------------------------Maxium--------------------------------------*/
int menor = vec[0];
int mayor = vec[n-1];
if((lon-menor)>mayor)
max= lon-menor;
else
max= mayor;
if (!flag)
{
//compare wich of the distances is near the middle of the pole
if (dist1 > dist2)
min = min2;
else
if (dist1 <= dist2)
min = min1;
}
printf("%d %d\n", min,max);
}
return 0;
}
Here's some more hints for those who need it. Both the min & max time scenarios won't involve 2 ants touching each other, so you can safely ignore that piece of fact in the problem description. Furthermore, while sorting would help, it also isn't necessary to solve the problem.