(一)题面:
Two organizations International Community for Preservation of Constructions (ICPC) and Japanese Archaeologist Group (JAG) engage in ruins preservation. Recently, many ruins were found in a certain zone. The two organizations decided to share the preservation of the ruins by assigning some of the ruins to ICPC and the other ruins to JAG.
Now, ICPC and JAG make a rule for assignment as follows:
- Draw a vertical straight line from the north to the south, avoiding to intersect ruins.
- Ruins located to the west of the line are preserved by ICPC. On the other hand, ruins located to the east of the line are preserved by JAG. (It is possible that no ruins are located to the east/west of the line; in this case, ICPC/JAG will preserve no ruins.)
A problem is where to draw a straight line. For each organization, the way to preserve its assigned ruins is to make exactly one fence such that all the assigned ruins are in the region surrounded by the fence. Furthermore, they should minimize the length of such a fence for their budget. If the surrounded areas are vast, expensive costs will be needed to maintain the inside of areas. Therefore, they want to minimize the total preservation cost, i.e. the sum of the areas surrounded by two fences. Your task is to write a program computing the minimum sum of the areas surrounded by two fences, yielded by drawing an appropriate straight line.
Input
The input consists of a single test case.
N
x1 y1
x2 y2
...
xN yN
The first line contains an integer N (1≤N≤100,000), which is the number of founded ruins. The following N lines represent the location of the ruins. The i-th line of them consists of two integers xi and yi, which indicate the location of the ii-th ruin is xi east and yi north from a certain location in the zone. You can assume the following things for the ruins:
- −109≤xi,yi≤109
- You can ignore the sizes of ruins. That is, you can assume ruins are points.
- No pair of ruins has the same location.
Output
Print the minimum total preservation cost yielded by drawing an appropriate straight line. You should round off the cost to the nearest integer.
Sample Input 1
8 -10 0 -10 5 -5 5 -5 0 10 0 10 -5 5 -5 5 0
Output for the Sample Input 1
50
Sample Input 2
5 0 0 0 1 0 2 1 0 1 1
Output for the Sample Input 2
0
Sample Input 3
6 1 5 1 6 0 5 0 -5 -1 -5 -1 -6
Output for the Sample Input 3
6
Sample Input 4
10 2 5 4 6 9 5 8 8 1 3 6 4 5 9 7 3 7 7 3 9
Output for the Sample Input 4
17
(二)题意:
在二维平面(xOy)上给定N个点,让你找到一条垂直于x轴的直线将所有的点分为两个部分,使得这两个部分的点形成的凸包(凸包及其算法简介)的面积和最小,输出这个最小值(四舍五入,划分直线上不能有点)。
(三)题解:
只有当划分线越过不同的点时,最终的结果才可能发生变化。
考虑暴力的解法,枚举每一个点得到划分线的位置,然后求两个凸包,求和得到结果。考虑复杂度:显然O(nlogn)排序,枚举位置O(n),对于每一个枚举的位置求凸包O(n),故复杂度为O(n^2),会TLE。
在上述的思路中,由于每次移动枚举划分线位置时,我们都去O(n)计算了一次凸包,故比较耗时,而实际上由于我们每次枚举位置时只会改变一个点的划分(直线通过多个点的情况稍后考虑),那么实际上也就是在左边的凸包的基础上加入一个点以及在右边的凸包中减少一个点,然后求加入点以后的凸包的面积。
如果在移动划分线位置时直接枚举左右两边的凸包的改变情况,似乎不是那么好处理(既要向左边加入点又要向右边删除点),而如果只维护左边凸包加点的情况就好得多,接下来就是一个经典的“动态凸包”问题了。
为什么加引号呢?因为这里并没有那么动态,因为点是从左往右枚举,所以加入的点一定在现有凸包的右边(对于其它的情况这里不做展开了,不会2333),下面分析一下这种情况,这里基于水平序的解法进行说明,由于用水平序来写分为上凸壳和下凸壳,所以我们分别分析加入一个点对两个凸壳的影响,而对于两个凸壳的处理实际上几乎是一摸一样的,故这里只说明 下凸壳的变化情况,如图:
我们在原来的基础上加入点D,如果发现D需要加入凸包,而存在这样C需要从凸包中删去,那么加入D以后得到的新的下凸壳的面积为S'=S-S▲ABC+S▲ABD,这样就做到了O(1)转移(实际上单次不一定O(1),但是每个点最多进凸包一次出凸包一次,均摊为O(1))。上凸壳也是一样的处理。
而总的凸包的面积为两个凸壳的面积和,故我们可以通过O(nlogn)进行排序,然后O(n)地处理出从左至右枚举的位置时左边的凸包的面积,同理我们从右往左维护凸包,便可以可以O(n)处理出划分线在各个位置时,右边的凸包的面积。求出这两个以后,我们再枚举划分位置,就可以得到左边和右边的凸包的面积和了。
有一个小问题是由于直线不能通过点,实际上相当于直线上的点需要全部划分到同一边,在最后枚举位置的时候处理一下即可。具体细节参见代码。
(四)代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=1e5+10;
struct Point{
ll x,y;
Point(){}
Point(ll _x,ll _y):x(_x),y(_y){}
bool operator < (const Point &P)const{
return x<P.x||(x==P.x&&y<P.y);
}
Point operator - (const Point &P)const{
return Point(x-P.x,y-P.y);
}
ll Cross(const Point & P)const{
return x*P.y-y*P.x;
}
}p[maxn],down[maxn],up[maxn];
ll Area(Point P1,Point P2){return P1.Cross(P2);}
ll s1[maxn],s2[maxn];
void solve(ll s[maxn],int n,bool rev){
int top1=0,top2=0;
for(int i=0;i<n;i++){
if(i)s[i]=s[i-1];
while(top1>1&&(p[i]-down[top1-2]).Cross(down[top1-1]-down[top1-2])>=0){ //rev==0:下凸包
s[i]-=Area(down[top1-2]-down[0],down[top1-1]-down[0]);top1--;
}down[top1++]=p[i];
if(top1>2)s[i]+=Area(down[top1-2]-down[0],down[top1-1]-down[0]);
while(top2>1&&(up[top2-1]-up[top2-2]).Cross(p[i]-up[top2-2])>=0){ //rev==0:上凸包
s[i]-=Area(up[top2-1]-up[0],up[top2-2]-up[0]);top2--;
}up[top2++]=p[i];
if(top2>2)s[i]+=Area(up[top2-1]-up[0],up[top2-2]-up[0]);
}
if(rev)reverse(s,s+n);
}
int main(){
// freopen("in.txt","r",stdin);
int n;scanf("%d",&n);
for(int i=0;i<n;i++){scanf("%I64d%I64d",&p[i].x,&p[i].y);}
if(n<=3){printf("0\n");return 0;}sort(p,p+n);
solve(s1,n,0);reverse(p,p+n);//正着扫描
solve(s2,n,1);reverse(p,p+n);//反着扫描
ll ans=s1[n-1];
//将直线上的点全部分至左边
for(int i=0;i<n-1;i++)if(p[i].x!=p[i+1].x)ans=min(ans,s1[i]+s2[i+1]);
//将直线上的点全部分至右边
for(int i=n-1;i>0;i--)if(p[i].x!=p[i-1].x)ans=min(ans,s1[i-1]+s2[i]);
printf("%I64d\n",ans/2+ans%2);
return 0;
}
(五)总结:
新知识get√--动态凸包
第一次听到这个词,算是一个超级基础的入门题了
貌似其它复杂的情况还要写棵平衡树维护信息?有点怕怕~