Wannafly模拟赛4 B Distance

传送门
解法一:
将绝对值去掉, 讨论 i^2+ai^2的最大最小值的差和i^2-ai^2的最大最小值的差谁大。用四个变量维护。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
typedef long long LL;
const LL inf = 1e18+10;
struct node{
   LL x, y;
}a[MAXN];
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        for(int i=1; i<=n; ++i)
        {
            scanf("%lld", &a[i].x);
            a[i].x=a[i].x*a[i].x;
            a[i].y=(LL)i*i;
        }
        LL max1=-inf, max2=-inf, min1=inf, min2=inf;
        for(int i=1;i<=n;++i)
        {
            LL x=a[i].x;
            LL y=a[i].y;
            min1=min(min1, x+y);
            min2=min(min2, y-x);
            max1=max(max1, x+y);
            max2=max(max2, y-x);
        }
        printf("%lld\n", max(max1-min1,max2-min2));
    }
    return 0;
}

解法二:
利用曼哈顿最远距离

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
const int  N = 100003;
typedef long long LL;
const LL  inf =  1e18;
LL a[N][2];

int main()
{
    LL n;
    scanf("%lld", &n);
    for(LL i=1;i<=n;i++)
    {
        LL x;
        scanf("%lld", &x);
        a[i][0]=i*i;
        a[i][1]=x*x;
    }
    LL ans = 0, mi, mx, t;
    for (int k=0; k<(1<<2); k++)
    {
        mi = inf, mx = -inf;
        for (int i=1; i<=n; i++)
        {
            t = 0;
            for (int j=0; j<2; j++)
                if ((1<<j) & k) t += a[i][j];
                else t -= a[i][j];
            mi = min(mi, t);
            mx = max(mx, t);
        }
        ans = max(ans, mx-mi);
    }
    printf("%lld\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值