题解 | #洗牌-用双栈#
洗牌
http://www.nowcoder.com/practice/5a0a2c7e431e4fbbbb1ff32ac6e8dfa0
//偶数牌,洗牌算法,一开始给数组预开辟的容量写成n了,应该是2n,同时处理两半n
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int main(void)
{
stack<int> right;
stack<int> left;
int T,n,k;
cin>>T;
while(T--)
{
cin>>n>>k;
vector<int> all(2*n,0);
for(int i=0;i<n;i++)
{
cin>>all[2*i];
cin>>all[2*i+1];
}
while(k--)
{
for(int i=0;i<n;i++)
{
right.push(all[n+i]);
left.push(all[i]);
}
for(int i=n;i>0;i--)
{
all[2*i-1] = right.top();
right.pop();
all[2*i-2] = left.top();
left.pop();
}
}
for(int i=0;i<2*n;i++) cout<<all[i]<<" ";
cout<<endl;
}
}