[Programming thinking and practice of CSP-M1 B] Cuckoo East want to eat

Mention entry describes:

Cuckoo East going to eat fried every day for a period of n days of exam week, his first day i need to buy A i a fried. Fried shops in order to stimulate consumption, provides two ways to buy: 1, a one-time buy two fried. 2, buy a fried today, but for tomorrow to buy a fried, the store will give a ticket, the next day to get coupons to come and collect. And do not provide other ways to buy. Two kinds of ways to buy can be used many times. At the end of exam week, cuckoo East do not want to have extra tickets and do not waste any of a ticket, I asked if he could buy a day i a fried.

Input formats:

Two input lines, the first line of a positive integer n (1 <= n <= 100000), indicates the number of test Sunday.
The second row has the number n, the i-th A i (0 <= A i <= 10000) represents the number of i-th day to buy fried.

Output formats:

If you meet the requirements, the output of "YES", otherwise outputs "NO" (output without the quotation marks).

Sample input 1:

4
1 2 1 2

Sample output 1:

YES

Sample input 2:

3
1 0 1

Sample Output 2:

NO

Ideas:

To meet every day to buy a i a fried, and not waste any of a ticket. Therefore, it should be minimal use of the second purchase, so choose to make use of the first ways to buy every day, if the last remaining one was using the second purchase, and put a ticket to leave the next day, the next day priority for a day left before the coupons, not after the rest of the purchase amount and then using the same strategy, that is, as much as possible using the first purchase, if the remaining purchase only use the second way. If the half-way ticket to leave one day and the next day after purchase amount is 0, or n days still remaining coupons, you can not meet the demand.

Code:

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	int n;
	cin>>n;
	int temp=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		if(a==0&&temp!=0)
			break;
		temp=(a-temp)%2;
	}
	if(temp==0)
		cout<<"YES"<<endl;
	else 
		cout<<"NO"<<endl;
	
	return 0;
}
Published 25 original articles · won praise 8 · views 538

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104871060