10947  Bear with me, again..
Moderator: Board moderators

 Guru
 Posts: 647
 Joined: Wed Jun 26, 2002 10:12 pm
 Location: Hong Kong and New York City
 Contact:
You can solve the problem without using doubles, as I imagine that might be a problem.
Check out http://www.algorithmist.com !
You only need to compare the numbers. Compare their squares: for any real x,y >= 0, it holds that x <= y iff x^2 <= y^2.adnan2nd wrote:but how?
the distance can be noninteger.
Last edited by mf on Mon Oct 24, 2005 11:11 pm, edited 1 time in total.
Then how can I convert the following
into correct comarison. distr1r2 is to be compared with limit. r1,r2 are integers.
Code: Select all
dist=sqrt(pow(x1x2,2)+pow(y1y2,2));
if(fabs(distr1r2limit)<.000000001) g[i][j]=g[j][i]=1;
else if(floor(distr1r2)<limit) g[i][j]=g[j][i]=1;
sobhani
I think most problems from the last contest were badly specified (if not downright wrong, as was the case for problem A). Does anyone know how big can the coordinates be? Will a sum of squares overflow a long long? I have also tried using doubles, but nothing seems to work.
My algorithm simply uses the unionfind data structure to merge connected components. Two islands are connected when the distance between their centers is no larger than the maximum distance plus the sum of their radii.
Any ideas as to why it gives WA?
Thanks in advance.
My algorithm simply uses the unionfind data structure to merge connected components. Two islands are connected when the distance between their centers is no larger than the maximum distance plus the sum of their radii.
Any ideas as to why it gives WA?
Thanks in advance.
Code: Select all
deleted, it was correct... there are inputs with a negative number of islands, which makes absolutely no sense
Last edited by david on Sat Oct 22, 2005 5:06 pm, edited 1 time in total.
So you want to check if the inequality holds:adnan2nd wrote:Then how can I convert the followinginto correct comarison. distr1r2 is to be compared with limit. r1,r2 are integers.Code: Select all
dist=sqrt(pow(x1x2,2)+pow(y1y2,2)); if(fabs(distr1r2limit)<.000000001) g[i][j]=g[j][i]=1; else if(floor(distr1r2)<limit) g[i][j]=g[j][i]=1;
dist  r1  r2 <= limit. (*)
Start by squaring both sides.
(dist  (r1+r2))^2 <= limit^2
dist^2  2 dist (r1+r2) + (r1+r2)^2 <= limit^2
dist^2 + (r1+r2)^2  limit^2 <= sqrt(4 dist^2 (r1+r2)^2)
If the LHS of the above equation is negative, then (*) holds.
Otherwise, once again square both sides.
(dist^2 + (r1+r2)^2  limit^2)^2 <= 4 dist^2 (r1+r2)^2
The last inequality can be checked with just integer computations, since dist^2, r1, r2 and limit are all integers.
long longSanny wrote:Anybody who got AC can please tell me what data type he used?

 Guru
 Posts: 647
 Joined: Wed Jun 26, 2002 10:12 pm
 Location: Hong Kong and New York City
 Contact:
I got AC using only ints for this problem, so overflow should not matter.
The mistake for problem A was mine. I changed the problem description and the test data very last minute for the local contest, but sent Miguel the outdated input.
The mistake for problem A was mine. I changed the problem description and the test data very last minute for the local contest, but sent Miguel the outdated input.
Check out http://www.algorithmist.com !
 little joey
 Guru
 Posts: 1080
 Joined: Thu Dec 19, 2002 7:37 pm
mf: I can't follow your reasoning for squaring again [EDIT: now I can]
Isn't this much simpler:
Since the islands are non overlapping, dist>=r1+r2, so we can write
dist <= limit + r1 + r2 for your condition (*)
and check
(dist)^2 <= (limit + r1 +r2)^2
which is an allinteger expression.
Still got WA, though
Got AC during the contest, but WA after rejudge. Still can't find my error.
Can anyone see it?
Integer overflow is not an issue, as I verified with other code, and it's also not TLE, although it's unnecessary slow. Please help!
Isn't this much simpler:
Since the islands are non overlapping, dist>=r1+r2, so we can write
dist <= limit + r1 + r2 for your condition (*)
and check
(dist)^2 <= (limit + r1 +r2)^2
which is an allinteger expression.
Still got WA, though
Got AC during the contest, but WA after rejudge. Still can't find my error.
Can anyone see it?
Code: Select all
DELETED AFTER AC
Last edited by little joey on Sat Oct 22, 2005 3:54 pm, edited 2 times in total.
 little joey
 Guru
 Posts: 1080
 Joined: Thu Dec 19, 2002 7:37 pm
See the previous message, that was the code that got AC during the contest.mf wrote:Ah yes, your solution is correct and simpler than mine. (During the contest I just ignored that line where they say that islands don't overlap.)
Just post your code, I'll try to check what's wrong.Still got WA, though
This is new code that runs much faster, but also is WA
Code: Select all
DELETED AFTER AC
Last edited by little joey on Sat Oct 22, 2005 3:55 pm, edited 1 time in total.
Ok, I think I know what's going on. There are test cases where N<0. (???!)
(makes one wonder, how can something "be followed by [a negative number of] lines"...)
little joey, I got your first code accepted by inserting this line after scanf("%d", &islands):
(makes one wonder, how can something "be followed by [a negative number of] lines"...)
little joey, I got your first code accepted by inserting this line after scanf("%d", &islands):
Code: Select all
if (islands < 0) { printf("Larry and Ryan will be eaten to death.\n"); continue; }
 little joey
 Guru
 Posts: 1080
 Joined: Thu Dec 19, 2002 7:37 pm
Alow me to swear in my mother tongue: G0DV3RD0MM3 !*&#*&$
You're right mf, thank you very much.
This is ridiculous. This means that L&R get eaten if the number of Islands is negative, even if the start and target island are close enough to be reached ?!?!
This also explains why some people now get TLE (while(islands){ ...}).
This problem definitly needs rerejudging (and I want my perfect score 6 AC for 6 submissions for the contest back...).
You're right mf, thank you very much.
This is ridiculous. This means that L&R get eaten if the number of Islands is negative, even if the start and target island are close enough to be reached ?!?!
This also explains why some people now get TLE (while(islands){ ...}).
This problem definitly needs rerejudging (and I want my perfect score 6 AC for 6 submissions for the contest back...).