#include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5; int n; pair<long long, int> arr[MAX_N + 5]; string result; bool possible(int index) { long long s = arr[index].first; for (int i = 0; i < n; ++i) { if (i == index) continue; if (arr[i].first >= s) return false; else s += arr[i].first; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i].first; arr[i].second = i; } sort(arr, arr + n); result.resize(n); int bsMin = 0; int bsMax = n - 1; while (bsMax - bsMin > 1) { int bsMid = (bsMin + bsMax) / 2; if (possible(bsMid)) bsMax = bsMid; else bsMin = bsMid; } long long minPossible = (possible(bsMin) ? arr[bsMin].first : arr[bsMax].first); for (int i = 0; i < n; ++i) result[arr[i].second] = (arr[i].first >= minPossible ? 'T' : 'N'); cout << result << "\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 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5; int n; pair<long long, int> arr[MAX_N + 5]; string result; bool possible(int index) { long long s = arr[index].first; for (int i = 0; i < n; ++i) { if (i == index) continue; if (arr[i].first >= s) return false; else s += arr[i].first; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i].first; arr[i].second = i; } sort(arr, arr + n); result.resize(n); int bsMin = 0; int bsMax = n - 1; while (bsMax - bsMin > 1) { int bsMid = (bsMin + bsMax) / 2; if (possible(bsMid)) bsMax = bsMid; else bsMin = bsMid; } long long minPossible = (possible(bsMin) ? arr[bsMin].first : arr[bsMax].first); for (int i = 0; i < n; ++i) result[arr[i].second] = (arr[i].first >= minPossible ? 'T' : 'N'); cout << result << "\n"; return 0; } |