UVa 10730 Antiarithmetic? (想法题)

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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值