#include <iostream> #include <algorithm> using namespace std; #define N 500005 int n; pair<int, int> a[N]; long long p[N]; bool ans[N]; bool can_be_king(int p){ long long sum = a[p].first; for(int i=0;i<n;i++){ if(i == p) continue; else if(a[i].first >= sum){ return false; } else{ sum += a[i].first; } } return true; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); cin >> n; for(int i=0;i<n;i++){ cin >> a[i].first; a[i].second = i; } sort(a, a+n); int beg = 0; int end = n-1; int pos = n; while(beg <= end){ int mid = (beg + end)/2; if(can_be_king(mid)){ end = mid - 1; pos = mid; } else{ beg = mid + 1; } } for(int i=pos;i<n;i++){ ans[a[i].second] = true; } for(int i=0;i<n;i++){ cout << (ans[i] ? "T" : "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 | #include <iostream> #include <algorithm> using namespace std; #define N 500005 int n; pair<int, int> a[N]; long long p[N]; bool ans[N]; bool can_be_king(int p){ long long sum = a[p].first; for(int i=0;i<n;i++){ if(i == p) continue; else if(a[i].first >= sum){ return false; } else{ sum += a[i].first; } } return true; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); cin >> n; for(int i=0;i<n;i++){ cin >> a[i].first; a[i].second = i; } sort(a, a+n); int beg = 0; int end = n-1; int pos = n; while(beg <= end){ int mid = (beg + end)/2; if(can_be_king(mid)){ end = mid - 1; pos = mid; } else{ beg = mid + 1; } } for(int i=pos;i<n;i++){ ans[a[i].second] = true; } for(int i=0;i<n;i++){ cout << (ans[i] ? "T" : "N"); } return 0; } |