题解:AT_abc250_f [ABC250F] One Fourth

· · 题解

前置知识

因为 N \le 10^5 ,所以说我们无法暴力枚举每一个相不邻的顶点,考虑使用双指针优化。指针 l 从顶点 1 开始,r 从顶点 3 开始。每次如果较小的一半小于 \frac{a}{4},那么 r+1,否则 l+1。每一次 l+1 时,计算最小答案,最后输出即可。

注意:披萨的顶点是一个圆,所以每次 r+1 时需要判断一下是否超过 n,超过即变为 1。

AC 代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
struct node{
    int x,y;
}a[N];
int n,ans=9e18;
int gets(node d1,node d2,node d3)//计算面积 
{
    int xx=d1.x,yy=d1.y;
    int x2=d2.x,y2=d2.y;
    int x3=d3.x,y3=d3.y;
    return ((x2-xx)*(y3-yy)-(y2-yy)*(x3-xx));//返回两倍面积 
}
signed main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&a[i].x,&a[i].y);
    int tots=0;
    for(int i=2;i<=n-1;i++)
        tots+=gets(a[1],a[i],a[i+1]);//计算总面积 
    int l=1,r=2,now=0,w;
    for(;l<=n;l++)
    {
        while(4ll*(now+(w=gets(a[l],a[r],a[(r+1>n?1:r+1)])))<tots)//不满足条件 
        {
            now+=w;
            if(r>=n)r=1;//坑人点 
            else r++;
        }
        ans=min(ans,min(abs(tots-now*4),abs(tots-(now+w)*4)));//取最小 
        int t;
        if(l>=n)t=1;
        else t=l+1;
        now-=gets(a[l],a[t],a[r]);
    }
    printf("%lld",ans);//完结散花 
    return 0;
}

AC 记录