CodeForces-849A Odds and Ends

来源:互联网 发布:腾讯微信数据报告 编辑:程序博客网 时间:2024/05/21 18:44

 A. Odds and Ends

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output

standard output


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.


第一次打CodeForces有点小激动,但是结果不尽如人意,掉了一百多分。
第一次打,背到家Orz,写了两次,就被同一个房间里面的大佬Hack了两次。
第一次打也知道了CF怎么用,怎么Hack别人。
系统会把参赛人员先分到若干个房间,同一个房间内的人员才可以相互Hack。
当你提交代码以后通过了样例(这里说AC不太严谨)以后,可以考虑把问题锁住。
锁住了代码以后就不能再提交,也就是说你确定自己的代码没问题了。
如果没锁住就可以在别人Hack你的代码以后继续提交。
锁住以后就可以去房间里面去看别人的代码,如果发现错误,就可以想一组测试数据把别人的代码漏洞显露出来。


回归正题把,这道题目的做法。
CodeForces上面的题目真的很锻炼思维能力。


首先题目的意思是,给你一个长度为N的数字串,问你这个数字串是否可以被分割为奇数个小的数字串,
小的数字串要满足1这个数字串的头和尾都是奇数2这个数字串的长度为奇数
题目最下面有给样例的解释,可以配合看,更容易理解。


首先,比较明显的两个结论。
1如果第一个数字是偶数或者最后一个数字是偶数,这时候一定不会符合条件,这时候输出No。(样例3就是这样)
2如果长度为N的数字串的头和尾都是奇数,长度N也是奇数,这时候一定符合条件(不分割就可以符合条件),输出Yes
3比赛的时候考虑就在考虑这种情况,头和尾是奇数,长度N是偶数的情况。

一开始想找数字串中连续的相邻的两个奇数,有这样奇数存在才会存在分割的情况。
我想着是遍历一遍,找到这样的奇数对,判断一下他和前面一个奇数对之间的长度。这样遇到就分割。
最后在处理整体小数字串的个数这个条件。需要合并小数字串


这样写的很复杂,虽然样例过了,但是最后被人找到了漏洞,~~~~(>_<)~~~~。


这道题关键的地方就是我上面说的第三种情况。
其实如果我把结论告诉你,就会恍然大悟,但做的时候这么也没想到(还是太弱了 ╮(╯▽╰)╭ )
题目说的是奇数个小数字串,数字串的长度也是奇数,那么满足这两个条件的大数字串不可能是偶数
所以说上面第三种情况直接输出No

奇数个奇数长度的数字总长度一定是奇数。(奇数*奇数=奇数)


#include<iostream>#include<cstdio>using namespace std;int n;int a[105];int main(){int i;while(scanf("%d",&n)!=EOF){for( i=1;i<=n;i++)scanf("%d",&a[i]);if(n%2==0||a[1]%2==0||a[n]%2==0)//与下面对立的情况printf("No\n");else //头和尾是奇数且N是奇数printf("Yes\n");}return 0;}

#include<iostream>#include<cstdio>using namespace std;int n;int a[105];int main(){int i;while(scanf("%d",&n)!=EOF){for( i=1;i<=n;i++)scanf("%d",&a[i]);if(n%2==0)//N是偶数printf("No\n");else if((a[1]%2)&&(a[n]%2))//头和尾是奇数且N是奇数printf("Yes\n");else//头或尾一个是偶数printf("No\n");}return 0;}




原创粉丝点击