问题 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;
}