Codeforces Round #358 (Div. 2) E 計算幾何 旋轉卡殼求最大三角形面積

CrossDolphin發表於2016-07-02



連結:戳這裡


E. Alyona and Triangles
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n points with integer coordinates on the plane. Points are given in a way such that there is no triangle, formed by any three of these n points, which area exceeds S.

Alyona tried to construct a triangle with integer coordinates, which contains all n points and which area doesn't exceed 4S, but, by obvious reason, had no success in that. Please help Alyona construct such triangle. Please note that vertices of resulting triangle are not necessarily chosen from n given points.

Input
In the first line of the input two integers n and S (3 ≤ n ≤ 5000, 1 ≤ S ≤ 1018) are given — the number of points given and the upper bound value of any triangle's area, formed by any three of given n points.

The next n lines describes given points: ith of them consists of two integers xi and yi ( - 108 ≤ xi, yi ≤ 108) — coordinates of ith point.

It is guaranteed that there is at least one triple of points not lying on the same line.

Output
Print the coordinates of three points — vertices of a triangle which contains all n points and which area doesn't exceed 4S.

Coordinates of every triangle's vertex should be printed on a separate line, every coordinate pair should be separated by a single space. Coordinates should be an integers not exceeding 109 by absolute value.

It is guaranteed that there is at least one desired triangle. If there is more than one answer, print any of them.

Example
input
4 1
0 0
1 0
0 1
1 1
output
-1 0
2 0
0 2
Note


題意:

給出n個點,要求找出一個最大的三角形使得三角形覆蓋這n個點。三角形的面積不能超過四倍的點集區域面積。


思路:

旋轉卡殼求出凸包內最大的三角形ABC(圖片來自卿神)


顯然三角形A'B'C'就是答案。


程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct point{
    double x,y;
    point(double x=0,double y=0):x(x),y(y){}
    bool operator < (const point &a)const{
        if(x==a.x) return y<a.y;
        return x<a.x;
    }
};
const double eps=1e-8;
typedef point vec;
vec operator + (point a,point b){
    return vec(a.x+b.x,a.y+b.y);
}
vec operator - (point a,point b){
    return vec(a.x-b.x,a.y-b.y);
}
vec operator * (point a,double t){
    return vec(a.x*t,a.y*t);
}
vec operator / (point a,double t){
    return vec(a.x/t,a.y/t);
}
double cross(vec a,vec b){
    return a.x*b.y-a.y*b.x;
}
int dcmp(double x){
    if(fabs(x)<=eps) return 0;
    return x<0?-1:1;
}
bool cmp(point a,point b){
    if(dcmp(a.x-b.x)==0) return a.y<b.y;
    return a.x<b.x;
}
point p[5010];
void convexhull(point *s,int &n){
    sort(s,s+n);
    ///sort(s,s+n,cmp);
    int m=0;
    s[n]=s[0];
    for(int i=0;i<n;i++){
        while(m>1 && dcmp(cross(p[m-1]-p[m-2],s[i]-p[m-2]))<=0)
            m--;
        p[m++]=s[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--){
        while(m>k && dcmp(cross(p[m-1]-p[m-2],s[i]-p[m-2]))<=0)
            m--;
        p[m++]=s[i];
    }
    m--;
    n=m;
    for(int i=0;i<n;i++) s[i]=p[i];
}
point s[5010];
ll S;
int n;
int main(){
    scanf("%d%I64d",&n,&S);
    for(int i=0;i<n;i++) scanf("%lf%lf",&s[i].x,&s[i].y);
    convexhull(s,n);
    s[n]=s[0];
    s[n+1]=s[1];
    s[n+2]=s[2];
    double sum=0.0;
    int ansi=0,ansj=1,ansk=2;
    int j=1,k=2;
    for(int i=0;i<n;i++){
        while(fabs(cross(s[j]-s[i],s[k+1]-s[i]))-fabs(cross(s[j]-s[i],s[k]-s[i]))>=eps)
            k=(k+1)%n;
        while(fabs(cross(s[k]-s[i],s[j+1]-s[i]))-fabs(cross(s[k]-s[i],s[j]-s[i]))>=eps)
            j=(j+1)%n;
        double tmp=fabs(cross(s[j]-s[i],s[k]-s[i]));
        if(dcmp(tmp-sum)>=0){
            ansi=i;
            ansj=j;
            ansk=k;
            sum=tmp;
        }
    }
    point A=s[ansj]+s[ansk]-s[ansi];
    point B=s[ansi]+s[ansk]-s[ansj];
    point C=s[ansi]+s[ansj]-s[ansk];
    printf("%.0f %.0f\n",A.x,A.y);
    printf("%.0f %.0f\n",B.x,B.y);
    printf("%.0f %.0f\n",C.x,C.y);
    return 0;
}


相關文章