Wannafly模拟赛4 B题

来源:互联网 发布:爱范儿 知乎 编辑:程序博客网 时间:2024/06/01 10:54


题目描述

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

输入描述:

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

输出描述:

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

输入

24 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;}



原创粉丝点击