您的位置:首页 > 其它

UVa 10730 Antiarithmetic? (想法题)

2013-09-20 13:27 477 查看


10730 - Antiarithmetic?

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1671

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.

Sample input

3: 0 2 1 
5: 2 0 1 3 4
6: 2 4 3 5 0 1
0

Output for sample input

yes
no
yes


思路:

由于是全排列,我们可以先做下预处理,即标记每个数的位置。

然后对从公差j=1开始扫描序列,判断数字的相对位置即可。

复杂度:O(N^2)

完整代码:

/*0.019s*/

#include<cstdio>

int num[10010], n;

inline bool check()
{
	for (int i = 0; i < n; ++i)
		for (int j = 1; i + (j << 1) < n; ++j)
			if (num[i] < num[i + j] && num[i + j] < num[i + (j << 1)])
				return false;
	return true;
}

int main(void)
{
	int a;
	while (scanf("%d", &n), n)
	{
		getchar();
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &a);
			num[a] = i;
		}
		puts(check() ? "yes" : "no");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: