#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Fish{ int w, id; bool king; }; struct FishBlock{ int w; vector<int>ids; }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Fish>V; for(int i=0;i<n;i++){ int w; cin >> w; V.push_back(Fish{.w=w, .id=i, .king=false}); } sort(V.begin(), V.end(), [](const Fish& f1, const Fish& f2){return f1.w<f2.w;}); //sort po wagach V.push_back(Fish{.w=V[n-1].w}); vector<long long>Pref(n); Pref[0] = V[0].w; for(int i=1;i<n;i++) Pref[i] = Pref[i-1] + V[i].w; int last_bigger_pos = n; for (int i = n - 1; i >= 0; i--) { // cerr << "i=" << i << " size=" << V[i].w << endl;; if(V[i+1].w > V[i].w) last_bigger_pos = i+1; // cerr << "\tlbp=" << last_bigger_pos << endl; //if exists smaller if (V[0].w < V[i].w) { long long init_weight = Pref[last_bigger_pos-1]; V[i].king = init_weight > V[last_bigger_pos].w; if (!V[i].king) break; } else break; } V.pop_back(); sort(V.begin(), V.end(), [](const Fish& f1, const Fish& f2){return f1.id<f2.id;}); for(Fish f : V) if(f.id != -1) cout << (f.king?'T':'N'); 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Fish{ int w, id; bool king; }; struct FishBlock{ int w; vector<int>ids; }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Fish>V; for(int i=0;i<n;i++){ int w; cin >> w; V.push_back(Fish{.w=w, .id=i, .king=false}); } sort(V.begin(), V.end(), [](const Fish& f1, const Fish& f2){return f1.w<f2.w;}); //sort po wagach V.push_back(Fish{.w=V[n-1].w}); vector<long long>Pref(n); Pref[0] = V[0].w; for(int i=1;i<n;i++) Pref[i] = Pref[i-1] + V[i].w; int last_bigger_pos = n; for (int i = n - 1; i >= 0; i--) { // cerr << "i=" << i << " size=" << V[i].w << endl;; if(V[i+1].w > V[i].w) last_bigger_pos = i+1; // cerr << "\tlbp=" << last_bigger_pos << endl; //if exists smaller if (V[0].w < V[i].w) { long long init_weight = Pref[last_bigger_pos-1]; V[i].king = init_weight > V[last_bigger_pos].w; if (!V[i].king) break; } else break; } V.pop_back(); sort(V.begin(), V.end(), [](const Fish& f1, const Fish& f2){return f1.id<f2.id;}); for(Fish f : V) if(f.id != -1) cout << (f.king?'T':'N'); cout << "\n"; return 0; } |