首页 > 其他分享 >最大周长(贪心)

最大周长(贪心)

时间:2022-08-22 17:48:43浏览次数:80  
标签:贪心 右边 最大 int leq inf include 周长

题意

题目链接:https://www.acwing.com/problem/content/4608/

数据范围

\(3 \leq n \leq 3 \times 10^5\)

思路

首先需要注意的是,这里的距离指的是曼哈顿距离,而不是欧几里得距离。

我们观察\(n\)个点的凸多边形的周长,通过线段的平移发现,等价于一个外接长方形的周长。

再继续观察发现,只需要四个点就可以确定这个外接长方形,即:最上边的点、最下边的点、最左边的点和最右边的点。

因此,当\(i > 3\)时,答案就已经确定。下面考虑\(i = 3\)的情况。

我们枚举所有点,将其作为三角形的左下角(左上角、右上角、右下角),剩下两个点必然为最上边的点和最右边的点(最下边的点和最右边的点、最下边的点和最左边的点、最上边的点和最左边的点)。取最大值即可。

代码

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

using namespace std;

const int N = 300010, inf = 0x3f3f3f3f;

int n;
int X[N], Y[N];

int main()
{
    scanf("%d", &n);
    int u = -inf, d = inf, l = inf, r = -inf;
    for(int i = 1; i <= n; i ++) {
        scanf("%d%d", &X[i], &Y[i]);
        u = max(u, Y[i]), d = min(d, Y[i]);
        r = max(r, X[i]), l = min(l, X[i]);
    }
    int res = 0;
    for(int i = 1; i <= n; i ++) {
        res = max(res, u - Y[i] + r - X[i]); //左下角
        res = max(res, u - Y[i] + X[i] - l); //右下角
        res = max(res, Y[i] - d + r - X[i]); //左上角
        res = max(res, Y[i] - d + X[i] - l); //右上角
    }
    printf("%d ", 2 * res);
    for(int i = 4; i <= n; i ++) {
        printf("%d ", 2 * (u - d) + 2 * (r - l));
    }
    printf("\n");
    return 0;
}

标签:贪心,右边,最大,int,leq,inf,include,周长
From: https://www.cnblogs.com/miraclepbc/p/16613625.html

相关文章