#include<bits/stdc++.h> using namespace std; struct CatFish{ int weight; long long weightSum; bool canBeKing; int previousHighestLowerWeight; long long previousSum; CatFish(int weight){ this->weight = weight; this->weightSum = weight; this->canBeKing = false; this->previousHighestLowerWeight = 0; this->previousSum = 0; } }; bool catFishPriority(CatFish* fst, CatFish* snd){ return fst->weight < snd->weight; } int main(){ int n; cin >> n; vector<CatFish *> catFishes(n), catFishesInOrder(n); for(int i=0; i<n; i++){ int a; cin >> a; CatFish * catFish = new CatFish(a); catFishesInOrder[i]=catFish; catFishes[i]=catFish; } sort(catFishes.begin(), catFishes.end(), catFishPriority); for(int i=1; i<n; i++){ CatFish * currentFish = catFishes[i], * previousFish = catFishes[i-1]; currentFish->previousSum += previousFish->previousSum + previousFish->weight; if(currentFish->weight > previousFish->weight || currentFish->weight + previousFish->previousHighestLowerWeight > previousFish->weight && currentFish->weight > previousFish->previousHighestLowerWeight){ currentFish->weightSum += currentFish->previousSum; } if(currentFish->weight == previousFish->weight) currentFish->previousHighestLowerWeight = previousFish->previousHighestLowerWeight; else currentFish->previousHighestLowerWeight = previousFish->weight; } if(catFishes[n-1]->weightSum > catFishes[n-1]->weight) catFishes[n-1]->canBeKing = true; int iter = n-1; while(iter >= 0 && catFishes[iter]->canBeKing){ if(catFishes[iter-1]->weightSum > catFishes[iter]->weight) catFishes[iter-1]->canBeKing = true; --iter; } for(int i=0; i<n; i++){ if(catFishesInOrder[i]->canBeKing) cout << 'T'; else cout << 'N'; } }
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 | #include<bits/stdc++.h> using namespace std; struct CatFish{ int weight; long long weightSum; bool canBeKing; int previousHighestLowerWeight; long long previousSum; CatFish(int weight){ this->weight = weight; this->weightSum = weight; this->canBeKing = false; this->previousHighestLowerWeight = 0; this->previousSum = 0; } }; bool catFishPriority(CatFish* fst, CatFish* snd){ return fst->weight < snd->weight; } int main(){ int n; cin >> n; vector<CatFish *> catFishes(n), catFishesInOrder(n); for(int i=0; i<n; i++){ int a; cin >> a; CatFish * catFish = new CatFish(a); catFishesInOrder[i]=catFish; catFishes[i]=catFish; } sort(catFishes.begin(), catFishes.end(), catFishPriority); for(int i=1; i<n; i++){ CatFish * currentFish = catFishes[i], * previousFish = catFishes[i-1]; currentFish->previousSum += previousFish->previousSum + previousFish->weight; if(currentFish->weight > previousFish->weight || currentFish->weight + previousFish->previousHighestLowerWeight > previousFish->weight && currentFish->weight > previousFish->previousHighestLowerWeight){ currentFish->weightSum += currentFish->previousSum; } if(currentFish->weight == previousFish->weight) currentFish->previousHighestLowerWeight = previousFish->previousHighestLowerWeight; else currentFish->previousHighestLowerWeight = previousFish->weight; } if(catFishes[n-1]->weightSum > catFishes[n-1]->weight) catFishes[n-1]->canBeKing = true; int iter = n-1; while(iter >= 0 && catFishes[iter]->canBeKing){ if(catFishes[iter-1]->weightSum > catFishes[iter]->weight) catFishes[iter-1]->canBeKing = true; --iter; } for(int i=0; i<n; i++){ if(catFishesInOrder[i]->canBeKing) cout << 'T'; else cout << 'N'; } } |