首页 > 其他分享 >UVa 11129 An antiarithmetic permutation (构造题&想法题&分治)

UVa 11129 An antiarithmetic permutation (构造题&想法题&分治)

时间:2023-04-12 13:05:57浏览次数:47  
标签:progression int antiarithmetic permutation input 序列 11129


11129 - An antiarithmetic permutation


Time limit: 3.000 seconds


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 permutationp 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 (pipjpk) 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



UVa 10730 的逆问题。

思路:

先把序列分成奇偶序列(把奇在位置上的数分成一组,在偶位置上的数分成一组),这两个序列之间不会形成等差序列。同样再在奇偶序列中再这样分,这样分就能保证不会出现等差序列。


完整代码:

/*0.012s*/

#include<cstdio>

int n, num[10010], tempa[5010], tempb[5010];

void divide(int l, int r)
{
	if (l == r) return;
	int a = 0, b = 0, aa = 0, bb = 0, i;
	for (i = l; i <= r; i += 2)     tempa[a++] = num[i];
	for (i = l + 1; i <= r; i += 2) tempb[b++] = num[i];
	for (i = l; i < l + a; ++i)     num[i] = tempa[aa++];
	for (; i <= r; ++i)             num[i] = tempb[bb++];
	divide(l, l + a - 1); divide(l + a, r);
}

int main(void)
{
	while (scanf("%d", &n), n)
	{
		for (int i = 0; i < n; ++i)
			num[i] = i;
		divide(0, n - 1);
		printf("%d:", n);
		for (int i = 0; i < n; ++i)
			printf(" %d", num[i]);
		putchar('\n');
	}
	return 0;
}


标签:progression,int,antiarithmetic,permutation,input,序列,11129
From: https://blog.51cto.com/u_5535544/6185480

相关文章

  • CF698F Coprime Permutation 题解
    题意给定一个未填满的数组\(p\),求有多少种\(1\simn\)的排列\(p\)满足对于任意\(i<j\),都有\([\gcd(i,j)=1]=[\gcd(p_i,p_j)=1]\),答案对\(10^9+7\)取模。题解部分参考这篇题解(感觉这篇题解应该是目前为止最详细的吧)。记\(P\)为\([1,n]\)中所有素数与\(1\)构成......
  • CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)- Make It Permutation
    题目链接:Problem-C-Codeforces  #include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;#defineendl"\n"intmain(){ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);intT=1;cin>>T;while(......
  • CF1295E Permutation Separation 题解 线段树优化dp
    题目链接:https://codeforces.com/problemset/problem/1295/E题目大意:将排列\(p_1,p_2,\ldots,p_n\)先分成\(p_1,\ldots,p_k\)与\(p_{k+1},\ldots,p_n\)两个......
  • Permutation Game
    #include<iostream>usingnamespacestd;constintN=5e5+10;intn;inta[N];voidsolve(){scanf("%d",&n);intcnt1=0,cnt2=0,cnt3=0;for(inti......
  • D. XOR Permutations
    D.XORPermutations注意多次输入输出不要忘了初始化注意分析代码点击查看代码#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#inc......
  • CF888D Almost Identity Permutations 题解
    CF链接:AlmostIdentityPermutationsLuogu链接:AlmostIdentityPermutations${\scr\color{Aquamarine}{\text{Solution}}}$前言这好像是一道能用数学秒掉的题目但......
  • 线段树维护哈希 | CF213E Two Permutations + CF452F Permutation
    __终于学到了线段树维护xx,嘿嘿,嘿嘿嘿..树树:D做了两题,感觉知识点是维护区间相同不相同可以拿hash做,不连续的区间也可以拿hash维护主要是出得很有想法,太妙了要想得到hh1.......
  • E - Find Permutation
    E-FindPermutationhttps://atcoder.jp/contests/abc291/tasks/abc291_e 思路对于能唯一确定的情况,必然存在一个升序路径AX1<AX2<....<AXn***如果有连个......
  • [AGC030F] Permutation and Minimum
    又被计数题创思力。Description洛谷传送门&AT传送门Solution考虑建立图论模型:建立一张包含\(2n\)个点的图,将所有\((A_{2i-1},A_{2i})\)连边。首先,考虑一种连边......
  • 「CF798E」 Mike and code of a permutation
    \(O(n^2)\)做法让第\(i\)个点向\(p_j(p_j>p_i)\)的点连边首先\(i\)肯定能连向\(a_i\),若当\(a_i==-1\),那么当前所有没打过标记的点向\(i\)连边,然后就可以跑出一个拓扑序来......