2021-3-27 Spring Individual Competition Supplement (B-Minimal Area (Find the area of a triangle by cross multiplication))

B-Minimal Area (Find the area of ​​a triangle by cross multiplication)

题目链接: link.
原题描述:
You are given a strictly convex polygon. Find the minimal possible area of non-degenerate triangle whose vertices are the vertices of the polygon.
The first line contains a single integer n (3 ≤ n ≤ 200000) — the number of polygon vertices.

Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of polygon vertices.

The polygon is guaranteed to be strictly convex. Vertices are given in the counterclockwise order.
It is known that the area of triangle whose vertices are the integer points on the grid is either integer or half-integer.

Output a single integer — the required area, multiplied by 2.

Title:

: Given the n vertices of a convex polygon in counterclockwise order, find the triangle with the smallest area and all vertices are on the convex polygon, and output twice the area of ​​the triangle

Idea
The triangle with the smallest area must be the area of ​​three connected vertices. Traverse every three connected vertices in order to find the smallest value.

Knowing the coordinates of the three points to find the area of ​​the triangle, this uses the cross product formula. The reasoning process is as follows:
Insert picture description here
Insert picture description here
Therefore, the cross product method to find the area of ​​the triangle is: abs((x2y3-x3y2)-(x1y3-x3y1)+(x1y2 -x2y1)), merge once to be abs((x2-x1) (y3-y1)-(y2-y1) (x3-x1))

code show as below

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct node {
    
    
    long double x, y;
} a[N];
long long s(node a, node b, node c) {
    
    
    long long x1 = a.x;
    long long x2 = b.x;
    long long x3 = c.x;
    long long y1 = a.y;
    long long y2 = b.y;
    long long y3 = c.y;
    long long sum1 = x2 * y3 - x3 * y2;
    long long sum2 = x1 * y3 - x3 * y1;
    long long sum3 = x1 * y2 - x2 * y1;
    return abs(sum1 - sum2 + sum3);
}
int main() {
    
    
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
    
    
        cin >> a[i].x >> a[i].y;
    }
    a[n + 1] = a[1];//类似于约瑟夫环遍历方法,因为下面是从i=3开始的。
    a[n + 2] = a[2];
    n = n + 2;
    long long int m = 0x3f3f3f3f3f3f3f3f;
    for (int i = 3; i <= n; i++) {
    
    
        long long int f = s(a[i], a[i - 1], a[i - 2]);
        m = min(m, f);
    }
    cout << m << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_53827449/article/details/115271603