The way to swing the list follows the algorithm below :
1Pick the smallest integer from s and append it to the result.
2Pick the smallest integer from which is greater than the last appended integer to the result and append it.
3Repeat step 2 until you cannot pick more integers.
4Pick the largest integer from s and append it to the result.
5Pick the largest integer from s which is smaller than the last appended integer to the result and append it.
6Repeat step 5 until you cannot pick more integer.
7Repeat the steps from 1 to 6 until you pick all integers from s .
Input Format
The first line has a integer meaning there are n lists.
The next n line, each line has a list. The first integer l of a line is the length of the list. And followed integers are the elements of the list.
I've tried modifying the iterators, while the code below shows no output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <list>
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>
using namespace std;
int main()
{
int n,i;
cin>>n;
while(n--)
{
int L,val;
cin>>L;
vector<int> A(L);
vector<int>::iterator it,itr;
A.clear();
for(auto& a:A)
cin>>a;
sort(A.begin(),A.end());
while(!A.empty())
{
val=*(A.begin());
cout<<val<<" ";
it=A.begin();
for( i=0;i<A.size();i++)
{
if(*(it+i)>val)
{
val=*(it+i);
cout<<val<<" ";
itr=it+i;
A.erase(itr);
it--;
}
}
val=*(A.end()-1);
cout<<val<<" ";
it=(A.end()-1);
for(i=0;i<A.size(); i++)
{
if(*(it-i)<val)
{
val=*(it-i);
cout<<val<<" ";
itr=it-i;
A.erase(itr);
it--;
}
}
}
cout<<"\n";
}
return 0;
}
|