Mooncrater's blog

By Mooncrater, history, 5 years ago, In English

Hello,

I was trying to solve 339 A — Peter and Snow Blower, which basically involves a polygon, bound to some point, by a rope. The polygon moves around the point, in a circle. We have to find the area cleared by the polygon.

(It's working correctly here too)

Approach: Find the point that is at the maximum distance from the tethering point ($$$P$$$). This is amongst the vertices of the polygon. To find the point, that is closest to $$$P$$$, we consider each side, and find the point on this segment, that is closest to $$$P$$$. A line perpendicular to the segment, that goes through $$$P$$$ is found. The intersection of the segment and the perpendicular is the point closest to $$$P$$$. This point might not be on the segment, so we have to check that too.

Enough explanation. Here is my code. It fails on test 54, which when I run locally, gives out the correct answer.

Good problem.

So, let's think, why this should happen. The -g flag does change the memory. So, if I am accessing some memory that I should not have, then in the local case, that memory is fixed, but not in the case of the online compiler. So, what memory do I access in my code?

const int MAXN = 1e5+5 ;
pt pts[MAXN] ;

And where do I use it?

f(i,0,n)
{
    T a,b ;
    cin>>a>>b ;
    pts[i] = {a,b} ;
    maxd = max(maxd,dist(P,pts[i])) ;
    mind = min(mind,dist(P,pts[i])) ;
}

n is an input, $$$3 \leq n \leq 1e5$$$, so I don't think we have any problem here. Let's see:

f(i,0,n)
{ 
    int j = (i+1)%n ;
    line l ;
    l.v = getv(pts[i],pts[j]) ;
    l.c = cross(l.v,pts[i]) ;
    pt poii = poi(l,P) ;
    if(inseg(pts[i],pts[j],poii))
    {
        mind = min(mind,dist(poii,P)) ;
    }
}

i goes from 0 to n-1. That doesn't cross any barrier. j=(i+1)%n, so j's range is (0,n-1). That too shouldn't do anything bad.

Is there any other place, where I could do a bad memory access?

I think, but no.

Imported functions work different for different compilers?

Whenever I think it's not my fault, I'm 100% wrong. So no.

Where are you little bug. Where do you hide?

I am trying to find this little bug, please share more strategies to find this little pos.

  • Vote: I like it
  • -12
  • Vote: I do not like it

»
5 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

The problem was you were initializing a long double variable "mind" with value LONG_MAX, which is only 2^31 -1. I changed it to 1e18 (10^18) and it passed all tests. As you can see, all I changed was the "mind" initialization (and removed comments to keep it tidy). Also changed your array to vector while testing for possible out-of-bounds access. 72449284

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    God?

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Aww shmucks. I was assuming LONG_MAX to be $$$2^{63}-1$$$. Thank you very much!

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Weirdly enough, the value of LONG_MAX in onlinegdb's compiler (and ideone too) comes out to be $$$2^{63}-1$$$.

    I saw the docs, and they say "$$$2^{31}-1$$$ or greater". So, should have used LLONG_MAX instead.

    Learned a thing. Thanks.