// Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #include <algorithm> #include "bits/stdc++.h" #define FOR(i, n) for(int i = 0, __n = n; i < __n; i++) using namespace std; const int MINE = 1; const int MAXE = 1000021; int NodeNo; int NodeCount; int A, B; int n; int* val; long long counter[MAXE]; long long larger[MAXE]; long long smaller[MAXE]; long long merger(int* to, int* t1, int m1, int* t2, int m2) { long long tot = 0; int a1 = 0; int a2 = 0; while (a1 + a2 < m1 + m2) { bool first; if (a1 == m1) { first = false; } else if(a2 == m2) { first = true; } else if(t1[a1] > t2[a2]) { first = false; } else { first = true; } if (first) { to[a1 + a2] = t1[a1]; a1++; tot += a2; } else { to[a1 + a2] = t2[a2]; a2++; } } // cerr << "MERGING:" << endl; // for (int i = 0; i<m1; i++) { // cerr << t1[i] << " "; // } // cerr << " with "; // for (int i = 0; i<m2; i++) { // cerr << t2[i] << " "; // } // cerr << " = " << tot << endl; return tot; } long long inversions(int* arr, int m) { long long tot = 0; int* T1 = new int[m]; int* T2 = new int[m]; FOR(i, m) T1[i] = arr[i]; int len = 1; while (len < m) { for (int a = 0; a < m; a += 2 * len) { int alen = min(a + len, m) - a; int b = min(a + len, m); int blen = min(b + len, m) - b; tot += merger(T2 + a, T1 + a, alen, T1 + b, blen); } // cerr << "TOT" << tot << endl; swap(T1, T2); len *= 2; } delete [] T1; delete [] T2; return tot; } int main() { NodeNo = MyNodeId(); NodeCount = NumberOfNodes(); n = GetN(); int space = (n + NodeCount - 1) / NodeCount; A = min(space * NodeNo, n); B = min(space * (NodeNo + 1), n); val = new int[B-A]; long long total = 0; for (int i = A; i < B; i++) { int el = GetElement(i); val[i - A] = el; counter[el]++; } for (int i = 1; i < MAXE; i++) { smaller[i] = smaller[i-1] + counter[i-1]; } for (int i = MAXE-2; i >= 0; i--) { larger[i] = larger[i+1] + counter[i+1]; } for (int i = 0; i < A; i++) { int el = GetElement(i); total += smaller[el]; } // for (int i = B; i < n; i++) { // int el = GetElement(i); // total += larger[el]; // } // cerr << NodeNo << ") " << A << "-" << B << " ext:" << total << endl; total += inversions(val, B - A); // cerr << "INT " << total << endl; // cerr << NodeNo << ") " << A << "-" << B << " " << total << endl; if (NodeNo == 0) { for (int i = 1; i < NodeCount; i++) { Receive(i); total += GetLL(i); } cout << total << endl; } else { PutLL(0, total); Send(0); } delete [] val; }
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 | // Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #include <algorithm> #include "bits/stdc++.h" #define FOR(i, n) for(int i = 0, __n = n; i < __n; i++) using namespace std; const int MINE = 1; const int MAXE = 1000021; int NodeNo; int NodeCount; int A, B; int n; int* val; long long counter[MAXE]; long long larger[MAXE]; long long smaller[MAXE]; long long merger(int* to, int* t1, int m1, int* t2, int m2) { long long tot = 0; int a1 = 0; int a2 = 0; while (a1 + a2 < m1 + m2) { bool first; if (a1 == m1) { first = false; } else if(a2 == m2) { first = true; } else if(t1[a1] > t2[a2]) { first = false; } else { first = true; } if (first) { to[a1 + a2] = t1[a1]; a1++; tot += a2; } else { to[a1 + a2] = t2[a2]; a2++; } } // cerr << "MERGING:" << endl; // for (int i = 0; i<m1; i++) { // cerr << t1[i] << " "; // } // cerr << " with "; // for (int i = 0; i<m2; i++) { // cerr << t2[i] << " "; // } // cerr << " = " << tot << endl; return tot; } long long inversions(int* arr, int m) { long long tot = 0; int* T1 = new int[m]; int* T2 = new int[m]; FOR(i, m) T1[i] = arr[i]; int len = 1; while (len < m) { for (int a = 0; a < m; a += 2 * len) { int alen = min(a + len, m) - a; int b = min(a + len, m); int blen = min(b + len, m) - b; tot += merger(T2 + a, T1 + a, alen, T1 + b, blen); } // cerr << "TOT" << tot << endl; swap(T1, T2); len *= 2; } delete [] T1; delete [] T2; return tot; } int main() { NodeNo = MyNodeId(); NodeCount = NumberOfNodes(); n = GetN(); int space = (n + NodeCount - 1) / NodeCount; A = min(space * NodeNo, n); B = min(space * (NodeNo + 1), n); val = new int[B-A]; long long total = 0; for (int i = A; i < B; i++) { int el = GetElement(i); val[i - A] = el; counter[el]++; } for (int i = 1; i < MAXE; i++) { smaller[i] = smaller[i-1] + counter[i-1]; } for (int i = MAXE-2; i >= 0; i--) { larger[i] = larger[i+1] + counter[i+1]; } for (int i = 0; i < A; i++) { int el = GetElement(i); total += smaller[el]; } // for (int i = B; i < n; i++) { // int el = GetElement(i); // total += larger[el]; // } // cerr << NodeNo << ") " << A << "-" << B << " ext:" << total << endl; total += inversions(val, B - A); // cerr << "INT " << total << endl; // cerr << NodeNo << ") " << A << "-" << B << " " << total << endl; if (NodeNo == 0) { for (int i = 1; i < NodeCount; i++) { Receive(i); total += GetLL(i); } cout << total << endl; } else { PutLL(0, total); Send(0); } delete [] val; } |