-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1330.cpp
More file actions
61 lines (52 loc) · 1.53 KB
/
1330.cpp
File metadata and controls
61 lines (52 loc) · 1.53 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution
{
public:
int get_max(vector<int> &v)
{
int res = INT_MIN;
for(auto x : v)
res = max(res,x);
return res;
}
int get_min(vector<int> &v)
{
int res = INT_MAX;
for(auto x : v)
res = min(res,x);
return res;
}
int maxValueAfterReverse(vector<int>& nums) {
int res = 0;
int n = nums.size();
for(int i=0;i<n-1;i++)
res += abs(nums[i] - nums[i + 1]);
int maxv = 0;
for(int i=0;i<n;i++)
{
if(i != n - 1)
maxv = max(maxv,abs(nums[0] - nums[i + 1]) -
abs(nums[i] - nums[i + 1])); // 左端点为0右端点为i
if(i != 0)
maxv = max(maxv,abs(nums[n - 1] - nums[i - 1]) -
abs(nums[i] - nums[i - 1])); // 右端点为n-1,左端点为i
}
int mx[4] = {1,1,-1,-1};
int my[4] = {1,-1,1,-1};
for(int i=0;i<4;i++) // 枚举四种情况
{
vector<int> v1,v2;
for(int j=0;j<n-1;j++)
{
int a = mx[i] * nums[j];
int b = my[i] * nums[j + 1];
int cur = abs(nums[j] - nums[j + 1]);
v1.push_back(a + b - cur);
v2.push_back(a + b + cur);
}
int a = get_max(v1);
int b = get_min(v2);
maxv = max(maxv,a - b);
}
return res + maxv;
}
};