## 10714 - Ants

Moderator: Board moderators

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 10714 - Ants

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.

usuario
New poster
Posts: 3
Joined: Tue Nov 11, 2008 10:44 am

### Help plz!

I got WA in the UVA, this is my code help me plz

#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 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)
else

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 = pos;
/*-----------------------------------------------------------------------*/
/*--------------------- Minium-------------------------------------------*/

if (!flag)
{
if (lon % 2 == 0)
{
{
min = lon - pos;
flag = true;
}
{
flag = true;
}
}
else
{
flag = true;
}
}

if (!flag)
{
//im in the right side of the pole
//i want to know if im near the middle of the pole
if (dist1 > pos - mitad)
{
min1 = pos;
}

//im in the left side of the pole
if (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;
}

printf("%d %d\n", min,max);
}

return 0;
}

usuario
New poster
Posts: 3
Joined: Tue Nov 11, 2008 10:44 am

### Re: 10714 - Ants

I do a little correction, but i still go an WA in the UVa plz help: my code

Code: Select all

``````#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 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)
else

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)
{
{
min = lon - pos;
flag = true;
}
{
flag = true;
}
}
else
{
flag = true;
}
}

if (!flag)
{
//im in the right side of the pole
//i want to know if im near the middle of the pole
if (dist1 > pos - mitad)
{
min1 = pos;
}

//im in the left side of the pole
if (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;
}
``````

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 10714 - Ants

Why your are using such a long code!!! It's a simple code. sort the data then go for the sort or long time.
try_try_try_try_&&&_try@try.com
This may be the address of success.

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

### Re: 10714 - Ants

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.

BUET
New poster
Posts: 22
Joined: Sun Jun 13, 2010 8:38 am

Why I am getting WA to this code?

Code: Select all

``````#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

vector<int> v1,v2,v3;
vector<int>::iterator pos;

int main(void)
{

int t;
int ant,len,i,j,k;

int min,max,a,b;

cin >> t;

while(t--)
{
cin >> len >> ant;

for( i = 0; i < ant; i++)
{
cin >> j;
v1.push_back(j);
}

k = floor(len/2);

a = abs(len-v1[0]);
b = abs(0-v1[0]);

if( a < b)
v2.push_back(a);
else
v2.push_back(b);

for( i = 1; i < ant; i++)
{

a = abs(len-v1[i]);
b = abs(0-v1[i]);
if( a < b)
v2.push_back(a);
else
v2.push_back(b);
}

a = abs(len-v1[0]);
b = abs(0-v1[0]);

if( a < b)
v3.push_back(b);
else
v3.push_back(a);

for( i = 1; i < ant; i++)
{

a = abs(len-v1[i]);
b = abs(0-v1[i]);
if( a < b)
v3.push_back(b);
else
v3.push_back(a);
}

pos = max_element (v2.begin(), v2.end());

cout << *pos << " ";

pos = max_element (v3.begin(), v3.end());

cout << *pos << "\n";

v2.clear();
v3.clear();

}

return 0;
}``````