#include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; struct Pair { int val; int idx; Pair(int _val, int _idx) : val(_val), idx(_idx) { } bool operator<(const Pair &other) const { return (val < other.val || (val == other.val && idx < other.idx)); } }; const int MAX_N = 500000; long long sums[MAX_N]; char res[MAX_N]; //#define IN in #define IN cin int main() { ios_base::sync_with_stdio(0); cin.tie(0); // ifstream in("/Users/bartek/IdeaProjects/fun/ole.txt"); // if(!in.is_open()) { // cout << "ups"; // return 0; // } int n; IN >> n; int val; vector<Pair> vec; for (int i = 0; i < n; i++) { IN >> val; vec.push_back(Pair(val, i)); } sort(vec.begin(), vec.end()); if(vec[0].val == vec[vec.size()-1].val) { for(int i=0; i<n; i++) { cout << 'N'; } cout << endl; } else { sums[0] = vec[0].val; for (int i = 1; i < n; i++) { sums[i] = vec[i].val + sums[i - 1]; } int x = n-1; while(vec[x].val == vec[n-1].val) { res[vec[x].idx] = 'T'; x--; } int lastKing = x + 1; while (x >= 0) { int k = x-1; while(k>=0 && vec[k].val == vec[x].val) { k--; } long long sum; if(k < 0) { sum = vec[x].val; } else { sum = sums[k] + vec[x].val * (long long) (x - k); } if(sum > vec[lastKing].val) { for(int i=k+1; i<=x; i++) { res[vec[i].idx] = 'T'; } lastKing = k+1; } else { for(int i=k+1; i<=x; i++) { res[vec[i].idx] = 'N'; } } x = k; } for(int i=0; i<n; i++) { cout << res[i]; } cout << endl; } }
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 101 102 103 104 105 106 107 108 109 110 | #include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; struct Pair { int val; int idx; Pair(int _val, int _idx) : val(_val), idx(_idx) { } bool operator<(const Pair &other) const { return (val < other.val || (val == other.val && idx < other.idx)); } }; const int MAX_N = 500000; long long sums[MAX_N]; char res[MAX_N]; //#define IN in #define IN cin int main() { ios_base::sync_with_stdio(0); cin.tie(0); // ifstream in("/Users/bartek/IdeaProjects/fun/ole.txt"); // if(!in.is_open()) { // cout << "ups"; // return 0; // } int n; IN >> n; int val; vector<Pair> vec; for (int i = 0; i < n; i++) { IN >> val; vec.push_back(Pair(val, i)); } sort(vec.begin(), vec.end()); if(vec[0].val == vec[vec.size()-1].val) { for(int i=0; i<n; i++) { cout << 'N'; } cout << endl; } else { sums[0] = vec[0].val; for (int i = 1; i < n; i++) { sums[i] = vec[i].val + sums[i - 1]; } int x = n-1; while(vec[x].val == vec[n-1].val) { res[vec[x].idx] = 'T'; x--; } int lastKing = x + 1; while (x >= 0) { int k = x-1; while(k>=0 && vec[k].val == vec[x].val) { k--; } long long sum; if(k < 0) { sum = vec[x].val; } else { sum = sums[k] + vec[x].val * (long long) (x - k); } if(sum > vec[lastKing].val) { for(int i=k+1; i<=x; i++) { res[vec[i].idx] = 'T'; } lastKing = k+1; } else { for(int i=k+1; i<=x; i++) { res[vec[i].idx] = 'N'; } } x = k; } for(int i=0; i<n; i++) { cout << res[i]; } cout << endl; } } |