Original title:
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 are no subsequence of it forming an arithmetic progression of length Bigg Er than 2, i.e. there is no three indices 0≤i < J < K < n such that (P I, P J, p K) forms an arithmetic progr Ession. For example, the sequence (2, 0, 1, 4, 3) are an antiarithmetic permutation of 5. The sequence (0, 5, 4, 3, 1, 2) is a antiarithmetic permutation of 6 as its first, 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 an antiarithmetic permutation of N.
Input
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.
Output
For each n from input, produce one line of output containing a (any would do) antiarithmetic permutation
of n in the format shown below.
Sample Input
3
5
6
0
Sample Output
3:0 2 1
5:2 0 1) 4 3
6:2 4 3 5 0 1
English:
We define an arrangement of N (permutation) that arranges the 0,1,2,......, n-1 in any order. For example: N=5, then (0,1,2,4,3) is an arrangement, and (2,0,1,4,3) is another arrangement. A permutation p if no subsequence of length greater than 2 is found, and this subsequence is arithmetical progression, it is referred to as "inverse operation (antiarithmetic)". That is, there is no three sequence indicator I, J, K (0 <= I < J < K < N) makes (pi, PJ, PK) a arithmetical progression.
For example: Sequence (0,5,4,3,1,2) is an arrangement of n=6, and this arrangement is not "inverse". Since (0,1,2) this sub-sequence forms one arithmetical progression, in addition (5,4,3) forms another arithmetical progression.
However, (2,0,1,4,3) is the "inverse" permutation of n=5 because no subsequence of length greater than 2 is found, and the subsequence is arithmetical progression.
Give you n, your task is to produce an n "inverse" permutation (if more than one, that any one can).
#include <bits/stdc++.h>
using namespace std;
int n,num[10005],mark[10005];
void Divide (int s,int e)
{
if (e-s<2)
return;
for (int i=s;i<=e;i++)
mark[i]=num[i];
int j=s;
for (int i=s;i<=e;i+=2)
num[j++]=mark[i];
for (int i=s+1;i<=e;i+=2)
num[j++]=mark[i];
Divide (S, (s+e) >>1);
Divide (((s+e) >>1) +1,e);
}
int main ()
{
Ios::sync_with_stdio (false);
int n;
while (Cin>>n,n)
{for
(int i=0;i<n;i++)
num[i]=i;
Divide (0,n-1);
cout<<n<< ":";
for (int i=0;i<n;i++)
cout<< "" <<num[i];
cout<<endl;
}
return 0;
}
Answer:
Very good division of the topic. But I didn't think of it. -_-!!! Refer to other people's blog ideas and code.
Want to destroy a arithmetic progression, you can put the subscript is odd to the subscript is an even number behind, so that the formation of two arithmetic progression, but the whole sequence is not linear, and then the two arithmetic progression repeated use of the above rules, can completely disrupt the linear.
0 1 2 3 4 5 6 7
(0 2 4 8) (1 3 5 7) Now we've got two little arithmetic progression, but the sequence is not arithmetic progression.
, (0 4) (2 8) (1 5) (3 7) = = Because the number of series is less than 3, so the small series can not be judged whether it is a linear, but now the sequence has satisfied the requirements, that is, the sub-sequence is not arithmetic progression.
From http://www.cnblogs.com/byzsxloli/p/5408483.html