Wannafly模拟赛4 B题

本文介绍了一种名为FST距离的特殊距离计算方法,并通过分析给出了实现该算法的具体步骤及代码示例。针对一系列特征值,算法旨在寻找两元素间的最大FST距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目描述

FST作为小朋友,经常会遇到和距离有关的问题,但是他已经厌倦了曼哈顿距离和欧几里德距离,所以FST就定义了一种FST距离。
这种距离并不用于空间或平面中,而运用于FST发明的一些神奇的算法中(唔... ...)。
设i号元素的特征值为Ai,则i和j的FST距离是 |i2 - j2|+|Ai2 - Aj2|。
为了实现某新的数据结构,FST想在一大堆元素中找出距离最大的一对元素,他不关心是哪一对元素,只想求出最大距离。

输入描述:

第一行,一个正整数n,为元素个数。
第二行,n个正整数Ai为这n个元素的特征值。

输出描述:

一行,一个正整数表示最大距离。long long请用lld
示例1

输入

2
4 3

输出

10

备注:

n≤105,Ai≤109






分析:

去掉绝对值后一有四种情况     写一下四种情况的公式   化简一下其实就是两个公式     排三次序就行











AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define FIN     freopen("input.txt","r",stdin)
#define FOUT    freopen("output.txt","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
struct node{
    LL a,index;
}ans[1000005];
int cmp1(node x,node y){
    return x.a*x.a+x.index*x.index>y.a*y.a+y.index*y.index;
}
int cmp2(node x,node y){
    return x.a*x.a-x.index*x.index>y.a*y.a-y.index*y.index;
}
int cmp3(node x,node y){
    return x.index*x.index-x.a*x.a>y.index*y.index-y.a*y.a;
}
int main (){
    int n;
    while (scanf ("%d",&n)!=EOF){
        for (int i=0;i<n;i++){
            scanf ("%lld",&ans[i].a);
            ans[i].index=i+1;
        }
        sort(ans,ans+n,cmp1);
        LL t1=ans[0].a*ans[0].a+ans[0].index*ans[0].index-ans[n-1].a*ans[n-1].a-ans[n-1].index*ans[n-1].index;
        sort(ans,ans+n,cmp2);
        LL temp1=ans[0].a*ans[0].a-ans[0].index*ans[0].index;
        sort(ans,ans+n,cmp3);
        LL temp2=ans[0].index*ans[0].index-ans[0].a*ans[0].a;
        LL t2=temp1+temp2;
        printf ("%lld\n",t1>t2?t1:t2);
         
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值