传送门
解法一:
将绝对值去掉, 讨论 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;
}