← 返回题目页

A1260 序列

标程

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    // b 每轮「末尾加入一个元素后整体翻转」,等价于往双端队列两端交替插入
    deque<int> q;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        if (i % 2 == 0) {
            q.push_back(x);
        } else {
            q.push_front(x);
        }
    }

    bool first = true;
    for (int x : q) {
        if (!first) cout << ' ';
        first = false;
        cout << x;
    }
    cout << '\n';
    return 0;
}

动画演示