[递归]UVA11129 An antiarithmetic permutation
来源:互联网 发布:c语言初始化程序 编辑:程序博客网 时间:2024/05/22 09:50
Problem A: An antiarithmetic permutation
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 < nsuch 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 eachn from input, produce one line of output containing an (any will do) antiarithmetic permutation of n in the format shown below.
Sample input
3560
Output for sample input
3: 0 2 1 5: 2 0 1 4 36: 2 4 3 5 0 1
W. Guzicki, adapted by P. Rudnicki
一开始题目就理解错了,各种WA,后来参考网上的大神的思路。。
题意:给定一个包含了0到n - 1的序列。。要使得这个序列中每个长度大于2的子序列都不是等差数列。。
思路:对于一个等差的序列。如0 1 2 3 4 5 我们可以这样做,把他分离成2部分等差子序列0 2 4和1 3 5然后组合成一个新的序列0 2 4 1 3 5。这样做的话,可以保证前半部分无法和后半部分组合成等差数列。可以证明,一个序列的等差是k,首项为a1,序列为,a1, a1 +k, a1 + 2k, a1 + 3k .... a1 + (n - 1)k.分成的两部分为a1, a1 + 2k , a1 + 4k ....、 a1, a1 + k, a1 + 3k, a1 + 5k...如此一来,任意拿前面和后面组成序列的话。后面和后面的差都是2k的倍数,前面和后面的都是2k + 1的倍数。这样是成不了等差的。。 如此一来。我们只要把序列一直变换,变换到个数小于等于2即可。
#include<iostream>#include<cstring>using namespace std;int arry[10005],bin[10005];int n;void solve(int l,int r){if(r==l) return;memcpy(bin,arry,sizeof(arry));int k=l;for(int i=l;i<=r;i+=2,k++)arry[k]=bin[i];for(int i=l+1;i<=r;i+=2,k++)arry[k]=bin[i];solve(l,(l+r)/2);solve((l+r)/2+1,r);}int main(){while(cin>>n&&n){int i;for(i=0;i<n;i++)arry[i]=i;solve(0,n-1);cout<<n<<":";for(i=0;i<n;i++)cout<<" "<<arry[i];cout<<endl;}return 0;}
- [递归]UVA11129 An antiarithmetic permutation
- Uva11129-An Antiarithmetic Permutation
- 【个人训练】(UVa11129)An antiarithmetic permutation
- uva11129 - An antiarithmetic permutation(反算数级数)
- uva11129 - An antiarithmetic permutation(不等差序列,分治法)
- uva 11129 - An antiarithmetic permutation(递归)
- uva 11129 An antiarithmetic permutation (递归)
- 11129 - An antiarithmetic permutation
- 11129 - An antiarithmetic permutation
- UVaOJ11129 - An antiarithmetic permutation
- UVA 11129 An antiarithmetic permutation 非等差数列 贪心 递归
- UVa 11129 - An antiarithmetic permutation
- UVa 11129 - An antiarithmetic permutation
- uva 11129 - An antiarithmetic permutation
- UVA 11129 - An antiarithmetic permutation
- UVA 11129 An antiarithmetic permutation
- uva 11129 - An antiarithmetic permutation
- uva 11129 An antiarithmetic permutation
- 信息检索作业
- 每天一个小程序(15)——交换排序之快速排序
- SSIS Package Development - Issues and Solutions
- Ubuntu12.10下安装lighttpd1.4.32详细步骤
- Ipad air里面的照片能恢复吗
- [递归]UVA11129 An antiarithmetic permutation
- java中内存泄露
- JS调用后台函数
- 使用AGSJSONRequestOperation完成webservice资源的请求
- 推荐一个文献管理工具--Paperbox
- JQuery资料
- 关于malloc函数
- 浅谈协方差矩阵
- U盘卡机文件损坏怎么办?