PROBLEM LINK:Author: Praveen Dhinwa DIFFICULTY:MEDIUM PREREQUISITES:Convex Polygon, constructive algorithms PROBLEM:Given an $n$sided convex polygon where all vertices have integral coordinates. Output $\lfloor n/10\rfloor$ distinct integral points strictly inside the polygon. QUICK EXPLANATION:This editorial contains $3$ solutions: (Setter's solution is the easiest among them) Setter's solution
A dfs/bfsbased solution
Tester's solution
Setter's SolutionDFS/BFS Based SolutionTester's SolutionBonus: how to generate a convex polygon?ALTERNATIVE SOLUTION:There are many solutions for this problem. Please, feel free to share your approaches! AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here.
This question is marked "community wiki".
asked 03 Feb, 19:29

@admin how the pigeonhole principle is working here ? how it can be proved that there will be some midpoint whose x and y are integers . can u clarify it a bit more as i can't relate pgh principle to midpoint here answered 14 Feb, 13:29
I had the same question. How does 2 points being in the same hole prove that the midpoint is integral ?
(14 Feb, 14:49)
@zizx @dollarakshay x and y can have 4 scenarios O O E O O E E E now if there are four holes and there are 5 pigeons then as author mentioned that 2 of them will go to the same hole it means that the 2nd one which is going to the same hole will resemble the same property as that of 1st one i.e if O O goes the 2nd again O O will go their sum = E E hence E/2 E/2 will be an integer similarly if O E goes then O E will go to the same hole their sum = E E hence E/2 E/2 will be integer this much i understood
(14 Feb, 15:16)
Oh now I get it. Basically it says that there can only be 4 kinds of numbers:
So this means that if you have 5 points, 2 of them need to fall into one of these groups. So adding 2 numbers from the same group and dividing by 2 will always be an integer
(14 Feb, 15:18)
hmm.. right
(14 Feb, 15:22)

The entire editorial dosen't even talk about Pick's Theorm, which is required to even check whether or not it is possible to find that many integer points. answered 12 Feb, 16:02
shoelace formula too. :P
(12 Feb, 16:03)
4
Editorial doesn't talk about it because it is always possible to find n/10 points in any convex polygon of n sides with integral coordinates. It shows you one way of how to find such points.
(12 Feb, 16:04)
@admin can u give a proof of it?
(12 Feb, 17:45)
1
The setter's solution gives a proof of it.
(13 Feb, 12:45)

Another approach : answered 12 Feb, 16:12

@admin I think there could have been a better question,by giving the required no of points as input itself, rather than setting it to (n/10). The solution for (n/10) is pretty simpler . But then a case could have been asked for
to print all the points inside the polygon. My approach would handle such cases as well. My code : https://www.codechef.com/viewsolution/17322525 In this code, set the req variable to as desired, to get that many points as output. answered 13 Feb, 14:08
Thanks for sharing this :)
(13 Feb, 14:20)
If we could find if a point is inside a convex polygon in O(logN),couldnt we just run bfs from any vertex without dividing it into triangles? Please @kushal101 if u could say if its possible or not.
(15 Feb, 22:00)

