Problem Description
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 … anOutput
Print n integers in a line with spaces in between. The i-th integer should be bi.
Example
Sample Input 1
4
1 2 3 4Sample Output 1
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
3
1 2 3Sample Output 2
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
1
1000000000Sample Output 3
1000000000
Sample Input 4
6
0 6 7 6 7 0Sample Output 4
0 6 6 0 7 7
题意: 给出 n 个数和一个空序列,要求对空序列执行 n 次操作,每次操作将第 i 个数放到序列末尾,并序列进行反转,问 n 次操作后获得的序列
思路:
简单的推导一下可以发现:
- n 为奇数时:最终序列元素的下标为 n,n-2,n-4,...,2,1,3,...,n-5,n-3,n-1
- n 为偶数时:最终序列元素的下标为 n,n-2,n-4,...,2,1,...,n-5,n-3,n-1
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n%2){
for(int i=n;i>=1;i-=2)
printf("%d ",a[i]);
for(int i=2;i<=n;i+=2)
printf("%d ",a[i]);
printf("\n");
}
else{
for(int i=n;i>=1;i-=2)
printf("%d ",a[i]);
for(int i=1;i<=n;i+=2)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}