HDU 5805-nanoape Loves Sequence (bestcoder Round #86)

Source: Internet
Author: User

First find the next difference between the largest, the second largest, the third largest

Deleting the end point will reduce a value, delete the remaining points will be reduced by two values, a new value, so the new and existing maximum value comparison of the maximum can be

1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 #defineLL Long Long6 Const intN =100005;7 intT, N, p1, p2, p3;8 LL A[n];9 LL S1[n], s2[n];Ten LL sum; One intMain () A { -scanf"%d", &t); -      while(t--) the     { -scanf"%d", &n); -          for(inti =1; I <= N; i++) -scanf"%i64d", &a[i]); +P1 = P2 = P3 =0; -s1[0] = -1; +          for(inti =1; I < n; i++) A         { atS1[i] = ABS (A[i +1]-a[i]);//adjacent -             if(S1[P1] < s1[i]) P1 = i;//Maximum -         } -          for(inti =1; I < n; i++) -         { -             if(i = = p1)Continue; in             Else if(S1[P2] < s1[i]) P2 = i;//second largest -         } to          for(inti =1; I < n; i++) +         { -             if(i = = P1 | | i = p2)Continue;//The third largest the             Else if(S1[P3] < s1[i]) P3 =i; *         } $          for(inti =2; I < n; i++)Panax NotoginsengS2[i] = ABS (a[i+1]-a[i-1]);//Remove I point new value -          thesum =0; +         if(P1 = =1) Sum + =S1[P2]; A         ElseSum + =S1[P1]; the          for(inti =2; I < n; i++) +         { -             if(P1 = =-1|| P1 = =i) $             { $                 if(P2 = =-1|| P2 = = i) sum + =Max (S1[P3], s2[i]); -                 ElseSum + =Max (S1[P2], s2[i]); -             }  the             ElseSum + =Max (S1[P1], s2[i]); -         }Wuyi         if(P1 = = N-1) Sum + =S1[P2]; the         ElseSum + =S1[P1]; -printf"%i64d\n", sum); Wu     } -}

HDU 5805-nanoape Loves Sequence (bestcoder Round #86)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.