#include <iostream> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> using namespace std; using ull = unsigned long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif int main() { int n; scanf("%d", &n); vector<int> fish; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); fish.push_back(x); } // make copy of fish vector<int> val = fish; sort(val.rbegin(), val.rend()); for (int i = 0; i < val.size(); i++) { dbg("%d\t", val[i]); } dbg("\n"); vector<int> needed; needed.resize(n); needed[0] = val[0] + 1; for (int i = 1; i < n; i++) { needed[i] = std::max(val[i] + 1, needed[i - 1] - val[i]); } // print needed for (int i = 0; i < n; i++) { dbg("%d\t", needed[i]); } dbg("\n"); vector<ull> can_grow_to; can_grow_to.resize(n); can_grow_to[n-1] = val[n-1]; for (int i = n-2; i >= 0; i--) { can_grow_to[i] = can_grow_to[i+1] + val[i]; } for (int i = 0; i < n; i++) { dbg("%d\t", can_grow_to[i]); } dbg("\n"); vector<bool> can_be_king; can_be_king.resize(n); can_be_king[0] = true; for (int i = 1; i < n; i++) { can_be_king[i] = can_grow_to[i] >= needed[i-1]; } for (int i = 0; i < n; i++) { dbg("%c\t", can_be_king[i] ? 'T' : 'N'); } dbg("\n"); std::map<int, bool> can_be_king_map; for (int i = 0; i < n; i++) { if (can_be_king_map.count(val[i]) == 0) { can_be_king_map[val[i]] = can_be_king[i]; } } for (auto it = can_be_king_map.begin(); it != can_be_king_map.end(); it++) { dbg("%d: %c\t", it->first, it->second ? 'T' : 'N'); } dbg("\n"); // always smallest fish can't can_be_king_map[val[n-1]] = false; for (int i = 0; i < n; i++) { printf("%c", can_be_king_map[fish[i]] ? 'T' : 'N'); } printf("\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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <iostream> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> using namespace std; using ull = unsigned long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif int main() { int n; scanf("%d", &n); vector<int> fish; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); fish.push_back(x); } // make copy of fish vector<int> val = fish; sort(val.rbegin(), val.rend()); for (int i = 0; i < val.size(); i++) { dbg("%d\t", val[i]); } dbg("\n"); vector<int> needed; needed.resize(n); needed[0] = val[0] + 1; for (int i = 1; i < n; i++) { needed[i] = std::max(val[i] + 1, needed[i - 1] - val[i]); } // print needed for (int i = 0; i < n; i++) { dbg("%d\t", needed[i]); } dbg("\n"); vector<ull> can_grow_to; can_grow_to.resize(n); can_grow_to[n-1] = val[n-1]; for (int i = n-2; i >= 0; i--) { can_grow_to[i] = can_grow_to[i+1] + val[i]; } for (int i = 0; i < n; i++) { dbg("%d\t", can_grow_to[i]); } dbg("\n"); vector<bool> can_be_king; can_be_king.resize(n); can_be_king[0] = true; for (int i = 1; i < n; i++) { can_be_king[i] = can_grow_to[i] >= needed[i-1]; } for (int i = 0; i < n; i++) { dbg("%c\t", can_be_king[i] ? 'T' : 'N'); } dbg("\n"); std::map<int, bool> can_be_king_map; for (int i = 0; i < n; i++) { if (can_be_king_map.count(val[i]) == 0) { can_be_king_map[val[i]] = can_be_king[i]; } } for (auto it = can_be_king_map.begin(); it != can_be_king_map.end(); it++) { dbg("%d: %c\t", it->first, it->second ? 'T' : 'N'); } dbg("\n"); // always smallest fish can't can_be_king_map[val[n-1]] = false; for (int i = 0; i < n; i++) { printf("%c", can_be_king_map[fish[i]] ? 'T' : 'N'); } printf("\n"); return 0; } |