当前位置:   article > 正文

HDU 5806 NanoApe Loves Sequence

HDU 5806 NanoApe Loves Sequence

链接:点击打开链接

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. int data[100005];
  8. int sub[100005];
  9. int main(){
  10. int t,n;
  11. int max1,max2,max3;
  12. int left,right,now;
  13. __int64 result;
  14. cin>>t;
  15. while(t--){
  16. cin>>n;
  17. result=0;
  18. for(int i=1;i<=n;i++)
  19. scanf("%d",&data[i]);
  20. for(int i=1;i<=n-1;i++)
  21. sub[i]=abs(data[i+1]-data[i]);
  22. sort(sub+1,sub+n);
  23. //for(int i=1;i<=n-1;i++)
  24. //printf("%d%c",sub[i],i==n-1?'\n':' ');
  25. max1=sub[n-1];
  26. max2=sub[n-2];
  27. max3=sub[n-3];
  28. //printf("%d %d %d\n",max1,max2,max3);
  29. if(max1==abs(data[2]-data[1]))
  30. result+=max2;
  31. else
  32. result+=max1;
  33. if(max1==abs(data[n]-data[n-1]))
  34. result+=max2;
  35. else
  36. result+=max1;
  37. //printf("%d\n",result);
  38. for(int i=2;i<n;i++){
  39. now=abs(data[i+1]-data[i-1]);
  40. left=abs(data[i]-data[i-1]);
  41. right=abs(data[i+1]-data[i]);
  42. if(now>=max1)
  43. result+=now;
  44. else{
  45. if(left==max1){
  46. if(right==max2)
  47. result+=max(max3,now);
  48. else
  49. result+=max(max2,now);
  50. }
  51. else if(right==max1){
  52. if(left==max2)
  53. result+=max(max3,now);
  54. else
  55. result+=max(max2,now);
  56. }
  57. else
  58. result+=max1;
  59. }
  60. //printf("%d:%d %d %d %d\n",i,left,right,now,result);
  61. }
  62. printf("%I64d\n",result);
  63. }
  64. return 0;
  65. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/551351
推荐阅读
相关标签
  

闽ICP备14008679号