zoukankan      html  css  js  c++  java
  • codeforces 286B

    题意


    一个数列p(1,2,3,·····n),定义一个变换:f(p[],k),则将这个数列每k个数的第一个数字移动到剩下的k-1个数的后一个的位置,当最后一段不满k个数时,把最后这段第一个数字移动到数组最后的位置。


    分析:其实对于每次处理,都是把每组数的第一个数字移动到下一组数的一个位置,最后一组数的第一个数字移动到最后的位置,就这样模拟一下就行,(感觉这个模拟还有点意思)。

    时间复杂度O(nlogn) (1/2+1/3+1/4·····1/n)应该是logn级别的。

    //顺便吐槽一下用数组写的代码比用deque写的代码丑多了

    #include <cstdio>
    #include <cstring>  
    #include <algorithm>
    using namespace std;  
    #define MAX_NUM         2000007
    int number[MAX_NUM];
    int main(int argc, char const *argv[])
    {  
        int n;  
        scanf("%d",&n);  
        for (int i = 1; i <= n; ++i)
            number[i] = i;
        for (int i = 2; i <= n; ++i)
        {
            int pre = -1;
            for (int j = i-1; j <= n+i-2 ; j += i)
                swap(pre,number[j]);
            number[ n+i-1 ] = pre;
        }
        for (int i = n; i <= 2*n-1; ++i)
            printf("%d ",number[i] );
        return 0;
    }  
  • 相关阅读:
    257. Binary Tree Paths
    324. Wiggle Sort II
    315. Count of Smaller Numbers After Self
    350. Intersection of Two Arrays II
    295. Find Median from Data Stream
    289. Game of Life
    287. Find the Duplicate Number
    279. Perfect Squares
    384. Shuffle an Array
    E
  • 原文地址:https://www.cnblogs.com/miamiao/p/6875171.html
Copyright © 2011-2022 走看看