Week4 CSP B

Problem Description

Cuckoo East exam week, and a total of n day test on Monday. He did not want to exam week so tired, I intended to have a nice dinner every day. He decided to eat fried every day, cuckoo East aia_ia i need to buy a fried every day. But fried shops in order to stimulate consumption, there are only two ways to buy: ① one-time buy two fried one day. ② buy a fried today, but for tomorrow to buy a fried, the store will give a ticket, the next day to come and collect tickets. Not the rest of the purchase, the purchase of these two ways can be used many times, but the cuckoo East is a thrifty boy, he left end of the training, the hands of a ticket is not allowed at the end of training. Cuckoo East very rich, you do not need to worry about money East cuckoo, cuckoo East but stupid, he just wanted to ask you whether he could buy aia_ia day exam week A fried.

Input

Two input lines, a first line of the input integer n (1 <= n <= 100000), the number of days of examination weeks, the second row has the number n, the i-th ai (0 <= ai <= 10000) i represents the number of days to buy fried East cuckoo.

Output
If you meet the cuckoo East strange request, output "YES", if not met, output "NO". (Output without quotation marks)

INPUT1 the Sample
. 4
. 1 2. 1 2
. 1
2
the Sample OUTPUT1
YES
. 1
the Sample INPUT2
. 3
. 1 0. 1
. 1
2
the Sample Output2
NO
. 1
solving ideas

Because only two strategies, or buy a buy two plus a ticket tomorrow, even if unlimited number of times, we can still be converted to 0,1,2 problems after such a simplification. After recursively obtained according to the situation the day after the first day of the case, until there are no eligible situation.

#include <the iostream>
 the using  namespace STD;
     / * AI per day 
    later time two; a + a tomorrow Quan 
     
     
    * / 
    
    // from the last to determine the 3 + 2 = 1 2 --- 1-- error once bought day before bought a ticket 1 + 1 
     //    previous day 1 + 1 (the day after treatment as do) 2 ---- 2 or 1 + 1 + 1 1-- 1
             //    == 1 and then one day for the program before 1 again one day before the last day as        
             //      one day before and then == 2 is a scheme as the previous day 2 and then one day before 
int main () {
     int n-;    
     the while (CIN >> n-) {
     // CIN n->>;      
    int * = Ar new new  int [n-];
     int * Plan = new new  int[n];
    bool  can=true; 
    
    for(int i=0;i<n;i++)   plan[i]=0;//0 没买 ;1  1+1 ; 2  2                  
    
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
        while(ar[i]>3)
        ar[i]-=2;
    }
    if(ar[0]==3)
        ar[0]=1;        
    plan[0]=ar[0];    
    for(int i=1;i<n-1;i++){
        //0   1  2  3
        if(plan[i-1]==0||plan[i-1]==2){
            if(ar[i]==3||ar[i]==1)
            {
                plan[i]=1; 
            }
            else  
            plan[i]=ar[i];
            
        }
        else if(plan[i-1]==1){
             if(ar[i]==0) 
             {
                 can=false;
                 break;
             }             
             else 
               plan[i]=ar[i]-1;   
        }    
    }
    if(can){ 
    if(plan[n-2]==0||plan[n-2]==2)
    {
        if(ar[n-1]!=2 && ar[n-1]!=0)
        {
            can=false;
        }
        //else plan[n-1]=2;
     }
    if(plan[n-2]==1){
        if(ar[n-1]!=3 && ar[n-1]!=1)
         can  =  false;
       }
       //else plan[n-1]=
    
    }
//    for(int i=0;i<n-1;i++)
//    cout<<plan[i]<<" ";
//    cout<<2<<endl;

    if(can)  {
    cout<<"YES"<<endl;   
    }
    else 
    cout<<"NO"<<endl;    
 }    
    return 0;
}

/*
    1   3    3    2   1
    1+1 1+2  2+1  1+1 1+0   YES
    
    1   3    2    1    2
    1+1 1+2  2    1+1  1+1?错误 
    
    1   3     2    2    2 
    
    1 3 2 1 1 1
    1   4       3      5   9   8   
    1+1 1+2+1   1+2    4+1 1+8 8
    
    
     
 */

 

Guess you like

Origin www.cnblogs.com/liuzhuan-xingyun/p/12529507.html