CF 849A Odds and Ends

Odds and Ends

题目

Where do odds begin, and where do they end? Where does hope emerge, and will they ever break?

Given an integer sequence a1, a2, …, an of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers.

A subsegment is a contiguous slice of the whole sequence. For example, {3, 4, 5} and {1} are subsegments of sequence {1, 2, 3, 4, 5, 6}, while {1, 2, 4} and {7} are not.

Input
The first line of input contains a non-negative integer n (1 ≤ n ≤ 100) — the length of the sequence.

The second line contains n space-separated non-negative integers a1, a2, …, an (0 ≤ ai ≤ 100) — the elements of the sequence.

Output
Output “Yes” if it’s possible to fulfill the requirements, and “No” otherwise.

You can output each letter in any case (upper or lower).

Examples
Input
3
1 3 5
Output
Yes
Input
5
1 0 1 5 1
Output
Yes
Input
3
4 3 1
Output
No
Input
4
3 9 9 3
Output
No
Note
In the first example, divide the sequence into 1 subsegment: {1, 3, 5} and the requirements will be met.

In the second example, divide the sequence into 3 subsegments: {1, 0, 1}, {5}, {1}.

In the third example, one of the subsegments must start with 4 which is an even number, thus the requirements cannot be met.

In the fourth example, the sequence can be divided into 2 subsegments: {3, 9, 9}, {3}, but this is not a valid solution because 2 is an even number.

解释

对题目理解的不够透彻,
我原本写的就是模拟,从前往后进行遍历,是奇数个数而且前后面的数是奇数就让计算集合数量的计数器+1,看能不能弄完整个数组和查看集合数量是不是奇数
但是,…事实上只需要看第一个,最后一个数,以及n就可以了,只要这三个数都是奇数,就可以打印yes了

代码1

#include <stdio.h>
int cb[1000];
int main()
{
    int t;
    while(~scanf("%d",&t)){
    	int n,m,a,b;
	    for(int i=0 ; i<t ; i++){
	    	scanf("%d",&cb[i]);
		}
		if(cb[0]%2==0 || cb[t-1]%2==0){
			printf("No\n");
		}else if(t%2 == 1){
			printf("Yes\n");
		}else{
			int cnt = 0;
			int l = 0,r = 0;
			int no = 0;
			for(int i=0 ; i<t ; i++){
				r = i;
				if((r-l)%2 == 1 && cb[r]%2==1 && cb[l]%2==1){
					cnt++;
					l = r ;
				}
				if(r == t-1 && l == r+1){
					cnt++;
				}else if(r == t-1){
					no = 1;
				}
			}
	
			if(no == 0 && cnt%2 == 1){
				printf("Yes\n");
			}else{
				printf("No\n");
			}
		}
	}
    
}

代码2

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

typedef long long ll;

int a[1005];
int num[1005];
int main()
{
    int n;scanf("%d",&n);
    int cnt = 0;
    for(int i = 1;i <= n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i] % 2 == 1)
            num[++cnt] = i;
    }
    if(a[1] % 2 == 1 && a[n] % 2 == 1 && n % 2 == 1)
    {
        printf("Yes\n");
        return 0;
    }
    else
        printf("No\n");
    return 0;
}

代码2来源: https://blog.csdn.net/tomjobs/article/details/100017956?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160231679419724839215588%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160231679419724839215588&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v3~pc_rank_v2-15-100017956.first_rank_ecpm_v3_pc_rank_v2&utm_term=Odds+and+Ends+&spm=1018.2118.3001.4187.

猜你喜欢

转载自blog.csdn.net/Dueser/article/details/108999086
cf