#include <algorithm> #include <iostream> #include <vector> using vint = std::vector< int >; struct MinMaxer { vint minhead; vint maxtail; MinMaxer(vint const& v) { size_t sz = v.size(); minhead.resize(sz); minhead[0] = v[0]; for(int i = 1; i < sz; ++i) { minhead[i] = std::min(minhead[i-1], v[i]); } maxtail.resize(sz); maxtail[sz-1] = v[sz-1]; for(int i = sz - 2; 0 <= i; --i) { maxtail[i] = std::max(maxtail[i+1], v[i]); } } int min_front(int i) { return minhead[i]; } int max_end(int i) { return maxtail[i]; } }; vint solve2(vint const& profits) { MinMaxer mm(profits); for(int i = 0; i < profits.size() - 1; ++i) { if(mm.min_front(i) >= mm.max_end(i + 1)) { return {i + 1}; } } return {}; } vint solve3(vint const& profits) { MinMaxer mm(profits); for(int i = 1; i < profits.size() - 1; ++i) { int middle = profits[i]; if( (mm.min_front(i - 1) >= middle) || (middle >= mm.max_end(i + 1)) ) { return {i, i + 1}; } } return {}; } int find_last_high(vint const& v) { for(int i = 1; i < v.size(); ++i) { if(v[i] <= v[i-1]) return i - 1; } return -1; } vint split(vint const& profits, int last_high, int num_intervals) { int crap = last_high - 1; // licze od 0, +1 przy wkladaniu int high = last_high; int low = last_high + 1; vint result; if(0 <= crap) { // crap moze byc zbiorem pustym result.push_back(crap + 1); } result.push_back(high + 1); // high wkladam zawsze // nie wkladam low, bo moze byc na samym koncu // size() - 1 bo ostatniego nie wypisuje for(int i = low; i < profits.size() - 1; ++i) { if(result.size() == num_intervals - 1) break; result.push_back(i + 1); } for(int i = 0; i < crap; ++i) { if(result.size() == num_intervals - 1) break; result.push_back(i + 1); } std::sort(result.begin(), result.end()); return result; } vint solve4plus(vint const& profits, int num_intervals) { int last_high = find_last_high(profits); if(last_high == -1) return {}; return split(profits, last_high, num_intervals); } int main() { int num_profits; std::cin >> num_profits; int num_intervals; std::cin >> num_intervals; vint profits(num_profits); for(int i = 0; i < num_profits; ++i) { std::cin >> profits[i]; } vint solution; if(num_intervals == 2) { solution = solve2(profits); } else if(num_intervals == 3) { solution = solve3(profits); } else { solution = solve4plus(profits, num_intervals); } size_t sz = solution.size(); if (sz == 0) { std::cout << "NIE\n"; } else { for(int i = 0; i < sz - 1; ++i) { std::cout << solution[i] << " "; } std::cout << solution[sz - 1] << "\n"; } 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 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 132 133 134 135 | #include <algorithm> #include <iostream> #include <vector> using vint = std::vector< int >; struct MinMaxer { vint minhead; vint maxtail; MinMaxer(vint const& v) { size_t sz = v.size(); minhead.resize(sz); minhead[0] = v[0]; for(int i = 1; i < sz; ++i) { minhead[i] = std::min(minhead[i-1], v[i]); } maxtail.resize(sz); maxtail[sz-1] = v[sz-1]; for(int i = sz - 2; 0 <= i; --i) { maxtail[i] = std::max(maxtail[i+1], v[i]); } } int min_front(int i) { return minhead[i]; } int max_end(int i) { return maxtail[i]; } }; vint solve2(vint const& profits) { MinMaxer mm(profits); for(int i = 0; i < profits.size() - 1; ++i) { if(mm.min_front(i) >= mm.max_end(i + 1)) { return {i + 1}; } } return {}; } vint solve3(vint const& profits) { MinMaxer mm(profits); for(int i = 1; i < profits.size() - 1; ++i) { int middle = profits[i]; if( (mm.min_front(i - 1) >= middle) || (middle >= mm.max_end(i + 1)) ) { return {i, i + 1}; } } return {}; } int find_last_high(vint const& v) { for(int i = 1; i < v.size(); ++i) { if(v[i] <= v[i-1]) return i - 1; } return -1; } vint split(vint const& profits, int last_high, int num_intervals) { int crap = last_high - 1; // licze od 0, +1 przy wkladaniu int high = last_high; int low = last_high + 1; vint result; if(0 <= crap) { // crap moze byc zbiorem pustym result.push_back(crap + 1); } result.push_back(high + 1); // high wkladam zawsze // nie wkladam low, bo moze byc na samym koncu // size() - 1 bo ostatniego nie wypisuje for(int i = low; i < profits.size() - 1; ++i) { if(result.size() == num_intervals - 1) break; result.push_back(i + 1); } for(int i = 0; i < crap; ++i) { if(result.size() == num_intervals - 1) break; result.push_back(i + 1); } std::sort(result.begin(), result.end()); return result; } vint solve4plus(vint const& profits, int num_intervals) { int last_high = find_last_high(profits); if(last_high == -1) return {}; return split(profits, last_high, num_intervals); } int main() { int num_profits; std::cin >> num_profits; int num_intervals; std::cin >> num_intervals; vint profits(num_profits); for(int i = 0; i < num_profits; ++i) { std::cin >> profits[i]; } vint solution; if(num_intervals == 2) { solution = solve2(profits); } else if(num_intervals == 3) { solution = solve3(profits); } else { solution = solve4plus(profits, num_intervals); } size_t sz = solution.size(); if (sz == 0) { std::cout << "NIE\n"; } else { for(int i = 0; i < sz - 1; ++i) { std::cout << solution[i] << " "; } std::cout << solution[sz - 1] << "\n"; } return 0; } |