6574: pushpush
时间限制: 1 Sec 内存限制: 128 MB
提交: 97 解决: 57
[提交] [状态] [讨论版] [命题人: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.
解析:第一遍一看觉得题目是栈,没考虑数据大小,果然时间超时。没办法,数据这么大只剩下找规律了,找规律的时候,最好用带下标的a0, a1, a2, a3 。。。来着找规律,找到第6个规律太明了,然后就可以不用处理直接输出结果。
#include <iostream>
#include <stack>
#define N 200005
using namespace std;
stack<int> s;
//int v[N];
int a[N];
int main()
{
int n;
//int x;
/*while(cin>>n){ //用栈超时,数据这么大那只能找规律了
cin >> x;
v[0] = x;
for(int i = 1; i < n; i++){
cin >> x;
for(int j = 0; j <= i-1; j++){
s.push(v[j]);
}
s.push(x);
for(int j = 0; j <= i; j++){
v[j] = s.top();
s.pop();
}
}
for(int i = 0; i < n; i++){
cout << v[i] << endl;
}
}*/
while(cin>>n){ //写到第6个就可以得到规律
for(int i = 0; i < n; i++){
cin >> a[i];
}
if(n % 2 == 1){ //如果是奇数,先下标为奇数的从大到小输出,再把下标为偶数的从小到大输出
cout << a[n-1];
for(int i = n-3; i >= 0; i -= 2){ //奇数部分
cout << " " << a[i];
}
for(int i = 1; i <= n-2; i += 2){
cout << " " << a[i];
}
}
else{ //偶数与奇数相反
cout << a[n-1];
for(int i = n-3; i >= 0; i -= 2){ //偶数部分
cout << " " << a[i];
}
for(int i = 0; i <= n-2; i += 2){
cout << " " << a[i];
}
}
cout << endl;
}
return 0;
}