Codeforces #275 (Div. 2)(數學:找規律)

來源:互聯網
上載者:User

題意是找出n的一個排列p1 p2 ... pn,使得|p2-p1|, |p3-p2| ... |pn-p(n-1)|的值恰好有k個不同

其實這個題是很簡單的

很容易想到的想法是我們先找到一個排列,然後將這個排列通過某種變換得到對應不同k值的結果

我的做法是先找到恰有n-1個不同差值的排列,

很容易發現當n=5時

1 5 2 4 3滿足條件

當n=6時

1 6 2 5 3 4滿足條件

多以對於n

1 n 2 n-1...滿足條件

這個序列對應n-1個不同差值,對於要求的k,我們可以把數列的後半部分截取,排序,

使得差值變為恒為1即可

代碼如下:

#include <bits/stdc++.h>#define MAXN 100010using namespace std;int a[MAXN];int main(void) {    int n, k, flag, tmp;    scanf("%d%d", &n, &k);    a[1] = 1;    flag = 1;    tmp = n-1;    for(int i=2; i<=n; ++i) {        a[i] = a[i-1]+flag*tmp;        --tmp;        flag *= -1;    }    /*    for(int i=1; i<=n; ++i) {        printf("%d ", a[i]);    }    */    sort(a+k+1, a+n+1);    if(abs(a[k+1]-a[k]) == 1) {        for(int i=1; i<=n; ++i) {            printf("%d ", a[i]);        }    } else {        for(int i=1; i<=k; ++i) {            printf("%d ", a[i]);        }        for(int i=n; i>k; --i) {            printf("%d ", a[i]);        }    }    puts("");        return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.