标签:
先找相邻差值的最大,第二大,第三大
删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define LL long long 6 const int N = 100005; 7 int t, n, p1, p2, p3; 8 LL a[N]; 9 LL s1[N], s2[N]; 10 LL sum; 11 int main() 12 { 13 scanf("%d", &t); 14 while(t--) 15 { 16 scanf("%d", &n); 17 for(int i = 1; i <= n; i++) 18 scanf("%I64d", &a[i]); 19 p1 = p2 = p3 = 0; 20 s1[0] = -1; 21 for(int i = 1; i < n; i++) 22 { 23 s1[i] = abs(a[i + 1] - a[i]);//相邻 24 if(s1[p1] < s1[i] ) p1 = i;//最大 25 } 26 for(int i = 1; i < n; i++) 27 { 28 if(i == p1) continue; 29 else if(s1[p2] < s1[i]) p2 = i;//第二大 30 } 31 for(int i = 1; i < n; i++) 32 { 33 if(i == p1 || i == p2) continue;//第三大 34 else if(s1[p3] < s1[i]) p3 = i; 35 } 36 for(int i = 2; i < n; i++) 37 s2[i] = abs(a[i+1] - a[i-1]);//去掉 i 点新增值 38 39 sum = 0; 40 if(p1 == 1) sum += s1[p2]; 41 else sum += s1[p1]; 42 for(int i = 2; i < n; i++) 43 { 44 if(p1 == i-1 || p1 == i) 45 { 46 if(p2 == i-1 || p2 == i) sum += max(s1[p3], s2[i]); 47 else sum += max(s1[p2], s2[i]); 48 } 49 else sum += max(s1[p1], s2[i]); 50 } 51 if(p1 == n - 1) sum += s1[p2]; 52 else sum += s1[p1]; 53 printf("%I64d\n",sum); 54 } 55 }
HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)
标签:
原文地址:http://www.cnblogs.com/nicetomeetu/p/5744991.html