UVA - 10730
题目介绍:
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.
大意:
在n个数中能不能找出长度大于2的等差数列。枚举3个10000根据感觉不行。知道两个数,第三个数即可算出来。因此只要枚举2个数,判断算出的第三个数在不在第二个数之后。要判断是否在后面可以用图的方法。在最开始给定在i位置的数可以到达j处(j>i).
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
int Link[10005][10005];
int main(){
string que;
while(cin>>que&&que!="0"){
int n=0;
for(int i=0;que[i]!=':';i++)
if(isdigit(que[i]))
n=n*10+que[i]-'0';
int num[10005];
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
memset(Link,0,sizeof(Link));
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
Link[num[i]][num[j]]=1;
int ok=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int c=num[j]+num[j]-num[i];
if(c>=0&&c<n&&Link[num[j]][c]){
ok=1;
break;
}
}
if(ok)break;
}
printf("%s\n", ok==1 ? "no" : "yes");
}
return 0;
}