Wannafly模拟赛4 B题
来源:互联网 发布:爱范儿 知乎 编辑:程序博客网 时间:2024/06/01 10:54
题目描述
FST作为小朋友,经常会遇到和距离有关的问题,但是他已经厌倦了曼哈顿距离和欧几里德距离,所以FST就定义了一种FST距离。
这种距离并不用于空间或平面中,而运用于FST发明的一些神奇的算法中(唔... ...)。
设i号元素的特征值为Ai,则i和j的FST距离是 |i2 - j2|+|Ai2 - Aj2|。
为了实现某新的数据结构,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;}
阅读全文
0 0
- Wannafly模拟赛4 B题
- Wannafly模拟赛4 B题 Distance 【最大曼哈顿距离】
- Wannafly模拟赛4 B Distance
- Wannafly模拟赛4 B Distance
- Wannafly模拟赛4
- Wannafly模拟赛4 B.Distance 最大曼哈顿距离
- Wannafly模拟赛4 题解
- Wannafly模拟赛3-B 贝伦卡斯泰露(DFS)
- Wannafly模拟赛2 A题
- Wannafly模拟赛5 A题
- Nowcode Sum(Wannafly模拟赛4)
- Wannafly 挑战赛 4 C 题 割草机 【模拟】
- 10.9wannafly模拟赛
- Wannafly模拟赛3
- Wannafly模拟赛3 题解
- Wannafly模拟赛3 题解
- Wannafly模拟赛5 ASplit
- Wannafly模拟赛5 DAria
- 前端之nodejs(四)
- CentOS 安装PHP7的正确姿势
- 接下来的博客计划
- 《Hello Vue.js之道阻且长》
- hdu-1128 自私的数
- Wannafly模拟赛4 B题
- 安卓Activity高亮设置
- 以Point类为基础,定义一个平面中的Circle类: 1、编写一个无参的构造函数; 2、编写一个有参的构造函数; 3、在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实
- 2017已来,最全面试总结——这些Android面试题你一定需要
- Codeforces Problem 333A
- css笔记
- 使用chrome对前端调试
- 洛谷八连—逃避
- Android Studio初体验