#include <cstdio> struct fish { long long weight; int pos; fish() {} fish(long long w, int p) { weight = w; pos = p; } }; fish catfish[500500]; fish tmp[505050]; long long sums[500500]; long long min_fish; char ans[500500]; int n; bool compare(int id1, int id2) { if (catfish[id1].weight == catfish[id2].weight) return catfish[id1].pos < catfish[id2].pos; return catfish[id1].weight < catfish[id2].weight; } void sort(int begin, int end) { if (end - begin < 2) return; int mid = (begin + end) / 2; sort(begin, mid); sort(mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (compare(a, b)) tmp[c++] = catfish[a++]; else tmp[c++] = catfish[b++]; } while (a < mid) tmp[c++] = catfish[a++]; while (b < end) tmp[c++] = catfish[b++]; c = 0; while (begin < end) catfish[begin++] = tmp[c++]; } int main() { long long w; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld", &w); catfish[i] = fish(w, i); } sort(0, n); min_fish = sums[0] = catfish[0].weight; for (int i = 1; i < n; ++i) sums[i] = sums[i - 1] + catfish[i].weight; for (int i = 0; i < n; ++i) ans[i] = 'N'; ans[n] = 0; for (int i = n - 1; i >= 0; --i) { if (catfish[i].weight == min_fish || (i < n - 1 && sums[i] <= catfish[i + 1].weight)) break; ans[catfish[i].pos] = 'T'; } printf("%s\n", ans); }
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 | #include <cstdio> struct fish { long long weight; int pos; fish() {} fish(long long w, int p) { weight = w; pos = p; } }; fish catfish[500500]; fish tmp[505050]; long long sums[500500]; long long min_fish; char ans[500500]; int n; bool compare(int id1, int id2) { if (catfish[id1].weight == catfish[id2].weight) return catfish[id1].pos < catfish[id2].pos; return catfish[id1].weight < catfish[id2].weight; } void sort(int begin, int end) { if (end - begin < 2) return; int mid = (begin + end) / 2; sort(begin, mid); sort(mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (compare(a, b)) tmp[c++] = catfish[a++]; else tmp[c++] = catfish[b++]; } while (a < mid) tmp[c++] = catfish[a++]; while (b < end) tmp[c++] = catfish[b++]; c = 0; while (begin < end) catfish[begin++] = tmp[c++]; } int main() { long long w; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld", &w); catfish[i] = fish(w, i); } sort(0, n); min_fish = sums[0] = catfish[0].weight; for (int i = 1; i < n; ++i) sums[i] = sums[i - 1] + catfish[i].weight; for (int i = 0; i < n; ++i) ans[i] = 'N'; ans[n] = 0; for (int i = n - 1; i >= 0; --i) { if (catfish[i].weight == min_fish || (i < n - 1 && sums[i] <= catfish[i + 1].weight)) break; ans[catfish[i].pos] = 'T'; } printf("%s\n", ans); } |