D. Little Artem and Dance---cf669D(類比)

來源:互聯網
上載者:User

標籤:

題目連結:http://codeforces.com/problemset/problem/669/D

給你n個數,一開始是1 2 3 4 5 6 ... n 這樣的

現在有兩個操作,第一個操作是所有數向右邊移動x個位置 

第二個操作奇數和偶數的位置互換

現在有Q個指令,問最後的序列是什麼;

 n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000)

 

由於資料範圍比較大,但是我們可以發現所以得偶數他們相鄰的總是2 4 6 8 10...奇數都是1 3 5 7 9 11...也就是說無論怎麼變他們的相對位置是不變的,就是每個數(奇數或偶數)的前後總是固定的,

所以我們可以記錄數字1和2的位置在哪裡,然後依次填出最終答案;

為了方便我們下標從0開始;

 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <map>#include <vector>using namespace std;typedef long long LL;#define N 1000100#define met(a, b) memset(a, b, sizeof(a))int n, Q, a[N];int main(){    while(scanf("%d %d", &n, &Q)!=EOF)    {        met(a, 0);        int first = 0, second = 1, op, x;        while(Q--)        {            scanf("%d", &op);            if(op == 1)            {                scanf("%d", &x);                first = (x + first + n) % n;                second = (x + second + n) % n;            }            else            {///如果1所在位置下標是偶數(相對於下標為0來說的),那麼1的位置就要往後挪一位,2則相反;                if(first%2)                {                    first = (first - 1 + n) % n;                    second = (second + 1 + n) % n;                }                else                {                    first = (first + 1 + n) % n;                    second = (second - 1 + n) % n;                }            }        }        int num = 1;        while(num <= n)///填入n個數        {            a[first] = num++;///奇數所在位置            a[second] = num++;            first = (first+2) % n;///要加兩個的,            second = (second+2) % n;        }        for(int i=0; i<n; i++)            printf("%d%c", a[i], i==n-1?‘\n‘:‘ ‘);    }    return 0;}/*6 921 -221 -61 -61 421 -122 5 4 1 6 3*/
View Code

 

D. Little Artem and Dance---cf669D(類比)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.