UVa 11129:An antiarithmetic permutation

【链接】

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2070

【原题】

A permutation of n+1 is a bijective function of the initial n+1 natural numbers: 0, 1, ... n. 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 of 6 as its first, fifth and sixth term (0, 1, 2) form an arithmetic progression; and so do its second, fourth and fifth term (5, 3, 1).

Your task is to generate an antiarithmetic permutation of n.

Each line of the input file contains a natural number 3 ≤ n ≤ 10000. The last line of input contains 0 marking the end of input. For each n from input, produce one line of output containing an (any will do) antiarithmetic permutation of n in the format shown below.

Sample input

3
5
6
0

Output for sample input

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

【题目大意】

输入N, 然后要由0,1,...N-1组成一个antiarithmetic序列, 这个序列不能有3个元素的等差子序列。

【分析与总结】

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

这题想了很久还是没思路。于是百度学习之。原来是运用了分治的办法,这一直是我比较弱的地方。

【代码】

/*
 * UVa: 11129 - An antiarithmetic permutation
 * 递归与分治
 * Time: 0.012s
 * Author: D_Double
 */

#include<cstdio>
int init[10003],tar[10003],T=10000;  

void ST(int l,int h)
{
    int t=l;
    if(l==h) return;
    for(int i=l;i<=h;i+=2)
        tar[t++]=init[i];
    for(int i=l+1;i<=h;i+=2)
        tar[t++]=init[i];
    for(int i=l;i<=h;i++)
        init[i]=tar[i];
    ST(l,(l+h)/2);
    ST((l+h)/2+1,h);
}  

int main()
{
    while(~scanf("%d",&T)&T){
        for(int i=0;i<T;i++)
           init[i]=i;
        ST(0,T-1);
        printf("%d: %d",T,tar[0]);
        for(int i=1;i<T;i++)
            printf(" %d",tar[i]);
        printf("\n");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, input
, 分治法
, 分治递归
, 序列
, of
an
permutation、next permutation、next permutation函数、permutation test、permutation matrix,以便于您获取更多的相关知识。

时间: 2024-09-21 16:33:39

UVa 11129:An antiarithmetic permutation的相关文章

uva 11129 - An antiarithmetic permutation

点击打开链接uva 11129 题目意思: 给定一个初始序列为0 1 2 3 .....n-1;要求找到一个序列能够满足所有的子序列都不能够形成等差序列 解题思路: 1思路:递归+分治 2分析:网上那些神牛是这么说的:把这个序列按照奇偶位置分开成两个序列,然后再对两个序列进行奇偶位置分开......最后得到的数组就是所要的ans,为什么这样可以呢,因为在奇数位置内和偶数位置内等差为2,而两个序列之间为1,所以这样肯定不会构成等差序列. 代码: #include <algorithm> #inc

UVa 103:Stacking Boxes

[题目链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39 [原题] Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but

UVa 11198:Dancing Digits,Rujia Liu的神题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2139 类型: 隐式图搜索,  BFS, 哈希判重,模拟 原题: Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand in a line to have a wonderful

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di