10714 - Ants

All about problems in Volume 107. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10714 - Ants

Post by Obaida » Tue Jul 01, 2008 6:45 am

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:
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!

Post by usuario » Tue Nov 11, 2008 10:54 am

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;
}

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

Re: 10714 - Ants

Post by usuario » Tue Nov 11, 2008 11:27 am

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;
}

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10714 - Ants

Post by Obaida » Wed Nov 12, 2008 9:09 am

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:
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

Post by stcheung » Sat Aug 15, 2009 8:50 pm

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

10714:Wrong Answer

Post by BUET » Fri Nov 26, 2010 6:11 pm

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;
}

Post Reply

Return to “Volume 107 (10700-10799)”