#include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> using namespace std; /* 6 2 7 1 8 2 8 */ struct Fish { int index; long weight; }; bool compareFishWeight(Fish a, Fish b) { return (a.weight - b.weight) > 0; } bool isAllFishSameWeight(int array[], int n) { for (int i = 0; i < n; i++) { if (array[0] != array[i]) { return false; } } return true; } string sumy(int array[], int n) { if (n == 1) { return "T"; } if (isAllFishSameWeight(array, n)) { char ans[n]; for (int i = 0; i < n; i++) { ans[i] = 'N'; } return string(ans, n); } Fish fish_arr[n]; for (int i = 0; i < n; i++) { fish_arr[i].index = i; fish_arr[i].weight = array[i]; } sort(fish_arr, fish_arr + n, compareFishWeight); long sum_weights[n]; sum_weights[n - 1] = fish_arr[n - 1].weight; long rest = 0; for (int i = n - 2; i >= 0; --i) { if(fish_arr[i].weight > fish_arr[i + 1].weight) { sum_weights[i] = fish_arr[i].weight + sum_weights[i + 1] + rest; rest = 0; } else { rest += fish_arr[i].weight; sum_weights[i] = sum_weights[i + 1]; } } char ans[n]; ans[fish_arr[0].index] = 'T'; ans[fish_arr[n - 1].index] = 'N'; for (int i = 1; i < n; i++) { if (ans[fish_arr[i - 1].index] == 'N') { ans[fish_arr[i].index] = 'N'; } else if (sum_weights[i] > fish_arr[i - 1].weight) { ans[fish_arr[i].index] = 'T'; } else { ans[fish_arr[i].index] = 'N'; } } return string(ans, n); } int main() { int n, i; cin >> n; int array[n]; for (i = 0; i < n; i++) { cin >> array[i]; } cout << sumy(array, n) << 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 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 | #include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> using namespace std; /* 6 2 7 1 8 2 8 */ struct Fish { int index; long weight; }; bool compareFishWeight(Fish a, Fish b) { return (a.weight - b.weight) > 0; } bool isAllFishSameWeight(int array[], int n) { for (int i = 0; i < n; i++) { if (array[0] != array[i]) { return false; } } return true; } string sumy(int array[], int n) { if (n == 1) { return "T"; } if (isAllFishSameWeight(array, n)) { char ans[n]; for (int i = 0; i < n; i++) { ans[i] = 'N'; } return string(ans, n); } Fish fish_arr[n]; for (int i = 0; i < n; i++) { fish_arr[i].index = i; fish_arr[i].weight = array[i]; } sort(fish_arr, fish_arr + n, compareFishWeight); long sum_weights[n]; sum_weights[n - 1] = fish_arr[n - 1].weight; long rest = 0; for (int i = n - 2; i >= 0; --i) { if(fish_arr[i].weight > fish_arr[i + 1].weight) { sum_weights[i] = fish_arr[i].weight + sum_weights[i + 1] + rest; rest = 0; } else { rest += fish_arr[i].weight; sum_weights[i] = sum_weights[i + 1]; } } char ans[n]; ans[fish_arr[0].index] = 'T'; ans[fish_arr[n - 1].index] = 'N'; for (int i = 1; i < n; i++) { if (ans[fish_arr[i - 1].index] == 'N') { ans[fish_arr[i].index] = 'N'; } else if (sum_weights[i] > fish_arr[i - 1].weight) { ans[fish_arr[i].index] = 'T'; } else { ans[fish_arr[i].index] = 'N'; } } return string(ans, n); } int main() { int n, i; cin >> n; int array[n]; for (i = 0; i < n; i++) { cin >> array[i]; } cout << sumy(array, n) << endl; return 0; } |