POJ 2181 Mm... train of thought questions

Source: Internet
Author: User

Previously, the sum of the largest continuous subsequences was developed by myself. linear algorithms are quite simple. I haven't updated my problem solution for a long time. I don't know when I will be able to come out of the solution for 1,000, so I have a good question. In a sequence, take the numbers. The odd digits are the plus digits, and the even digits are the minus digits. The greatest sum is required. I had no idea, and I was not planning to think about it. I was too tired at noon. I had A rest and thought about it after I got up. I wrote 1A and wrote A again. Well, this is A question of water. I only want to cheat =

#include<iostream>#define MAXN 150001using namespace std;int date[MAXN];int main(){    int n,i;    while( scanf( "%d",&n )!=EOF )    {           date[0]=0;           for( i=1;i<=n;i++ )                scanf( "%d",&date[i] );           int ans=0;           i=0;           while( date[i+1]>=date[i] )                  i++;           ans+=date[i];                      for(;i<=n;i++ )           {                while( date[i+1]<=date[i] && i<=n ) i++;                if( i!=n )                      ans-=date[i];                 while( date[i+1]>=date[i] && i<=n ) i++;                       ans+=date[i];           }           printf( "%d\n",ans );               }    return 0;}


#include<iostream>#define MAXN 150001using namespace std;int max( int a,int b ){ return a>b?a:b; }int main(){    int n,i;    while( scanf( "%d",&n )!=EOF )    {           bool add=true;           int a,b;           a=0;int ans=0;           scanf( "%d",&a );           for( i=2;i<=n;i++ )           {                scanf( "%d",&b );                if( add )                {                    if( a>b )                    {                             add=!add;                         ans+=a;                    }                    if( b>=a && i==n )                        ans+=b;                }                else                {                    if( a<b )                    {                             add=!add;                         ans-=a;                    }                    if( a<b && i==n )                        ans+=b;                }                a=b;           }           printf( "%d\n",ans );               }    return 0;}

But it's still not fast to write it out... it's fast !!

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.