HDU - 6343 - Problem L. Graph Theory Homework
题意:
有 n 个点, 每个点均有一个权值wi,第 i 个点到第 j 个点的距离为⌊sqrt(|wi−wj|)⌋
求出 1 到 n 的最短距离
从 1 到 n 有两种选择
1、直接1 -> n ,路径长度为⌊sqrt(|w1−wn|)⌋
2、从1经过一些点然后再到n,设1->k->n ,设三点的权值为a,b,c,因为要使其最小,所以|a-b| < |a-c|,对于abc有两种状况,a < b < c 和 a > b > c
然后就是比较
利用基本不等式可以证明前面的较大
所以每次直接从1到n即为最短距离
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int T, n, a[N];
int main()
{
scanf("%d",&T);
while(T--) {
scanf("%d", &n);
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
printf("%d\n", (int)(sqrt(abs(a[1]-a[n]))));
}
return 0;
}