Eat fried: ask whether the number exactly. c ++

Eat fried issue

Bob decided to eat fried n days, you need to buy a fried ai every day. Fried shops are only two ways to buy: 1, a one-time in one day to buy two fried; 2, buy a fried today, the store gave a ticket, vouchers to change tomorrow with a fried. Not the rest of the purchase, the purchase of these two ways can be used many times, Xiao Ming does not allow tickets to be wasted, not allowed n the hands of days have coupons. Can I ask just to buy a fried ai day in n day.

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

If you meet the needs Xiaoming, output "YES", if not met, output "NO".

sample input:
4
1 2 1 2

sample output:
YES

(Programs exist: the first day of option two, a day to eat; the next option two, plus the first day of the ticket, the next day to eat two; the third day of the second day with coupons, the first eat for three days a; options for a fourth day, the fourth day to eat two)

sample input:
3
1 0 1

sample output:
NO

(First day of the two alternatives, will leave a ticket; the next day on the ticket is useless, so wrong)

Test point scale:
data points 1-2: n upper limit AI 10 10
data points 3-5: n 1000 ai upper limit 10
data points 6-7: n upper limit AI 10 10 000
data points 8-10: n limit 100000 ai the upper limit of 10,000
each test point 1000ms 262144kb

Ideas:

  • For the first way, every time to buy two, that is, if the first day i want to buy is even, then a number of options can be used directly
  • If this number is odd day fried, then a first, a remaining two fried scheme for a plurality of programs for later use, a voucher to be spent on the remaining i + 1 Angel
  • So get to determine the conditions, if the number of fried one day need to purchase less than zero (description given vouchers to purchase more than the number today, tickets will waste), indicating that buying the wrong way, false output
  • Bool type may be utilized for a determination, if the cycle through all days is TRUE, then the YES output; otherwise, output NO.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath> 
//#include<iostream>
using namespace std;
int a[111111]={0};//保证最后一位 
int main()
{
	//ios::sync_with_stdio(false);
	int n=0;
	bool flag=true;
	//cin>>n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		//cin>>a[i];
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++)
	{
		if(a[i]%2==1)//偶数直接跳过这一天,奇数思考和第二天的关系
		{
			a[i+1]--; 
		} 
		if(a[i+1]<0) 
                 {flag=false;break;}
	}
	if(flag==false)printf("NO"); 
	//"cout<<"NO";
	else if(flag==true)printf("YES");
	//cout<<"YES";
	return 0;
}
Published 29 original articles · won praise 1 · views 946

Guess you like

Origin blog.csdn.net/qq_44654498/article/details/104902529