Sicily 1934. 移動小球

來源:互聯網
上載者:User

標籤:sicily

1934. 移動小球Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

你有一些小球,從左至右依次編號為1,2,3,...,n. 你可以執行兩種指令(1或者2)。其中, 1 X Y表示把小球X移動到小球Y的左邊, 2 X Y表示把小球X移動到小球Y右邊。 指令保證合法,即X不等於Y。 例如,初始狀態1,2,3,4,5,6的小球執行1 1 4後,小球1被移動到小球4的左邊,即2,3,1,4,5,6。如果再執行2 3 5,結點3將會移到5的右邊,即2,1,4,5,3,6。 

Input

第一行為一個整數t(0<t<10),表示測試案例個數。每個測試案例的第一行為兩個整數n(1<n<=500000)和m(0<m<100000),n表示小球的個數,m為指令條數,以下m行每行為一條指令。 

Output

為每個測試案例單獨輸出一行,從左至右輸出最後序列,每個數字後面跟一個空格。 

Sample Input
2 6 2 1 1 4 2 3 5 5 1 2 1 5 
Sample Output
2 1 4 5 3 6  2 3 4 5 1  

0.26s:

#include <stdio.h>int l[500005], r[500005];void do_order(int order_num) {    int kind, x, y;    while (order_num--) {        scanf("%d%d%d", &kind, &x, &y);                r[l[x]] = r[x];//首先x球的位置兩邊要處理        l[r[x]] = l[x];                if (kind & 1) {            r[l[y]] = x;//然後新插入位置的兩個球以及它自身要處理            l[x] = l[y];            l[y] = x;            r[x] = y;        } else {            l[r[y]] = x;//同上            r[x] = r[y];            r[y] = x;            l[x] = y;        }    }}void print(int n) {        int temp = 0;    while (n--) {                temp = r[temp];//每次輸出球的右邊的球就是最後的順序        printf("%d ", temp);            }    printf("\n");}int main() {    int case_num, n, order_num;    scanf("%d", &case_num);    while (case_num--) {        scanf("%d%d", &n, &order_num);        r[0] = 1;        for (int i = 1; i <= n; i++) {            l[i] = i - 1;            r[i] = i + 1;        }        do_order(order_num);        print(n);    }    return 0;}                   


Sicily 1934. 移動小球

聯繫我們

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