问题 L: pushpush(双向队列)

问题 L: pushpush

时间限制: 1 Sec  内存限制: 128 MB
提交: 110  解决: 65
[提交] [状态] [讨论版] [命题人:admin]

题目描述

You are given an integer sequence of length n, a1,…,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
1≤n≤2×105
0≤ai≤109
n and ai are integers.

 

输入

Input is given from Standard Input in the following format:
n
a1 a2 … an

 

输出

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

 

样例输入

4
1 2 3 4

 

样例输出

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.

 

找规律也行,但我用的是双向队列,注意最后可能倒着输出

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
deque<int> a;
int main()
{
    int n;
    scanf("%d",&n);

    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        if(i%2==0)
            a.push_front(x);
        else
            a.push_back(x);
    }
    if(!(n%2))
    {
        printf("%d",a.front());
        a.pop_front();
        while(!a.empty())
        {
            printf(" %d",a.front());
            a.pop_front();
        }
        printf("\n");
    }
    else
    {
        printf("%d",a.back());
        a.pop_back();
        while(!a.empty())
        {
            printf(" %d",a.back());
            a.pop_back();
        }
        printf("\n");
    }

    return 0;
}

list好像更简单,附一个别的博主的代码          链接

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
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%2)) l.reverse();
    for(auto i=l.begin();i!=l.end();++i)
        printf("%d ",*i);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值