1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
int main()
{
std::vector<int> seq = { 67, 47, 50, 69, 70, 79, 85, 74, 63, 52, 49, 58 } ;
for( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;
std::vector< std::pair<int,int> > temp ; // weight for sorting, element
temp.emplace_back( seq[1], seq[0] ) ;
for( std::size_t i = 1 ; i < (seq.size()-1) ; ++i )
temp.emplace_back( seq[i-1] + seq[i+1], seq[i] ) ;
temp.emplace_back( seq[ seq.size() - 2 ], seq.back() ) ;
std::sort( temp.begin(), temp.end() ) ; // sort on weights
for( std::size_t i = 0 ; i < seq.size() ; ++i ) seq[i] = temp[i].second ;
for( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;
}
|