#include <iostream> using namespace std; const int SIZE = 500001; int n; pair<int, int> sum[SIZE]; char arr[SIZE]; bool canBeKing(int id) { long long mass = sum[id].first; for(int i = 0; i < n; i++) { if(i == id) continue; if(mass > sum[i].first) mass += sum[i].first; else return false; } return true; } int findSmallestKing(int l = 0, int r = n - 1) { int mid = (l + r) / 2; if(l == r) { if(canBeKing(mid)) return mid; else return n; } if(canBeKing(mid)) return findSmallestKing(l, mid); else return findSmallestKing(mid + 1, r); } int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { cin >> n; for(int i = 0; i < n; i++) { cin >> sum[i].first; sum[i].second = i; } qsort(sum, n, sizeof(pair<int, int>), compare); int id = findSmallestKing(); for(int i = 0; i < id; i++) arr[sum[i].second] = 'N'; for(int i = id; i < n; i++) arr[sum[i].second] = 'T'; for(int i = 0; i < n; i++) cout << arr[i]; cout << "\n"; }
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> using namespace std; const int SIZE = 500001; int n; pair<int, int> sum[SIZE]; char arr[SIZE]; bool canBeKing(int id) { long long mass = sum[id].first; for(int i = 0; i < n; i++) { if(i == id) continue; if(mass > sum[i].first) mass += sum[i].first; else return false; } return true; } int findSmallestKing(int l = 0, int r = n - 1) { int mid = (l + r) / 2; if(l == r) { if(canBeKing(mid)) return mid; else return n; } if(canBeKing(mid)) return findSmallestKing(l, mid); else return findSmallestKing(mid + 1, r); } int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { cin >> n; for(int i = 0; i < n; i++) { cin >> sum[i].first; sum[i].second = i; } qsort(sum, n, sizeof(pair<int, int>), compare); int id = findSmallestKing(); for(int i = 0; i < id; i++) arr[sum[i].second] = 'N'; for(int i = id; i < n; i++) arr[sum[i].second] = 'T'; for(int i = 0; i < n; i++) cout << arr[i]; cout << "\n"; } |