#include <iostream> #include <algorithm> // std::fill #include <vector> // std::vector #include <iterator> using namespace std; // class generator: struct c_unique { int current; c_unique() {current=0;} int operator()() {return ++current;} } UniqueNumber; template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) { if ( !v.empty() ) { //out << "[" std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, " ")); //out << "\b\b]"; } return out; } int count_inversions(vector<int> &seq) { int cnt = 0; for (int i = 0; i<seq.size(); i++) { for (int j = i+1; j<seq.size(); j++) { if(seq[i]>seq[j]) cnt++; } } return cnt; } bool is_stable(vector<int> &seq) { vector<int> reversed_seq(seq); reverse(reversed_seq.begin(), reversed_seq.end()); //cout<<" "<<seq<<" "<<reversed_seq<<endl; return (count_inversions(seq) == count_inversions(reversed_seq)); } int main() { int n, k; cin>>n>>k; vector<int> seq(n); std::generate (seq.begin(), seq.end(), UniqueNumber); int i=0; do { // cout<<seq<<" "<<"("<<count_inversions(seq)<<")"<<endl; if(is_stable(seq)) { if(++i == k) { cout<<"TAK"<<endl<<seq<<endl; return 0; } } } while ( std::next_permutation(seq.begin(), seq.end()) ); cout<<"NIE"<<endl; return 0; }
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 | #include <iostream> #include <algorithm> // std::fill #include <vector> // std::vector #include <iterator> using namespace std; // class generator: struct c_unique { int current; c_unique() {current=0;} int operator()() {return ++current;} } UniqueNumber; template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) { if ( !v.empty() ) { //out << "[" std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, " ")); //out << "\b\b]"; } return out; } int count_inversions(vector<int> &seq) { int cnt = 0; for (int i = 0; i<seq.size(); i++) { for (int j = i+1; j<seq.size(); j++) { if(seq[i]>seq[j]) cnt++; } } return cnt; } bool is_stable(vector<int> &seq) { vector<int> reversed_seq(seq); reverse(reversed_seq.begin(), reversed_seq.end()); //cout<<" "<<seq<<" "<<reversed_seq<<endl; return (count_inversions(seq) == count_inversions(reversed_seq)); } int main() { int n, k; cin>>n>>k; vector<int> seq(n); std::generate (seq.begin(), seq.end(), UniqueNumber); int i=0; do { // cout<<seq<<" "<<"("<<count_inversions(seq)<<")"<<endl; if(is_stable(seq)) { if(++i == k) { cout<<"TAK"<<endl<<seq<<endl; return 0; } } } while ( std::next_permutation(seq.begin(), seq.end()) ); cout<<"NIE"<<endl; return 0; } |