Weak Test case Have a look at this solution of Poinpoly problem (it's not mine) https://www.codechef.com/viewsolution/17306405 answered 14 Feb, 09:54
Firstly, the approach is correct its only taking in mid point which will always lie inside polygon. He cannot get 2 same mid points from same starting/base vertex. He is taking one vertex, finding mid point of all vertices from that, it will give him $N/10$ points then and there. The only edge case where it fails is, where the mid point lies outside due to the loss f decimal/truncation of decimal. Why is it weak test cases?
(14 Feb, 10:41)
Its weak because even after failing on some edge cases the user is getting 70 points.
(14 Feb, 14:26)

@zizx lets see that i am taking n*(n3)/2 diagonal of the polygon.Then if (x1+x2)=even and (y1+y2)=even at the same time then you will find the perfect mid loint of that diagonal in the form of integer. If you take any two vertex and find their mid point in form of integer by negletting the decimal points or floating poing there will be some case where error will occur. answered 15 Feb, 19:05

This is another way to do it. It allows you to enumerate as many points as you want in O(k+n.log(M)) operations, where k is the number of points returned, and M is the maximum size of any integer coordinate. First split the polygon into n2 triangles (emanating from a fixed vertex). Then for each triangle with vertices (p0, p1, p2), translate to (0, p1p0, p2p0), then transform these vertices by a 2x2 matrix with integer entries and determinant 1 into the form ((0,0), (g,0), (a,b)), where b, g >0. (Could also insist 0≤a<g, but it doesn't matter.) This is OK to do, because such matrices preserve the integer grid of points. This step takes O(log(M)) operations for each of the n2 triangles. Then you can show that the only case there are no interior points is when (g=1 or b=1 or g=b=2) and (ba or b(ag)), which is an O(1) check. You can also show that if there are some interior points then there are least b/41/2 of them, which means you can check each of the b horizontal lines inside the triangle and spend no more work than O(#points returned). (You have to make a slight adjustment for counting points on the edges of the triangles, which is allowed if they are interior to the polygon.) answered 2 days ago

From the given n points Form 4 groups like even_even, even_odd, odd_even and odd_odd(where x_y are x and y cordinates respectively). answered 12 Feb, 17:39

I did this problem by finding the centroid of the polygon. Since the polygon is Convex, Centroid is surely strictly inside the polygon.Then find the mid pts of each of the N vertices and the centroid and stop when the number of points exceed floor(n/10). This gave me 70 pts....probably some careful implementation was required for low N. For small N I did horizontol scanline. (Drawing horizontol lines and checking the intersection points). answered 12 Feb, 18:06
True, low $N$ seemed to give nightmares. Passing 70 point task was easier than the 30 mark one. Had to at last swallow pride and resort to brute force for low $N$
(13 Feb, 01:35)
Yeah. I am very much interested in knowing that N=10 testcase in the first subtask which failed almost all the random techniques. Can @admin share that testcase?
(14 Feb, 14:36)
I talked to @admin regarding that. I dont think it will help much, since the polygons in that TC arent "small". Example, one of the cases where many solutions failed was
Basic idea is, huge change in x axis with very small change in y axis. Your code passed this TC though, but quite many failed at it (esp those who took mid points)
(16 Feb, 14:29)

since we only have to find n/10 points we can iterate over all given points of the polygon and look for the point in the immediate neighbour i.e(left, right, up, down) and add them to the set. the point checking part could be done with area of triangle approach where area of all 3 subtriangle == area of main triangle(3 consecutive points of the polygon). answered 12 Feb, 18:14

I followed the tester's approach by scanning a line from left to right. I found the intersection points by maintaining a list of lower and upper parts of polygon and calculating intersection points from the equation of a line. I got 70 points and couldn't pass the first test case in first subtask. Here is a link to my solution. Can anybody point out to me which test case I am missing? answered 13 Feb, 00:35
2
The basic idea of this case was including a point which is outside in the answer .I cant help much, except for this TC had points close by, steep sides and $T=100$, and $N=10$ for atleast 1 case.
(13 Feb, 01:54)

I used the bfs approach, but to check the point is inside or outside, I used a different approach, I basically triangulated the polygon and in each triangle I used the bfs approach to find the points. Checking if a point lies inside a triangle or not takes O(1). answered 13 Feb, 01:33

My approach was very different and very easy too. We just use one property of a polygon: If A and B are distinct vertices, then every point on segment AB will be inside it. There are N vertices. We can divide them into 4 disjoint groups based on the parities of X and Y coordinates. (first group: all vertices (x,y) where x is odd and y is odd etc.) It's clear that inside each group, if we take any 2 points A and B, the midpoint of segment AB will have integer coordinates, since both X and Y coordinates have same parities. There are 4 groups, so the largest group will have at least [N/4] points. We can simply choose one vertex and connect it to every other in this group, giving us [N/4]  1 distinct points, which is more than [N/10] already. answered 13 Feb, 01:52

centroid must lie inside the triangle;
answered 13 Feb, 06:25

@lokesh2002 Even I did follow a similar approach First I chose 3 points from the polygon and checked if the centroid of the triangle (Since It always lies inside the polygon) has integer coordinates but I did not check the condition "if my centroids obtained of two different triangles will coincide or not". My code seems to pass even without checking this condition . So, Is it like two centroids of two different triangles in a convex polygon never coincide (or) any specific reason ?? answered 13 Feb, 09:08
No points may coincidence but fortunately non of first n/10 points u found coincided.may be test cases are weak.
(15 Feb, 07:13)

I took a random triangle and used binary search to find the smallest triangle in which the point lies. Then i checked if the point lies inside the triangle by finding the areas of the three trianglesand the entire triangle. Also i didnt check for all the vertices. Checking the adjacent point of each vertex will suffice. Complexity O(nlogn) as we only need to check the 4 adjacent points of each vertex of the polygon. answered 13 Feb, 09:37

I just checked the 2 adjacent points of each vertex. Yikessss!! Checked whether inside by checking if that point lies on the left of 2 edges meeting at that vertex answered 13 Feb, 09:50

I used line sweeping algorithm to print the points. Link to my solution is as follow: https://www.codechef.com/viewsolution/17365491 answered 13 Feb, 15:48
can u explain me what u exactly did as i tried almost the same thing but it was WA. what i did was find the leftmost x and rightmost x loop through i = lx+1 to rx1 then for each i , find where the vertical line intersects lower_y coordinate and upper_y coordinate. then from floor(lower_y)+1 to ceil(higher_y)1 store all the values until it reaches our desired result.
(13 Feb, 16:07)
you need to count from floor(lower_y+1.0) to ceil(higher1.0)
(13 Feb, 17:48)

i have tried both approach dividing into triangles and simply using whole polygon:
in simply using whole polygon approach first i checked it contain sufficient point or not by using pick's theorem then calculated maxY and minY and then start sweeping line to adjacent point on boundary of polygon and calculating all points on line by using bresenham line points generating algorithm and checking inclusion also...by this i passed top 6 subcases passed in less time limit and got TLE for last 2.link text
as said in editorial i divided polygon into triangle and used triangle inclusion test not that Area test for inclusion given in editorial because i feel it will give answer true(for inside) when point is in boundary which will give wrong answer.and similarly find points inside triangle as in previous approach(rotating line point to next point)...but this give large TLE on face only top 2 test case passed.and after testing time taken by traingle inclusion i found out it is taking too much time than fast polygon inclusion test.link text **After contest over i saw some solutions and find that this question did a comedy with me. it is not a medium problem as tags are saying(binary search...etc.) and can be bypassed easily with some observations.link text ** answered 13 Feb, 16:05

Here, in the convex polygon, all interior angles are less than or equal to 180 degrees, OR strictly less than 180 degrees. answered 13 Feb, 17:31

I am trying to follow the setter's logic , but only few subtasks are passing. Can someone please review my submission and help me. My code is solution answered 14 Feb, 14:26
@pavitra_ag (agni) just make a small change for(long long int j=i+1;j<=i+8;j++)
(14 Feb, 15:00)
you should not start j from i as it will be a[0]+a[0]/2 in the very first scenario resulting wrong answer ...
(14 Feb, 15:02)
can you please explain to me why doing j=i+1 works? All I was doing was to pick 10 points then next 10 points and so on. If I start from j=i+1 I never pick the first point. A little help :)
(15 Feb, 22:37)

I see everyone considered it as MEDIUM level Question,but i don't think so.Though my approach is a bit similar with setter's solution,but i just use IF ELSE and FOR LOOP only,so for a beginner one can easily deals with it,rather than left it by thinking the vector approach or other algorithm's https://www.codechef.com/viewsolution/17430200 Please see my solution and respond if any query,thanking you. answered 15 Feb, 18:18
Minor point: you don't handle the case where two mid points are identical. Input
gives output
(2 days ago)

@souradeep1999 that was a good approach u r choosing everytime pure integers . with no loss of precision thanks for the explanation answered 15 Feb, 21:19

For those who used mid point trick for their answers, the corner cases were like
A simple figure illustration will be like (PS: Not an art student, cant draw a perfect decagon like that XD) The crux of these tests is, big change of x (or y) coordinate, and very minor/very small change in the y (or x) coordinate. Due to this, when calculating midpoint, a $+1$ error in y cooord results in WA (meaning, the decimal part is the deciding factor). If you truncate the decimal, you fail in the first (upper figure), if you round up the decimal, you fail in the lower one. answered 16 Feb, 15:10

Just keep it simple. After getting the idea how to check whether a given point is inside the given polygon or not,I adopted the following strategy. Since it is a convex polygon ,say for a vertex (x,y) it is guaranteed that one of (x,y1),(x,y+1),(x1,y),(x+1,y) will definitely lie inside the polygon.So just iterate through all given n points and break out when you get [n/10] points. My solution : MySolutionPOINPOLY answered 16 Feb, 18:14
1
But your basic reasoning is not correct. We can create a square, which is convex, with corners at $(\pm 1, \pm 1)$. The only internal integer point is at $(0,0)$, but doesn't have the form $(x \pm 1, y)$ or $(x, y \pm 1)$. A more general counterexample follows from multiplying the points in the sample data by some matrix like $\left( \begin{matrix} 1000 & 999999 \\ 1 & 1000 \end{matrix}\right)$. The result is a long narrow convex polygon. The internal integer points are not close to the vertices.
(17 Feb, 01:57)
if (x1,y1) and (x+1,y+1) i think all the cases should come under it
(17 Feb, 02:45)
1
Consider a convex quadrilateral with corners $(0,0)$, $(2,0)$, $(1002,2)$, and $(1000,2)$. The only internal grid point is $(501,1)$. Or a more complicated case:
which has 71 internal grid points. The first few are
but they are not close to the vertices.
(2 days ago)
yes.. right !!
(2 days ago)
Wow! How did you derive this @john_smith_3 ? Any tips on how to generate such sexy polygons? :3
(2 days ago)
2
Take the coordinates of the original sample data, and multiply by an integer 2 by 2 matrix with determinate 1. $$ \left( \begin{matrix} 1000 & 999999 \\\\ 1 & 1000 \end{matrix} \right) \left( \begin{matrix} 0 & 1 & 2 & 2 & 0 & 2 & 5 & 8 & 8 & 6 & 2 \\\\ 0 & 1 & 3 & 5 & 10 & 10 & 9 & 7 & 4 & 1 & 0 \end{matrix} \right) $$ If the original points form a convex polygon, then so do the transformed points. Internal grid points of the original polygon will transform 1to1 onto the internal grid points of the transformed polygon.
(2 days ago)
Wow!! Thats so nice to know!! Thank you @john_smith_3 !!
(2 days ago)
@john_smith_3 very clever trick !! #@
(2 days ago)
See also the comment by @alexthelemon. There he takes a possibly long thin triangle and transforms it to a nicer shape where it is easy to find the internal grid points.
(2 days ago)
@john_smith_3 :IF we consider a square,we require [4/10] = 0 points.So not an issue!
(2 days ago)
@monsij  he gave another example of convex polygon where internal points were quite far from vertices.
(2 days ago)
showing 5 of 11
show all

Hello Everyone! answered 2 days ago

With respect to first solution how do you make sure that all points are unique (that they don't overlap as incase of regular polygon if opposite vertices are chosen they would overlap at center)????
Is this regarding the setter's solution? Selecting first 10 points and finding one central point and then moving to next 10 and so on?
@pk1210  In case its a doubt in general, you can use set to avoid duplicates. If its asking about setter or tester's solution, please specify which one.
@pk1210: these n/10 points are in different "subpolygon"s, as you can see in the figure. So they have to be different.