#include <iostream> #include <vector> #include <string> #include <algorithm> #include <unordered_map> struct Fish { int weight; int count; unsigned long long histo; bool operator<(Fish const& other) const { return weight<other.weight; } }; int minFish(std::vector<int> const& fishes) { std::unordered_map<int, int> counter; for (auto const& fish : fishes) counter[fish]++; if (counter.size() == 1) for (auto const& [fish, count] : counter) return fish + 1; std::vector<Fish> fishs; fishs.reserve(counter.size()); for (auto const& [fish, count] : counter) { fishs.push_back({fish,count, 0}); } std::sort(fishs.begin(), fishs.end()); for (int i = 1; i< fishs.size(); ++i) fishs[i].histo = fishs[i-1].histo + fishs[i-1].weight * fishs[i-1].count; // for (auto const& fish : fishs) // std::cout << fish.weight << ":" << fish.count << ":" << fish.histo << std::endl; for (int i = fishs.size()-2; i>0; --i) { unsigned long long sum = fishs[i].histo + fishs[i].weight * fishs[i].count; if (fishs[i+1].weight >= sum) return fishs[i+1].weight; } return fishs[1].weight; } int main() { int n; std::cin >> n; std::vector<int> fishes; fishes.reserve(n); for (int i = 0; i<n;++i) { int fish; std::cin >> fish; fishes.push_back(fish); } int smallestFish = minFish(fishes); for (auto const& fish : fishes) { std::cout << (fish>=smallestFish ? 'T' : 'N'); } std::cout << std::endl; 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 | #include <iostream> #include <vector> #include <string> #include <algorithm> #include <unordered_map> struct Fish { int weight; int count; unsigned long long histo; bool operator<(Fish const& other) const { return weight<other.weight; } }; int minFish(std::vector<int> const& fishes) { std::unordered_map<int, int> counter; for (auto const& fish : fishes) counter[fish]++; if (counter.size() == 1) for (auto const& [fish, count] : counter) return fish + 1; std::vector<Fish> fishs; fishs.reserve(counter.size()); for (auto const& [fish, count] : counter) { fishs.push_back({fish,count, 0}); } std::sort(fishs.begin(), fishs.end()); for (int i = 1; i< fishs.size(); ++i) fishs[i].histo = fishs[i-1].histo + fishs[i-1].weight * fishs[i-1].count; // for (auto const& fish : fishs) // std::cout << fish.weight << ":" << fish.count << ":" << fish.histo << std::endl; for (int i = fishs.size()-2; i>0; --i) { unsigned long long sum = fishs[i].histo + fishs[i].weight * fishs[i].count; if (fishs[i+1].weight >= sum) return fishs[i+1].weight; } return fishs[1].weight; } int main() { int n; std::cin >> n; std::vector<int> fishes; fishes.reserve(n); for (int i = 0; i<n;++i) { int fish; std::cin >> fish; fishes.push_back(fish); } int smallestFish = minFish(fishes); for (auto const& fish : fishes) { std::cout << (fish>=smallestFish ? 'T' : 'N'); } std::cout << std::endl; return 0; } |