注册 登录
编程论坛 数据结构与算法

一道简单题求思路

chuangketie1 发布于 2013-10-18 17:07, 526 次点击
There is a number sequence A1,A2,...,An. Let f(i)=min{|Aj-Ai| ,j<i}. You only need to compute f(1)+f(2)+...+f(n).
Input
The first line is an integer c which shows the number of cases. For each case there is a single line. The first number is n and the next n positive integer is A1 to An. (n<10000, Ai<100000)

Output

For each case print the answer at a single line.

Sample Input
2
3 1 2 3
4 2 3 1 4

Sample Output
2
3
我只有暴力直接算的思路,因此总是超时。不知道各位大神有什么想法,这题可以用动态规划解吗?
3 回复
#2
yuccn2013-10-18 21:41
翻译一下再贴出来吧,至少写上自己的一些代码或者思路。就怎么一个题目,估计很少人会看
#3
liuchaowangl2013-10-19 12:47
先排个序,计算相邻数的差就是
#4
liuchaowangl2013-10-19 12:49
最大值减去最小值
1