Week4 CSP-M1 B - East cuckoo want to eat

Subject 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 ai need to buy a fried every day. But fried shops in order to stimulate consumption, there are only two ways to buy: ① one day at a one-time buy two fried ② buy a fried today, but for tomorrow to buy a fried, the store will give a ticket, the next day tickets to come and collect.

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 the end of the exam, the hands of a ticket at the end of the examination is not allowed. 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 a fried ai day exam week.

It should be added that Italy: as long as the hands of a ticket bought yesterday, today it is necessary, otherwise it is a failure; the last day of the second approach can no longer buy fried.

Ideas:

① to only buy a ticket, there is no need to buy more coupons 

② day i, when ai is an odd number, if the need to buy tickets, just buy a ticket; if vouchers, coupons not buy

③ day i, when ai is an even number, if coupons, to buy a ticket, voucher if not, do not buy tickets

Correctness of ideas:

Why the above ideas are correct? That is why there is no need to buy more coupons?

Suppose I bought a ticket of m, m> 2, then m = 2k + or m = 2k 1 =;

When m = 2k, the next day can buy two fried with k times, and buy tickets unnecessary adventure today

When m = 2k + 1, only one ticket is required to buy the rest of 2k voucher, you can buy the next day with two fried k times, and buy tickets unnecessary adventure today

to sum up:

Again, on the first issue, abstraction, analysis, do it; it seems a little greedy feeling

It seems to prove that the above is the use of "results not get greedy than the optimal Jiecha "

Code:

. 1 #include <cstdio>
 2 #include <the iostream>
 . 3  the using  namespace STD;
 . 4  int main ()
 . 5  {
 . 6      int Last = 0 ;   // beginning no coupons 
. 7      int n-; CIN >> n-;
 . 8      for ( int I = . 1 ; <= n-I; I ++ )
 . 9      {
 10          int T; Scanf ( " % D " , & T);
 . 11          IF (& T . 1 )     // SALE odd fried 
12         {
13             if(last==1) last=0;
14             else if(last==0) last=1;
15         }
16         else if(t==0&&last==1)   //要买零个生煎
17         {
18             cout<<"NO"<<endl;
19             return 0; 
20         }
21         else if(last==1) last=1;     // buy fried even number of 
22 is      }
 23 is      IF (Last == . 1 ) COUT << " NO " << endl;
 24      the else COUT << " YES " << endl;
 25      return  0 ;
 26 is }

 

Guess you like

Origin www.cnblogs.com/qingoba/p/12511252.html