• Adjacent Swaps


    Adjacent Swaps

    https://atcoder.jp/contests/abc250/tasks/abc250_c

     



    Solution

    这是一个典型的利用双向链表来提供性能的问题。

    利用双向链表,需要注意:

    (1)添加 表头 和 表尾 节点。

    (2)抽象出交换相邻节点,需要用到的 prev 和 next_next节点。

    #include <bits/stdc++.h>
    #include <vector>
    #include <algorithm>
    #include <deque>
    #include <queue>
    #include <string>
    #include <set>
    using namespace std;
    
    
    /*
    https://atcoder.jp/contests/abc250/tasks/abc250_c
    */
    
    struct node {
        int prev;
        int next;
    };
    
    const int limit = 200010;
    
    struct node balls[limit] = {
        {
            0, 0
        }
    };
    
    int main() {
        int n, q;
    
        cin >> n >> q;
    
    //    cout << to_string(n) << endl;
    
        // initialize n ball node prev and next
        // normally all previous and next pointer value must be in range [1, n]
        // but for first node, its previous pointer is 0, called head
        // and for last node, its next pointer is n+1, called tail
        int head_index = 0;
        int tail_index = n + 1;
        balls[head_index].next = 1;
        balls[tail_index].prev = n;
    
        for (int i = 1; i <= n; i++) {
            balls[i].prev = i - 1;
            balls[i].next = i + 1;
        }
    
        for (int i = 0; i < q; i++) {
            int xi;
            cin >> xi;
    
            // need to swap curr_index and next_index
            int curr_index = xi;
            int next_index = balls[curr_index].next;
    
            int prev_index = 0;
            int next_next_index = 0;
    
            // current ball is the last ball,
            // set curr_index as n-2, next_index as n-1
            if (next_index == tail_index) {
                int prev_curr_index = balls[curr_index].prev;
                next_index = curr_index;
                curr_index = prev_curr_index;
            }
    
            // find two helping index
            prev_index = balls[curr_index].prev;
            next_next_index = balls[next_index].next;
    
            //swap curr and next
            balls[prev_index].next = next_index;
            balls[next_next_index].prev = curr_index;
    
            balls[curr_index].prev = next_index;
            balls[curr_index].next = next_next_index;
    
            balls[next_index].prev = prev_index;
            balls[next_index].next = curr_index;
        }
    
        // print
        int curr_index = balls[head_index].next;
        while (curr_index != tail_index) {
            cout << curr_index;
            curr_index = balls[curr_index].next;
    
            if (curr_index != tail_index) {
                cout << " ";
            }
        }
    }
  • 相关阅读:
    多模块应用自动化部署
    shell杀死指定端口的进程
    SpringBoot打包成war
    Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes
    nginx上配置phpmyadmin
    Ubuntu16.04中php如何切换版本
    E:dpkg was interrupted, you must manually run'dpkg配置'to correct the problem.
    Edusoho之LAMP环境搭建
    cocos2d-x -3.81+win7+vs2013开发环境创建新的项目
    M2Mqtt is a MQTT client available for all .Net platform
  • 原文地址:https://www.cnblogs.com/lightsong/p/16260051.html
Copyright © 2020-2023  润新知