#include <iostream> #include <algorithm> using namespace std; pair <int, int> tab[500004]; int pref[500004]; bool odp[500004]; bool king(int k, int n) { int sum = pref[k]; for (int i = k+1; i <= n; i++) { if (sum <= tab[i].first) return false; else sum += tab[i].first; } //cout << k << " "; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[i].first; tab[i].second = i; } sort(tab, tab+n); pref[1] = tab[1].first; for (int i = 2; i <= n; i++) { pref[i] = pref[i-1] + tab[i].first; } for (int i = 1; i <= n; i++) { int ind = tab[i].second; odp[ind] = king(i, n); int j = i; while (tab[j+1].first == tab[i].first) { j++; odp[tab[j].second] = odp[ind]; } i = j; //cout << ind << "\n"; } for (int i = 1; i <= n; i++) { if (odp[i]) cout << "T"; else 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 | #include <iostream> #include <algorithm> using namespace std; pair <int, int> tab[500004]; int pref[500004]; bool odp[500004]; bool king(int k, int n) { int sum = pref[k]; for (int i = k+1; i <= n; i++) { if (sum <= tab[i].first) return false; else sum += tab[i].first; } //cout << k << " "; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[i].first; tab[i].second = i; } sort(tab, tab+n); pref[1] = tab[1].first; for (int i = 2; i <= n; i++) { pref[i] = pref[i-1] + tab[i].first; } for (int i = 1; i <= n; i++) { int ind = tab[i].second; odp[ind] = king(i, n); int j = i; while (tab[j+1].first == tab[i].first) { j++; odp[tab[j].second] = odp[ind]; } i = j; //cout << ind << "\n"; } for (int i = 1; i <= n; i++) { if (odp[i]) cout << "T"; else cout << "N"; } return 0; } |