P10522 雪中楼 Solution

· · 题解

题目传送门

感觉赛时没 A 这题挺可惜的,毕竟链表用的不多。

边输入边插入,如果 x=0 就插入到链表尾部,否则插入到 a_x 左边,最后翻转输出。

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
const int N = 2e5 + 10;
list<int> ls;
list<int>::iterator a[N] = {ls.begin(), ls.end()};
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        a[i] = ls.insert(a[x], i);
    }
    reverse(ls.begin(), ls.end());
    while (!ls.empty()) {
        cout << ls.front() << " ";
        ls.pop_front();
    }
    return 0;
}