P5098 [USACO04OPEN] Cave Cows 3

纯粹的發表於2024-07-28

原題連結

題解

看到“最大值”“曼哈頓距離”,我們不禁想到轉換成切比雪夫距離之後求最大值

code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=1e8;

struct node
{
    ll v,id;
};

bool cmp(node a,node b)
{
    return a.v<b.v;
}

void solve()
{
    ll n;
    cin>>n;

    vector<node> heng,shu;
    vector<ll> ans(n+1);
    for(ll i=1;i<=n;i++)
    {
        ll x,y;
        cin>>x>>y;

        heng.push_back({x+y,i});
        shu.push_back({x-y,i});
    }

    sort(heng.begin(),heng.end(),cmp);
    sort(shu.begin(),shu.end(),cmp);

    ll xmin=heng[0].v,xmax=heng[n-1].v;
    for(auto it:heng)  ans[it.id]=max(ans[it.id],max(it.v-xmin,xmax-it.v));

    ll ymin=shu[0].v,ymax=shu[n-1].v;
    for(auto it:shu)  ans[it.id]=max(ans[it.id],max(it.v-ymin,ymax-it.v));

    ll res=0;

    for(ll i=1;i<=n;i++) res=max(res,ans[i]);
    cout<<res<<'\n';
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}


相關文章