uva 11129 - An antiarithmetic permutation

点击打开链接uva 11129

题目意思:

给定一个初始序列为0 1 2 3 .....n-1;要求找到一个序列能够满足所有的子序列都不能够形成等差序列

解题思路:

1思路:递归+分治
2分析:网上那些神牛是这么说的:把这个序列按照奇偶位置分开成两个序列,然后再对两个序列进行奇偶位置分开......最后得到的数组就是所要的ans,为什么这样可以呢,因为在奇数位置内和偶数位置内等差为2,而两个序列之间为1,所以这样肯定不会构成等差序列。

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define MAXN 10010

int n;
int ans[MAXN] , tmp[MAXN];
/*递归求解*/
void solve(int l , int r) {
    int i , j;
    if(r == l) return;/*只有一个数的时候直接返回*/
    memcpy(tmp , ans , sizeof(ans));/*将ans数组复制给tmp数组*/
    for(i = l , j = l ; i <= r ; i += 2 , j++)/*奇数位置存储*/
        ans[j] = tmp[i];
    for(i = l+1; i <= r ; i += 2 , j++)/*偶数位置存储*/
        ans[j] = tmp[i];
    solve(l , (l+r)/2);/*左半部递归*/
    solve((l+r)/2+1 , r);/*右半部递归*/
}

void output(){
    printf("%d:" , n);
    for(int i = 0 ; i < n ; i++)
        printf(" %d" , ans[i]);
    printf("\n");
}

int main() {
    //freopen("input.txt", "r", stdin);
    while(scanf("%d%*c" , &n) && n){
        for(int i = 0 ; i < n ; i++)/*初始化*/
            ans[i] = i;
        solve(0 , n-1) ; output();
    }
    return 0;
}
时间: 2024-10-03 17:14:25

uva 11129 - An antiarithmetic permutation的相关文章

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 cal

UVa 10252 Common Permutation (water ver.)

10252 - Common Permutation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1193 Given two strings of lowercase letters, a and b, print the longest string

uva 10730 - Antiarithmetic?

点击打开链接uva 10730 思路:枚举等差中项 分析: 1 给定一个n个数的序列判断是否有等差子序列 2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列 3 对于n<=10000,那么我们怎么去判断呢,由于我们要找的是是否有三个的等差序列,那么我们可以枚举每一个数作为等差中项,然后去判断 4 假设现在我们枚举num[i]作为等差中项,那么第一个数在0~i-1之间,第二个数是在i+1~n-1之间,这个时候如果单存利用for循环去找时间复杂度不能接受,那么我们想到

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 514 Rails (water ver.)

514 - Rails Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455 There is a famous railway station in PopPush City. Country there is incredibly hilly. The

UVa 299 Train Swapping:冒泡排序的次数

299 - Train Swapping Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=235 At an old railway station, you may still encounter one of the last remaining ``tr

UVa 10098 Generating Fast:全排列生成

10098 - Generating Fast Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1039 Generating permutation has always been an important problem in computer science. In this problem you will have

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.