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.
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 "Yes" if it's possible to fulfill the requirements, and "No" otherwise.
You can output each letter in any case (upper or lower).
3
1 3 5
Yes
5
1 0 1 5 1
Yes
3
4 3 1
No
4
3 9 9 3
No
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.
拆分序列,序列个数不能为偶数,每个子序元素个数不能为偶数,每个子序列不能以偶数开头或结尾。
特判开头结尾,判断n即可。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 1044266558 #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int a[105],n; int main() { scanf("%d",&n); a[0]=0; int ans=0; bool flag=true; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if((i==n && a[i]%2==0) ||(i==1 && a[i]%2==0)) flag=false; if(a[i]&1) ans++; else { if(a[i-1]&1) a[i]=1,ans++; } } if(ans%2==0) flag=false; puts(flag?"Yes":"No"); return 0;
}
Connect the countless points with lines, till we reach the faraway yonder.
There are n points on a coordinate plane, the i-th of which being (i, yi).
Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.
The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise.
You can print each letter in any case (upper or lower).
5
7 5 8 6 9
Yes
5
-1 -2 0 0 -5
No
5
5 4 3 2 1
No
5
1000000000 0 0 0 0
Yes
In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.
In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.
In the third example, it's impossible to satisfy both requirements at the same time.
找两条斜率相等地直线,使得这些点全在直线上。并且每条直线上至少包含一个点。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 1044266558 #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int a[1006],n,vis[1005]; double k1,k2; int main() { scanf("%d",&n); bool flag=false; int pos=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]-0==0) pos++; } if(pos==n-1) flag=true; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); vis[1]=1,vis[i]=1; k1=((a[i]-a[1])*(1.0))/((i-1)*1.0); int ans=1; for(int j=2;j<=n;j++) { if(((a[j]-a[1])*(1.0))/((j-1)*1.0)==k1) vis[j]=1,ans++; } int ok=0,xx=-1; for(int j=1;j<=n;j++) { if(vis[j]==0 && ok==0) xx=j,ok++; else if(ok==1 && vis[j]==0) k2=((a[j]-a[xx])*(1.0))/((j-xx)*1.0),ok++; else if(ok>=2 && vis[j]==0) { if(((a[j]-a[xx])*(1.0))/((j-xx)*1.0)==k2) ok++; } } if((ans+ok==n && ok && k1==k2) || ans==n-1 || ok==n-1) flag=true; if(ans==n || ok==n) { flag=false; goto k; } } k:puts(flag?"Yes":"No"); return 0; }