AtCoder:pushpush(STL & 思维)

C - pushpush


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given an integer sequence of length na1,…,an. Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

  1. Append ai to the end of b.
  2. Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

  • 1n2×105
  • 0ai109
  • n and ai are integers.

Input

Input is given from Standard Input in the following format:

n
a1 a2  an

Output

Print n integers in a line with spaces in between. The i-th integer should be bi.


Sample Input 1

Copy
4
1 2 3 4

Sample Output 1

Copy
4 2 1 3
  • After step 1 of the first operation, b becomes: 1.
  • After step 2 of the first operation, b becomes: 1.
  • After step 1 of the second operation, b becomes: 1,2.
  • After step 2 of the second operation, b becomes: 2,1.
  • After step 1 of the third operation, b becomes: 2,1,3.
  • After step 2 of the third operation, b becomes: 3,1,2.
  • After step 1 of the fourth operation, b becomes: 3,1,2,4.
  • After step 2 of the fourth operation, b becomes: 4,2,1,3.

Thus, the answer is 4 2 1 3.


Sample Input 2

Copy
3
1 2 3

Sample Output 2

Copy
3 1 2

As shown above in Sample Output 1, b becomes 3,1,2 after step 2 of the third operation. Thus, the answer is 3 1 2.


Sample Input 3

Copy
1
1000000000

Sample Output 3

Copy
1000000000

Sample Input 4

Copy
6
0 6 7 6 7 0

Sample Output 4

Copy
0 6 6 0 7 7
题意:给n个数a和一个空序列b,每次执行两个操作,①将a[i]放到b的末尾,②翻转b序列,输出执行完n个数后的b序列。

思路:容易想到是将a[i]前后前后地放到b里面,那么用queue或者list都能方便实现。

# include <bits/stdc++.h>
using namespace std;
list<int>l;
int main()
{
    int n, m, t=1;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
    {
        scanf("%d",&m);
        t = !t;
        if(t) l.push_back(m);
        else l.push_front(m);
    }
    if(!(n&1)) l.reverse();
    for(auto i=l.begin();i!=l.end();++i)
        printf("%d ",*i);
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值