#include <bits/stdc++.h> using namespace std; bool check(int sumS, map<int, int> &sums) { bool ch=true; int sum = sumS; sums[sumS]--; for(auto &elm : sums) { if(elm.first >= sum) { ch=false; break; } sum += (elm.first * elm.second); if( (--sums.end())->first < sum) { break; } } sums[sumS]++; return ch; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, x; map<int, int> sums; cin >> n; int tab[n]; int inTab[n]; for(int i=0; i<n; ++i) { cin >> x; sums[x]++; tab[i] = x; inTab[i] = x; } sort(tab, tab+n); int l=0, h=n-1; while(l+1 != h) { int mid = (l + h) / 2; if(check(tab[mid], sums)) h = mid; else l = mid; } for(int i=0; i<n; ++i) { if(inTab[i] <= tab[l]) cout << 'N'; else cout << 'T'; } cout << endl; 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 | #include <bits/stdc++.h> using namespace std; bool check(int sumS, map<int, int> &sums) { bool ch=true; int sum = sumS; sums[sumS]--; for(auto &elm : sums) { if(elm.first >= sum) { ch=false; break; } sum += (elm.first * elm.second); if( (--sums.end())->first < sum) { break; } } sums[sumS]++; return ch; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, x; map<int, int> sums; cin >> n; int tab[n]; int inTab[n]; for(int i=0; i<n; ++i) { cin >> x; sums[x]++; tab[i] = x; inTab[i] = x; } sort(tab, tab+n); int l=0, h=n-1; while(l+1 != h) { int mid = (l + h) / 2; if(check(tab[mid], sums)) h = mid; else l = mid; } for(int i=0; i<n; ++i) { if(inTab[i] <= tab[l]) cout << 'N'; else cout << 'T'; } cout << endl; return 0; } |