gym224647B

gym224647B

题意:

在二维平面中·选出一个面积最小的三角形,输出这个三角形面积的两倍。

解法:

首先,最优解一定在相邻最近的三个点中产生。
然后我们就可以用向量求三角形的面积。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

#define LL long long
const int N = 4e5 + 10;
const LL MAX = 9223372036854775805;

struct Vec { 
    LL x , y; 
} p[N];

inline LL labs(LL x) { 
    return x < 0 ? - x : x ; 
}
inline LL calc(Vec a , Vec b) { 
    return labs (a.x * b.y - a.y * b.x); 
}
LL ans = MAX,n;

int main () {
    scanf("%lld",&n); 
    for(int i = 1 ; i <= n ; i++) {
        scanf("%lld%lld",&p[i].x,&p[i].y);
        p[i + n] = p[i];
    }
    for(int i = 1 ; i <= n ; i++) {
        int j = i + 1,k = j + 1 ; // i - j , k - j 
        Vec a,b;
        a.x = p[i].x - p[j].x; 
        a.y = p[i].y - p[j].y;
        b.x = p[k].x - p[j].x; 
        b.y = p[k].y - p[j].y;
        LL res = calc(a,b) ;
        if(res != 0) ans = min(ans,res);
    }
    printf("%lld\n",ans); 
    //system("pause");
    return 0 ;
}
posted @ 2019-09-03 21:17  西窗夜雨  阅读(103)  评论(0编辑  收藏  举报