-
Notifications
You must be signed in to change notification settings - Fork 0
/
B. Suffix Operations.cpp
44 lines (43 loc) · 1.21 KB
/
B. Suffix Operations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma GCC optimize("O2")
#include <fstream>
#include <string>
#include <iostream>
#include <bitset>
#include <math.h>
#include <string>
#include <algorithm>
#include<bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
int t;
int n;
int main(){
cin >> t;
for (int z = 0; z < t; z++){
cin >> n;
vector<long long> st;
long long temp;
for (int x = 0; x < n; x++){
cin >> temp;
st.push_back(temp);
}
long long tanswer = 0;
long long banswer = 10000000000000000;
for (int x = n - 2; x >= 0; x--){
tanswer += abs(st[x] - st[x + 1]);
}
//cout << tanswer << "\n";
banswer = tanswer;
for (int x = 1; x < n - 1; x++){
long long curanswer = tanswer;
curanswer -= abs(st[x] - st[x + 1]);
curanswer -= abs(st[x - 1] - st[x]);
curanswer += abs(st[x + 1] - st[x - 1]);
banswer = min(banswer, curanswer);
}
banswer = min(banswer, tanswer - abs(st[0] - st[1]));
banswer = min(banswer, tanswer - abs(st[n - 2] - st[n - 1]));
cout << banswer << "\n";
}
}