#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> a(n); for(int &ai : a) cin >> ai; debug(n, k, a); auto solve = [&]() -> vector<int> { if(k == 2) { vector<int> pref_min = a, suff_max = a; FOR(i, 1, n - 1) pref_min[i] = min(pref_min[i], pref_min[i - 1]); for(int i = n - 2; i >= 0; --i) suff_max[i] = max(suff_max[i], suff_max[i + 1]); REP(i, n - 1) if(pref_min[i] >= suff_max[i + 1]) return {i}; return {}; } else if(k == 3) { map<int, vector<int>> val_loc; REP(i, n) val_loc[a[i]].emplace_back(i); auto ret_single_mid = [&](int i) -> vector<int>{ if(i == 0) return {i, i + 1}; if(i == n - 1) return {i - 2, i - 1}; return vector{i - 1, i}; }; if(val_loc.begin()->second.back() != 0) return ret_single_mid(val_loc.begin()->second.back()); if(prev(val_loc.end())->second.front() != n - 1) return ret_single_mid(prev(val_loc.end())->second.front()); return {}; } else { assert(k >= 4); int found_split = -1; REP(i, n - 1) if(a[i] >= a[i + 1]) found_split = i; if(found_split == -1) return {}; set<int> ret = {found_split}; if(found_split != 0) ret.emplace(found_split - 1); if(found_split != n - 2) ret.emplace(found_split + 1); for(int i = found_split - 2; i >= 0; --i) if(ssize(ret) < k - 1) ret.emplace(i); for(int i = found_split + 2; i < n - 1; ++i) if(ssize(ret) < k - 1) ret.emplace(i); assert(ssize(ret) == k - 1); return {ret.begin(), ret.end()}; } }; vector<int> ans = solve(); if(ans.empty()) { #ifdef LOCAL cout << "OK\n"; #else cout << "NIE\n"; #endif return 0; } #ifdef LOCAL REP(i, ssize(ans) - 1) assert(ans[i] < ans[i + 1]); assert(ssize(ans) == k - 1); assert(ans.back() != n - 1); vector<bool> is_ending(n); for(int vi : ans) is_ending[vi] = true; vector<vector<int>> vals = {{}}; REP(i, n) { vals.back().emplace_back(a[i]); if(is_ending[i]) vals.emplace_back(); } debug(vals); auto is_valid = [&] { REP(i, ssize(vals) - 1) { int mn = *min_element(vals[i].begin(), vals[i].end()); int mx = *max_element(vals[i + 1].begin(), vals[i + 1].end()); if(mn >= mx) return true; } return false; }; assert(is_valid()); #endif #ifdef LOCAL cout << "OK\n"; #else cout << "TAK\n"; for(int vi : ans) cout << vi + 1 << ' '; cout << '\n'; #endif }
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> a(n); for(int &ai : a) cin >> ai; debug(n, k, a); auto solve = [&]() -> vector<int> { if(k == 2) { vector<int> pref_min = a, suff_max = a; FOR(i, 1, n - 1) pref_min[i] = min(pref_min[i], pref_min[i - 1]); for(int i = n - 2; i >= 0; --i) suff_max[i] = max(suff_max[i], suff_max[i + 1]); REP(i, n - 1) if(pref_min[i] >= suff_max[i + 1]) return {i}; return {}; } else if(k == 3) { map<int, vector<int>> val_loc; REP(i, n) val_loc[a[i]].emplace_back(i); auto ret_single_mid = [&](int i) -> vector<int>{ if(i == 0) return {i, i + 1}; if(i == n - 1) return {i - 2, i - 1}; return vector{i - 1, i}; }; if(val_loc.begin()->second.back() != 0) return ret_single_mid(val_loc.begin()->second.back()); if(prev(val_loc.end())->second.front() != n - 1) return ret_single_mid(prev(val_loc.end())->second.front()); return {}; } else { assert(k >= 4); int found_split = -1; REP(i, n - 1) if(a[i] >= a[i + 1]) found_split = i; if(found_split == -1) return {}; set<int> ret = {found_split}; if(found_split != 0) ret.emplace(found_split - 1); if(found_split != n - 2) ret.emplace(found_split + 1); for(int i = found_split - 2; i >= 0; --i) if(ssize(ret) < k - 1) ret.emplace(i); for(int i = found_split + 2; i < n - 1; ++i) if(ssize(ret) < k - 1) ret.emplace(i); assert(ssize(ret) == k - 1); return {ret.begin(), ret.end()}; } }; vector<int> ans = solve(); if(ans.empty()) { #ifdef LOCAL cout << "OK\n"; #else cout << "NIE\n"; #endif return 0; } #ifdef LOCAL REP(i, ssize(ans) - 1) assert(ans[i] < ans[i + 1]); assert(ssize(ans) == k - 1); assert(ans.back() != n - 1); vector<bool> is_ending(n); for(int vi : ans) is_ending[vi] = true; vector<vector<int>> vals = {{}}; REP(i, n) { vals.back().emplace_back(a[i]); if(is_ending[i]) vals.emplace_back(); } debug(vals); auto is_valid = [&] { REP(i, ssize(vals) - 1) { int mn = *min_element(vals[i].begin(), vals[i].end()); int mx = *max_element(vals[i + 1].begin(), vals[i + 1].end()); if(mn >= mx) return true; } return false; }; assert(is_valid()); #endif #ifdef LOCAL cout << "OK\n"; #else cout << "TAK\n"; for(int vi : ans) cout << vi + 1 << ' '; cout << '\n'; #endif } |