#include <iostream> #include <algorithm> using namespace std; constexpr int MAXN = 1e6; int tab[MAXN]; bool cmpIndx(int l, int r) { return tab[l] < tab[r]; } int main() { int n; cin >> n; int sorted_indx[n]; long long prefix_sums[n]; bool can_be_king[n]; for (int i = 0; i < n; ++i) { cin >> tab[i]; sorted_indx[i] = i; } sort(&sorted_indx[0], &sorted_indx[n], cmpIndx); prefix_sums[0] = tab[sorted_indx[0]]; for (int i = 1; i < n; ++i) prefix_sums[i] = prefix_sums[i - 1] + tab[sorted_indx[i]]; bool rozne = false; for (int i = 1; i < n; ++i) { if (prefix_sums[i - 1] > tab[sorted_indx[i]] && rozne) { int p = i; while (++p < n && prefix_sums[p - 1] > tab[sorted_indx[p]]) ; if (p == n) { while (i <= n) can_be_king[sorted_indx[i++ - 1]] = true; break; } else { while (i <= p) can_be_king[sorted_indx[i++ - 1]] = false; --i; } } else can_be_king[sorted_indx[i - 1]] = false; if (tab[sorted_indx[i - 1]] != tab[sorted_indx[i]]) rozne = true; } if (rozne) can_be_king[sorted_indx[n - 1]] = true; else can_be_king[sorted_indx[n - 1]] = false; for (int i = 0; i < n; ++i) { if (can_be_king[i]) cout << 'T'; else cout << 'N'; } cout << "\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 | #include <iostream> #include <algorithm> using namespace std; constexpr int MAXN = 1e6; int tab[MAXN]; bool cmpIndx(int l, int r) { return tab[l] < tab[r]; } int main() { int n; cin >> n; int sorted_indx[n]; long long prefix_sums[n]; bool can_be_king[n]; for (int i = 0; i < n; ++i) { cin >> tab[i]; sorted_indx[i] = i; } sort(&sorted_indx[0], &sorted_indx[n], cmpIndx); prefix_sums[0] = tab[sorted_indx[0]]; for (int i = 1; i < n; ++i) prefix_sums[i] = prefix_sums[i - 1] + tab[sorted_indx[i]]; bool rozne = false; for (int i = 1; i < n; ++i) { if (prefix_sums[i - 1] > tab[sorted_indx[i]] && rozne) { int p = i; while (++p < n && prefix_sums[p - 1] > tab[sorted_indx[p]]) ; if (p == n) { while (i <= n) can_be_king[sorted_indx[i++ - 1]] = true; break; } else { while (i <= p) can_be_king[sorted_indx[i++ - 1]] = false; --i; } } else can_be_king[sorted_indx[i - 1]] = false; if (tab[sorted_indx[i - 1]] != tab[sorted_indx[i]]) rozne = true; } if (rozne) can_be_king[sorted_indx[n - 1]] = true; else can_be_king[sorted_indx[n - 1]] = false; for (int i = 0; i < n; ++i) { if (can_be_king[i]) cout << 'T'; else cout << 'N'; } cout << "\n"; return 0; } |