1462A - Favorite Sequence

Reverse the operation.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n), ans(n);
        read_n(begin(a), n);
        int i = 0, l = 0, r = n - 1;
        while (l < r)
        {
            ans[i++] = a[l++];
            ans[i++] = a[r--];
        }
        if (l == r)
            ans[i] = a[l];
        write(begin(ans), end(ans), " "), cout << endl;
    }
    return 0;
}