#include <iostream> #include <algorithm> #include <cmath> #include "message.h" #include "teatr.h" using namespace std; int main() { ios_base::sync_with_stdio(false); long long n = GetN(); long long poczatek = (MyNodeId() * n) / NumberOfNodes(); long long koniec = ((MyNodeId() + 1) * n) / NumberOfNodes(); int *tab = new int[koniec - poczatek]; int maks = 0; for (int i = 0; i < koniec - poczatek; i++) { tab[i] = GetElement(i + poczatek); maks = max(maks, tab[i]); } long long *send = new long long[maks]; long long sum = 0; for (int i = 0; i < maks; i++) { send[i] = 0; } for (int i = 0; i < koniec - poczatek; i++) { sum += send[tab[i] - 1]; for (int j = 0; j < tab[i] - 1; j++) { send[j]++; } } int size = 0; long long *recived; if (MyNodeId() != 0) { Receive(MyNodeId() - 1); size = GetInt(MyNodeId() - 1); sum += GetLL(MyNodeId() - 1); recived = new long long[size]; for (int i = 0; i < size; i++) { recived[i] = GetLL(MyNodeId() - 1); } for (int i = 1; i < min(size, maks); i++) { sum += (send[i - 1] - send[i]) * recived[i]; } } long long *Long; long long *Short; if (maks >= size) { Long = send; Short = recived; } else { Long = recived; Short = send; } for (int i = 0; i < min(size, maks); i++) { Long[i] += Short[i]; } if (MyNodeId() == NumberOfNodes() - 1) { cout << sum << "\n"; } else { PutInt(MyNodeId() + 1, max(size, maks)); PutLL(MyNodeId() + 1, sum); for (int i = 0; i < max(maks, size); i++) PutLL(MyNodeId() + 1, Long[i]); Send(MyNodeId() + 1); } 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 | #include <iostream> #include <algorithm> #include <cmath> #include "message.h" #include "teatr.h" using namespace std; int main() { ios_base::sync_with_stdio(false); long long n = GetN(); long long poczatek = (MyNodeId() * n) / NumberOfNodes(); long long koniec = ((MyNodeId() + 1) * n) / NumberOfNodes(); int *tab = new int[koniec - poczatek]; int maks = 0; for (int i = 0; i < koniec - poczatek; i++) { tab[i] = GetElement(i + poczatek); maks = max(maks, tab[i]); } long long *send = new long long[maks]; long long sum = 0; for (int i = 0; i < maks; i++) { send[i] = 0; } for (int i = 0; i < koniec - poczatek; i++) { sum += send[tab[i] - 1]; for (int j = 0; j < tab[i] - 1; j++) { send[j]++; } } int size = 0; long long *recived; if (MyNodeId() != 0) { Receive(MyNodeId() - 1); size = GetInt(MyNodeId() - 1); sum += GetLL(MyNodeId() - 1); recived = new long long[size]; for (int i = 0; i < size; i++) { recived[i] = GetLL(MyNodeId() - 1); } for (int i = 1; i < min(size, maks); i++) { sum += (send[i - 1] - send[i]) * recived[i]; } } long long *Long; long long *Short; if (maks >= size) { Long = send; Short = recived; } else { Long = recived; Short = send; } for (int i = 0; i < min(size, maks); i++) { Long[i] += Short[i]; } if (MyNodeId() == NumberOfNodes() - 1) { cout << sum << "\n"; } else { PutInt(MyNodeId() + 1, max(size, maks)); PutLL(MyNodeId() + 1, sum); for (int i = 0; i < max(maks, size); i++) PutLL(MyNodeId() + 1, Long[i]); Send(MyNodeId() + 1); } return 0; } |