#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1000000000000000; int bin_search(int n, pair<int, int> a[], int val) { int p = 1; int k = n; int m; while (p < k) { m = (p + k + 1) / 2; if (a[m].first <= val) { p = m; } else { k = m - 1; } } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; pair<int, int> a[n + 2]; ll S[n + 1]; S[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); a[n + 1].first = -1; for (int i = 1; i <= n; i++) { S[i] = S[i - 1] + a[i].first; } char res[n + 1]; bool found = 0; int x; for (int i = n; i >= 1 && !found; i--) { int f = bin_search(n, a, a[i].first); if (S[f] <= a[f + 1].first || a[i].first == a[1].first) { found = 1; x = i; } } for (int i = 1; i <= x; i++) { res[a[i].second] = 'N'; } for (int i = x + 1; i <= n; i++) { res[a[i].second] = 'T'; } for (int i = 1; i <= n; i++) { cout << res[i]; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1000000000000000; int bin_search(int n, pair<int, int> a[], int val) { int p = 1; int k = n; int m; while (p < k) { m = (p + k + 1) / 2; if (a[m].first <= val) { p = m; } else { k = m - 1; } } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; pair<int, int> a[n + 2]; ll S[n + 1]; S[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); a[n + 1].first = -1; for (int i = 1; i <= n; i++) { S[i] = S[i - 1] + a[i].first; } char res[n + 1]; bool found = 0; int x; for (int i = n; i >= 1 && !found; i--) { int f = bin_search(n, a, a[i].first); if (S[f] <= a[f + 1].first || a[i].first == a[1].first) { found = 1; x = i; } } for (int i = 1; i <= x; i++) { res[a[i].second] = 'N'; } for (int i = x + 1; i <= n; i++) { res[a[i].second] = 'T'; } for (int i = 1; i <= n; i++) { cout << res[i]; } cout << "\n"; return 0; } |