Orchard Division
orchard.c, orchard.cpp, orchard.c11, Orchard.java, orchard.py
Uncle Oliver isgoing to sell a significant part of his famous dwarf plum tree orchard. He isgoing to divide the orchard into two parts, sell the first one and keep theother one.
The trees wereoriginally planted in regular rows and columns forming a rectangular grid withthe same number of rows and columns. As years went by, Oliver removed manytrees which were weak or plagued by bugs so nowadays there is also a lot offree squares unoccupied by any tree.
Oliver hasdecided that he will keep exactly half of all the trees in the orchard.Moreover, he has few additional demands which, in his opinion, will ensure easymaintenance of his part in the future.
• The part Oliver is going to keep should be in theshape of a rectangle.
• A least one corner of the rectangle shouldcoincide with a corner of the orchard.
• The rectangle area should be as small aspossible.
Originally, eachtree was planted in the center of an imaginary square whose area was exactlyone square meter. Thus, the position of each tree can be described by thecoordinates of the square on which it is standing. The dividing fence betweenthe two parts of the orchard will run along the borders of the squares.
Input Specification
There are moretest cases. Each case starts with a line containing two integersM (1 ≤ M ≤ 109) and N (1≤ N ≤ 106) separated byspace. The orchard side length in meters is expressed byM and the number of trees in the orchard is expressed by N. Next, there areN lines, each line specifies xand y coordinates of one tree inthe orchard. The coordinates are separated by space. For simplicity reasons, weassume that the coordinates are zero based, so the coordinates of the squaresin the corners of the orchard are (0,0),(0,M−1),(M −1,M −1),(M−1,0). All coordinate pairs (x,y) in one test case are unique.
Output Specification
For each testcase, print a single line with one whole number A denoting the minimum possible area in square meters of uncleOliver’s part of the orchard. If it is not possible to divide the orchardaccording to Oliver’s demands print “-1”. Note that the output value might not fit into 32-bit integer type.
Sample Input
6 8
4 5
1 4
0 3
5 3
1 2
3 2
3 1
2 0
3 3
2 0
1 1
0 2
2 2
0 0
1 1
Output for Sample Input
12
-1
1
【思路】
首先数据范围很大,点数却没那么大,也就需要先对坐标进行离散化。然后对所有的点进行双关键字排序,接下来进行扫描线操作。第一遍扫描线从高到低扫,维护横坐标出现的的次数,并查询前缀和为总点数一半的那个点的位置,以及后缀和为总点数一半的那个点的位置,可以据此算出面积;第二遍从低到高扫,也是一样分前缀和后缀来算。面积最小的那个正是答案。维护和查询横坐标出现次数用树状数组,由于sum函数具有递增性,查找某个点的位置时可以二分查找。这里两个优化中的一个不做都会TLE,另外二分时应该注意,找前缀的那次要尽可能找左边的点,找后缀的那次要尽可能找右边的点。
【代码】
#include
#include
#include
#include