#include<cstdlib> #include<cstdio> #include<cstring> #include<cstdint> #include "message.h" #include "teatr.h" const int MAX_SINGLE = 1000; int NODES, MY_ID, N, PARTS, PART_LENGTH, MY_P1, MY_P2, MY_P1_LENGTH; int* MY_SORTED_P1; int* TMP_BUF; int64_t mergesort(int* a, int N) { int rght, wid, rend, i, j, m, t; int64_t swaps = 0; for (int k = 1; k < N; k <<= 1) { for (int left = 0; left + k < N; left += k << 1) { rght = left + k; rend = rght + k; if (rend > N) { rend = N; } m = left; i = left; j = rght; while (i < rght && j < rend) { if (a[i] <= a[j]) { TMP_BUF[m] = a[i]; ++i; } else { TMP_BUF[m] = a[j]; ++j; swaps += rght - i; } ++m; } while (i < rght) { TMP_BUF[m] = a[i]; ++i; ++m; } while (j < rend) { TMP_BUF[m] = a[j]; ++j; ++m; } if (rend - left >= 8) { std::memcpy(a + left, TMP_BUF + left, (rend - left) * sizeof(int)); } else { for (m = left; m < rend; ++m) { a[m] = TMP_BUF[m]; } } } } return swaps; } int64_t sort_my_p1() { int start = PART_LENGTH * MY_P1; int end = start + PART_LENGTH; if (end > N) { end = N; } MY_P1_LENGTH = end - start; MY_SORTED_P1 = (int*)malloc((MY_P1_LENGTH + 8) * sizeof(int)); for (int i = start; i < end; ++i) { MY_SORTED_P1[i - start] = GetElement(i); } return mergesort(MY_SORTED_P1, MY_P1_LENGTH); } int64_t load_my_p2() { int start = PART_LENGTH * MY_P2; int end = start + PART_LENGTH; if (end > N) { end = N; } int len = end - start; int* sorted_p2 = (int*)malloc((len + 8) * sizeof(int)); for (int i = start; i < end; ++i) { sorted_p2[i - start] = GetElement(i); } mergesort(sorted_p2, len); int i = 0; int j = 0; int64_t res = 0; while (i < len && j < MY_P1_LENGTH) { if (sorted_p2[i] <= MY_SORTED_P1[j]) { ++i; } else { ++j; res += len - i; } } return res; } void single() { int* heights = (int*)malloc((N + 8) * sizeof(int)); int ret = 0; for (int i = 0; i < N; ++i) { heights[i] = GetElement(i); for (int j = 0; j < i; ++j) { if (heights[j] > heights[i]) { ++ret; } } } printf("%lld\n", ret); } int main() { NODES = NumberOfNodes(); MY_ID = MyNodeId(); N = GetN(); if (N <= MAX_SINGLE) { if (MY_ID == 0) { single(); } return 0; } PARTS = 0; while (2 + PARTS * (PARTS - 1) <= 2 * NODES) { ++PARTS; } --PARTS; PART_LENGTH = N % PARTS ? (N + PARTS) / PARTS : N / PARTS; if (2 + PARTS * (PARTS - 1) <= 2 * MY_ID) { MY_P1 = -1; MY_P2 = -1; } else if (MY_ID == 0) { MY_P1 = 0; MY_P2 = -1; } else { MY_P1 = 0; MY_P2 = 0; for (int i = 0; i < MY_ID; ++i) { if (MY_P2 == 0) { ++MY_P1; MY_P2 = MY_P1 - 1; } else { --MY_P2; } } } int64_t res = 0; if (MY_P1 >= 0) { TMP_BUF = (int*)malloc((PART_LENGTH + 8) * sizeof(int)); int64_t internal = sort_my_p1(); if (MY_P2 <= 0) { res = internal; } } if (MY_P2 >= 0) { res += load_my_p2(); } if (MY_ID > 0) { PutLL(0, res); Send(0); return 0; } for (int i = 1; i < NODES; ++i) { Receive(i); res += GetLL(i); } printf("%lld\n", res); 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include<cstdlib> #include<cstdio> #include<cstring> #include<cstdint> #include "message.h" #include "teatr.h" const int MAX_SINGLE = 1000; int NODES, MY_ID, N, PARTS, PART_LENGTH, MY_P1, MY_P2, MY_P1_LENGTH; int* MY_SORTED_P1; int* TMP_BUF; int64_t mergesort(int* a, int N) { int rght, wid, rend, i, j, m, t; int64_t swaps = 0; for (int k = 1; k < N; k <<= 1) { for (int left = 0; left + k < N; left += k << 1) { rght = left + k; rend = rght + k; if (rend > N) { rend = N; } m = left; i = left; j = rght; while (i < rght && j < rend) { if (a[i] <= a[j]) { TMP_BUF[m] = a[i]; ++i; } else { TMP_BUF[m] = a[j]; ++j; swaps += rght - i; } ++m; } while (i < rght) { TMP_BUF[m] = a[i]; ++i; ++m; } while (j < rend) { TMP_BUF[m] = a[j]; ++j; ++m; } if (rend - left >= 8) { std::memcpy(a + left, TMP_BUF + left, (rend - left) * sizeof(int)); } else { for (m = left; m < rend; ++m) { a[m] = TMP_BUF[m]; } } } } return swaps; } int64_t sort_my_p1() { int start = PART_LENGTH * MY_P1; int end = start + PART_LENGTH; if (end > N) { end = N; } MY_P1_LENGTH = end - start; MY_SORTED_P1 = (int*)malloc((MY_P1_LENGTH + 8) * sizeof(int)); for (int i = start; i < end; ++i) { MY_SORTED_P1[i - start] = GetElement(i); } return mergesort(MY_SORTED_P1, MY_P1_LENGTH); } int64_t load_my_p2() { int start = PART_LENGTH * MY_P2; int end = start + PART_LENGTH; if (end > N) { end = N; } int len = end - start; int* sorted_p2 = (int*)malloc((len + 8) * sizeof(int)); for (int i = start; i < end; ++i) { sorted_p2[i - start] = GetElement(i); } mergesort(sorted_p2, len); int i = 0; int j = 0; int64_t res = 0; while (i < len && j < MY_P1_LENGTH) { if (sorted_p2[i] <= MY_SORTED_P1[j]) { ++i; } else { ++j; res += len - i; } } return res; } void single() { int* heights = (int*)malloc((N + 8) * sizeof(int)); int ret = 0; for (int i = 0; i < N; ++i) { heights[i] = GetElement(i); for (int j = 0; j < i; ++j) { if (heights[j] > heights[i]) { ++ret; } } } printf("%lld\n", ret); } int main() { NODES = NumberOfNodes(); MY_ID = MyNodeId(); N = GetN(); if (N <= MAX_SINGLE) { if (MY_ID == 0) { single(); } return 0; } PARTS = 0; while (2 + PARTS * (PARTS - 1) <= 2 * NODES) { ++PARTS; } --PARTS; PART_LENGTH = N % PARTS ? (N + PARTS) / PARTS : N / PARTS; if (2 + PARTS * (PARTS - 1) <= 2 * MY_ID) { MY_P1 = -1; MY_P2 = -1; } else if (MY_ID == 0) { MY_P1 = 0; MY_P2 = -1; } else { MY_P1 = 0; MY_P2 = 0; for (int i = 0; i < MY_ID; ++i) { if (MY_P2 == 0) { ++MY_P1; MY_P2 = MY_P1 - 1; } else { --MY_P2; } } } int64_t res = 0; if (MY_P1 >= 0) { TMP_BUF = (int*)malloc((PART_LENGTH + 8) * sizeof(int)); int64_t internal = sort_my_p1(); if (MY_P2 <= 0) { res = internal; } } if (MY_P2 >= 0) { res += load_my_p2(); } if (MY_ID > 0) { PutLL(0, res); Send(0); return 0; } for (int i = 1; i < NODES; ++i) { Receive(i); res += GetLL(i); } printf("%lld\n", res); return 0; } |