标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 440 Accepted Submission(s): 205
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main() { int t; int n; int cha=0; int cha2=0; int a[100005]={0}; int maxx1; int first; int maxx2; int second; int maxx3; int third; int sum=0; scanf("%d",&t); for(int z=0;z<t;z++){ sum=0; maxx1=0; maxx2=0; maxx3=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); if(i!=0){ cha=abs(a[i]-a[i-1]); if(maxx1<cha){ maxx1=cha; first=i; } } } for(int i=1;i<n;i++){ cha=abs(a[i]-a[i-1]); if(maxx2<cha){ if(maxx2<=maxx1){ if(i==first){ continue; }else{ maxx2=cha; second=i; } } } } for(int i=1;i<n;i++){ cha=abs(a[i]-a[i-1]); if(maxx2<cha){ if(maxx3<=maxx1&&maxx3<=maxx2){ if(i==first||i==second){ continue; }else{ maxx3=cha; third=i; } } } } for(int i=1;i<n-1;i++){ cha2=abs(a[i+1]-a[i-1]); if(maxx1<=cha2){ sum+=cha2; } if(maxx1>cha2){ if(i==first&&i+1==second||i==second&&i+1==first){ if(maxx3!=0){ if(maxx3>=cha2){ sum+=maxx3; }else{ sum+=cha2; } }else{ sum+=cha2; } } if(i==first||i==first-1){ if(cha2<=maxx2){ sum+=maxx2; }else{ sum+=cha2; } }else{ sum+=maxx1; } } } if(first==1){ sum+=maxx2; sum+=maxx1; } if(first==n-1){ sum+=maxx2; sum+=maxx1; } if(first!=1&&first!=n-1){ sum+=(2*maxx1); } printf("%d\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TWS-YIFEI/p/5745639.html