#include <bits/stdc++.h> using namespace std; #define LL long long #define f first #define s second const int t = 5e5 + 9; pair <LL, int> tab[t]; int wynik[t], odp[t]; LL sum[t]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; LL maks = 0; for(int i = 0; i < n; i ++) { cin >> tab[i].f; tab[i].s = i; maks = max(maks, tab[i].f); } if(n == 1) { cout << 'T'; return 0; } sort(tab, tab + n); sum[0] = tab[0].f; for(int i = 1; i < n; i ++) sum[i] = sum[i - 1] + tab[i].f; for(int i = n - 1; i >= 0; i --) { if(tab[0].f < tab[i].f) { int lo = 0, hi = n - 1, mid, best = -1; while(lo <= hi) { mid = (lo + hi) / 2; if(tab[mid].f <= tab[i].f) { best = mid; lo = mid + 1; } else hi = mid - 1; } if(best == n - 1) wynik[i] = 1; else if(tab[best + 1].f < sum[best]) wynik[i] = 1; else break; } else break; } for(int i = 0; i < n; i ++) odp[tab[i].s] = wynik[i]; for(int i = 0; i < n; i ++) { if(odp[i] == 1) 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 | #include <bits/stdc++.h> using namespace std; #define LL long long #define f first #define s second const int t = 5e5 + 9; pair <LL, int> tab[t]; int wynik[t], odp[t]; LL sum[t]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; LL maks = 0; for(int i = 0; i < n; i ++) { cin >> tab[i].f; tab[i].s = i; maks = max(maks, tab[i].f); } if(n == 1) { cout << 'T'; return 0; } sort(tab, tab + n); sum[0] = tab[0].f; for(int i = 1; i < n; i ++) sum[i] = sum[i - 1] + tab[i].f; for(int i = n - 1; i >= 0; i --) { if(tab[0].f < tab[i].f) { int lo = 0, hi = n - 1, mid, best = -1; while(lo <= hi) { mid = (lo + hi) / 2; if(tab[mid].f <= tab[i].f) { best = mid; lo = mid + 1; } else hi = mid - 1; } if(best == n - 1) wynik[i] = 1; else if(tab[best + 1].f < sum[best]) wynik[i] = 1; else break; } else break; } for(int i = 0; i < n; i ++) odp[tab[i].s] = wynik[i]; for(int i = 0; i < n; i ++) { if(odp[i] == 1) cout << 'T'; else cout << 'N'; } return 0; } |