UVa 10730 Antiarithmetic? (想法题)
2013-09-20 13:27
477 查看
10730 - Antiarithmetic?
Time limit: 3.000 secondshttp://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; }
相关文章推荐
- UVA 10730 - Antiarithmetic?
- uva - 10730 - Antiarithmetic?(暴力枚举)
- UVA 10730 - Antiarithmetic?(技巧枚举)
- uva 10730 - Antiarithmetic?
- UVA 10730 Antiarithmetic?
- uva - 10730 - Antiarithmetic?(暴力枚举)
- uva 10730 - Antiarithmetic?
- uva 10730 - Antiarithmetic?(枚举)
- UVa 278 Chess (想法题)
- uva 101 - The Blocks Problem 练习代码控制能力的题目吧 ,思路转化为代码,实现自己的比较复杂的想法
- UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想法题&优先队列)
- UVa -- 10635 Prince and Princess 【想法】
- UVA 6862 Triples 想法题
- UVa 12400 - 3, 2, 1, 0 (数学想法题&高精度 or 打表)
- 10730 - Antiarithmetic?(水题)
- UVALive5052 Genome Evolution(想法题)
- World Finals 2004 / UVa 1030 Image Is Everything (想法题&迭代更新)
- UVa 11489 Integer Game (博弈&想法题)
- UVa 10025 The ? 1 ? 2 ? ... ? n = k problem (数学&想法题&常数算法)
- UVa 10152 ShellSort (想法题)