#include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; int64_t n; int m = 0; vector<int64_t> sumy(500001); vector<pair<int64_t, int64_t>> uniq(500001); vector<int64_t> pref(500001); map<int64_t, int64_t> cnt; bool isGood(int med) { int64_t myPref = pref[med]; for (int i = med + 1; i < m; ++i) { if (myPref <= uniq[i].first) { return false; } myPref = pref[i]; } return true; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> sumy[i]; cnt[sumy[i]]++; } int64_t prev = 0; for (auto const& [key, val] : cnt) { uniq[m] = make_pair(key, val); pref[m] = key * val + prev; prev = pref[m]; m++; } if (m == 1) { for (int i = 0; i < n; ++i) { cout << "N"; } return 0; } int L = 0, R = m - 1; while (R - L > 1) { int med = (L + R) / 2; if (isGood(med)) { R = med; } else { L = med; } } // Now R is the last good one! int64_t lastGood = uniq[R].first; for (int i = 0; i < n; ++i) { if (sumy[i] >= lastGood) { cout << "T"; } else { 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 | #include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; int64_t n; int m = 0; vector<int64_t> sumy(500001); vector<pair<int64_t, int64_t>> uniq(500001); vector<int64_t> pref(500001); map<int64_t, int64_t> cnt; bool isGood(int med) { int64_t myPref = pref[med]; for (int i = med + 1; i < m; ++i) { if (myPref <= uniq[i].first) { return false; } myPref = pref[i]; } return true; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> sumy[i]; cnt[sumy[i]]++; } int64_t prev = 0; for (auto const& [key, val] : cnt) { uniq[m] = make_pair(key, val); pref[m] = key * val + prev; prev = pref[m]; m++; } if (m == 1) { for (int i = 0; i < n; ++i) { cout << "N"; } return 0; } int L = 0, R = m - 1; while (R - L > 1) { int med = (L + R) / 2; if (isGood(med)) { R = med; } else { L = med; } } // Now R is the last good one! int64_t lastGood = uniq[R].first; for (int i = 0; i < n; ++i) { if (sumy[i] >= lastGood) { cout << "T"; } else { cout << "N"; } } return 0; } |