zoukankan      html  css  js  c++  java
  • Odd sum CodeForces

    Odd sum CodeForces - 797B

    好方法:贪心 贪心2

    糟糕(不用动脑)的方法:dp

    ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大。

    但是,仍然容易写挂!(注意细节)

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 typedef long long LL;
     6 LL ans[100010][2];
     7 LL a[100010];
     8 LL n;
     9 int main()
    10 {
    11     LL i;
    12     scanf("%I64d",&n);
    13     for(i=1;i<=n;i++)
    14         scanf("%I64d",&a[i]);
    15     //memset(ans,144,sizeof(ans));
    16     ans[0][1]=-0x6f6f6f6f;
    17     for(i=1;i<=n;i++)
    18     {
    19         if(a[i]%2==0)
    20         {
    21             ans[i][0]=max(max(ans[i-1][0],ans[i-1][0]+a[i]),a[i]);
    22             //if(ans[i-1][1]%2==1)
    23             ans[i][1]=max(ans[i-1][1],ans[i-1][1]+a[i]);
    24         }
    25         else
    26         {
    27             //if(ans[i-1][1]%2==1)
    28             ans[i][0]=max(ans[i-1][0],ans[i-1][1]+a[i]);
    29             ans[i][1]=max(max(ans[i-1][1],ans[i-1][0]+a[i]),a[i]);
    30         }
    31     }
    32     //if(ans[n][1]>0)
    33     printf("%I64d",ans[n][1]);
    34     //else
    35         //printf("0");
    36     return 0;
    37 }
  • 相关阅读:
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
  • 原文地址:https://www.cnblogs.com/hehe54321/p/cf-797b.html
Copyright © 2011-2022 走看看