[2016-04-17] [Gym] [100947] C [Rotate It!]

Source: Internet
Author: User

    • Time: 2016-04-17-16:35:01 Sunday
    • Title Number: [2016-04-17][gym][100947][c][rotate It!]
    • Topic: Given a column of numbers, from the first start to take, every other number to take a number, there is an operation is to put the first number in the last, can be unlimited operation, ask the maximum value is how much
    • Analysis:
      • Can be seen as a ring, each ring, can start from each ring, ask the maximum value is how much
      • Number is an even number, very simple, is to find the odd position and the sum of the number of pairs of the maximum value
      • Number is an odd number,
      • Analysis can be known that there must be adjacent two numbers in the selected sequence, then only need to enumerate the adjacent two series, calculate the maximum value can be
        • Example of a sample 1 5 3 2 4
        • First 1 4 connected, then 15, one analogy,
        • If you just look at the format of the sample, that is, 1 5 3 2 4, the adjacent two digits are i,j,
        • Assuming I is in an odd position, then the answer becomes the sum of the I-front odd digits and the number of pairs after J.
        • Even the same
    • Problems encountered:
  
 
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int maxn = 1E4 + 10;
  5. long long a[maxn],b[maxn];
  6. int main(){
  7. int t;
  8. scanf("%d",&t);
  9. while(t--){
  10. int n,tmp;
  11. long long s[2] = {0};
  12. scanf("%d",&n);
  13. if(!(n&1)){
  14. for(int i = 0; i < n ; ++i){
  15. scanf("%d",&tmp);
  16. s[i&1] += tmp;
  17. }
  18. printf("%I64d\n",max(s[0],s[1]));
  19. }else {
  20. a [ 0 " = b [ 0 ] Span class= "pun" >= 0 ;
  21. for(int i = 1 ; i <= n; ++i){
  22. scanf("%d",&tmp);
  23. if(i & 1){
  24. a[i] = a[i - 1] + tmp;
  25. b[i] = b[i - 1];
  26. }else {
  27. a[i] = a[i - 1];
  28. b[i] = b[i - 1] + tmp;
  29. }
  30. }
  31. long long ans = -1E4 * 1E9;
  32. for(int i = 0 ; i < n ; ++i){
  33. if(i & 1){
  34. ans = max(ans , a[i] + b[n] - b[i]);
  35. }
  36. else{
  37. ans = max(ans , b[i] + a[n] - a[i]);
  38. }
  39. }
  40. printf("%I64d\n",ans);
  41. }
  42. }
  43. return 0;
  44. }


From for notes (Wiz)

[2016-04-17] [Gym] [100947] C [Rotate It!]

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.