Page 2 of 2

Re: 10714 - Ants

Posted: Tue Jul 01, 2008 6:45 am
by Obaida
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. :wink:

Help plz!

Posted: Tue Nov 11, 2008 10:54 am
by usuario
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 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 = 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)
{
if (min1 > min2)
min = min2;
else
if (min2 > min1)
min = min1;
else
min = min1;
}

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

return 0;
}

Re: 10714 - Ants

Posted: Tue Nov 11, 2008 11:27 am
by usuario
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 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;
}

Re: 10714 - Ants

Posted: Wed Nov 12, 2008 9:09 am
by Obaida
Why your are using such a long code!!! It's a simple code. sort the data then go for the sort or long time. :wink:

Re: 10714 - Ants

Posted: Sat Aug 15, 2009 8:50 pm
by stcheung
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.

10714:Wrong Answer

Posted: Fri Nov 26, 2010 6:11 pm
by BUET
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;
}