UVa 11129:an antiarithmetic permutation

Source: Internet
Author: User
Tags arithmetic printf

Link

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

"Original question"

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 isn't no subsequence of it forming an arithmetic progression of length Bigg Er than 2, i.e. there are no three indices 0≤i < J < K < n such that (pi, PJ, pk) forms a arithmetic Progressi On.

For example, the sequence (2, 0, 1, 4, 3) are an antiarithmetic permutation of 5. The sequence (0, 5, 4, 3, 1, 2) is not a antiarithmetic permutation of 6 as its A, fifth and sixth term (0, 1, 2) for m an arithmetic progression; And so does its second, fourth and fifth term (5, 3, 1).

Your task is to generate a 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 all n from input, produce one line of output containing of antiarithmetic permutation of N in the forma T 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

"The main effect of the topic"

Enter N, and then by 0, 1, ... N-1 consists of a sequence of antiarithmetic, which cannot have a linear sequence of 3 elements.

"Analysis and Summary"

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

This problem has been thought for a long time or no idea. So Baidu study. The original is the use of the method of partition, which has always been my weaker place.

Code

* 
 * * Uva:11129-an antiarithmetic permutation * time:0.012s * author:d_double * * * * 
 and
     
#inc lude<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;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.