C - pushpush
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
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:
- Append ai to the end of b.
- 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
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
思路:容易想到是将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;
}