问题 F: Antiarithmetic?
时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 9
[ 提交][ 状态][ 讨论版]
题目描述
A permutation of n is a bijective function of the initial n natural numbers: 0, 1, ... n-1. A permutation p is called antiarithmetic if there is no subsequence of it forming an arithmetic progression of length bigger than 2, i.e. there are no three indices 0 ≤ i < j < k < n such that (pi , pj , pk) forms an arithmetic progression.
For example, the sequence (2, 0, 1, 4, 3) is an antiarithmetic permutation of 5. The sequence (0, 5, 4, 3, 1, 2) is not an antiarithmetic permutation as its first, fifth and sixth term (0, 1, 2) form an arithmetic progression; and so do its second, forth and fifth term (5, 3, 1).
Your task is to check whether a given permutation of n is antiarithmetic.
输入
There are several test cases, followed by a line containing 0. Each test case is a line of the input file containing a natural number 3 ≤ n ≤ 10000 followed by a colon and then followed by n distinct numbers separated by whitespace. All n numbers are natural numbers smaller than n.
输出
For each test case output one line with yes or no stating whether the permutation is antiarithmetic or not.
样例输入
3: 0 2 1
5: 2 0 1 3 4
6: 2 4 3 5 0 1
0
样例输出
yes
no
yes
看了好多大佬都是考虑等差中项和前项,来算后项,只考虑的递增,不考虑递减,搞不明白。所以我先考虑了前后两项,保证中项在前两项之间,就哦了。当然有可能算式中的中项是小数,所以我用了double o 和 int t,判断o-t?=0,来保证中项为整数。
#include<iostream>
#include<cstring>
using namespace std;
int n;
int x;
char m;
int a[10001];
double o;//zhongxiang
int l;
int ppp(void){
for(int i=0;i<n;i++){
for(int j=n-1;j>i;j--){
o=(i+j)/2.0;
l=(i+j)/2;
if(o-l!=0)continue;
if(a[i]<a[l] && a[l]<a[j]){
return 1;
}
}
}
return -1;
}
int main(){
while(cin>>n && n){
cin>>m;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
cin>>x;
a[x]=i;
}
if(ppp()==1){
cout<<"no"<<endl;
}
else {
cout<<"yes"<<endl;
}
}
}