F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Wavy Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 642    Accepted Submission(s): 270


Problem Description
An array $a$ of length $n$ is said to be wavy, if for each $1 < i < n$ either $a_i > \max \{a_{i-1},a_{i+1}\}$ or $a_i < \min \{a_{i-1},a_{i+1}\}$ holds.

You are given an array $b$ of length $n$ $(1\leq b_i\leq 10^9)$ , consisting of integers. You want to make the array wavy. To do that you can spend some coins, with each coin you can make one element in $b$ increase or decrease by $1$. Calculate the minimum number of coins you need to spend to make the array wavy.
 

Input
The first line contains the number of test cases $T$ $(1\le T\le 10^3)$.

The first line of each test case contains one integer $n$ $(1\leq n\leq 10^6)$ - the length of array $b$ .

The second line contains $n$ integers $b_1,b_2,\cdots,b_n$ $(1\leq b_i\leq 10^9)$ - the array $b$ .

It's guarantee that the sum of $n$ among all test cases is not greater than $3\times 10^6$ .
 

Output
For each test case, output one integer, the minimum number of coins you need to spend to make the array wavy.

 

Sample Input
3 4 1 7 6 5 6 1 2 3 4 5 6 6 1 1 4 5 1 4
 

Sample Output
2 4 4
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-22 02:29:07, Gzip enabled