// Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #pragma warning(disable:4786) #pragma warning(disable:4996) #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) //typedef int LL; typedef long long int LL; //typedef __int128 LL; //typedef __int64 LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<long double, long double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII > VP; typedef vector<double> VD; typedef vector<LL> VL; typedef long double ld; int my_node, total_node; int n; VP P; #define MaxVal 1000005 int tree[1000066]; int read(int idx) { int sum = 0; while (idx > 0) { sum += tree[idx]; idx -= (idx & -idx); } return sum; } //add val to f[idx]. //MaxVal = Just max value of index void update(int idx, int val) { while (idx <= MaxVal) { tree[idx] += val; idx += (idx & -idx); } } PII SelectPair() { int now = my_node; for (int i = 0; i < SZ(P); i++) { for (int j = i; j < SZ(P); j++) { if (now == 0) return { i, j }; now--; } } return { -1, -1 }; } int main() { my_node = MyNodeId(); total_node = NumberOfNodes(); n = GetN(); if (n < 1000) { if (my_node) return 0; VI V(n); for (int i = 0; i < n; i++) V[i] = GetElement(i); int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans += (V[i] > V[j]); } } printf("%d\n", ans); return 0; } int part = 1; if (total_node == 100) part = 13; else if (total_node == 10) part = 4; for (int i = 0; i < part; i++) { int len = (n + part - 1) / part; P.push_back({ i * len, (i + 1) * len - 1 }); P.back().second = MIN(P.back().second, n - 1); } PII p = SelectPair(); if (p.first == -1) { PutLL(0, 0); Send(0); return 0; } LL ans = 0; if (p.first == p.second) { // Single node. int x = p.first; for (int i = P[x].second; i >= P[x].first; i--) { int num = GetElement(i); ans += read(num - 1); update(num, 1); } } else { // Double node. int y = p.first; int x = p.second; for (int i = P[x].first; i <= P[x].second; i++) { int num = GetElement(i); update(num, 1); } for (int i = P[y].first; i <= P[y].second; i++) { int num = GetElement(i); ans += read(num - 1); } } PutLL(0, ans); Send(0); if (my_node) return 0; ans = 0; for (int i = 0; i < total_node; i++) { Receive(i); ans += GetLL(i); } printf("%lld\n", ans); 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 174 175 176 | // Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #pragma warning(disable:4786) #pragma warning(disable:4996) #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) //typedef int LL; typedef long long int LL; //typedef __int128 LL; //typedef __int64 LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<long double, long double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII > VP; typedef vector<double> VD; typedef vector<LL> VL; typedef long double ld; int my_node, total_node; int n; VP P; #define MaxVal 1000005 int tree[1000066]; int read(int idx) { int sum = 0; while (idx > 0) { sum += tree[idx]; idx -= (idx & -idx); } return sum; } //add val to f[idx]. //MaxVal = Just max value of index void update(int idx, int val) { while (idx <= MaxVal) { tree[idx] += val; idx += (idx & -idx); } } PII SelectPair() { int now = my_node; for (int i = 0; i < SZ(P); i++) { for (int j = i; j < SZ(P); j++) { if (now == 0) return { i, j }; now--; } } return { -1, -1 }; } int main() { my_node = MyNodeId(); total_node = NumberOfNodes(); n = GetN(); if (n < 1000) { if (my_node) return 0; VI V(n); for (int i = 0; i < n; i++) V[i] = GetElement(i); int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans += (V[i] > V[j]); } } printf("%d\n", ans); return 0; } int part = 1; if (total_node == 100) part = 13; else if (total_node == 10) part = 4; for (int i = 0; i < part; i++) { int len = (n + part - 1) / part; P.push_back({ i * len, (i + 1) * len - 1 }); P.back().second = MIN(P.back().second, n - 1); } PII p = SelectPair(); if (p.first == -1) { PutLL(0, 0); Send(0); return 0; } LL ans = 0; if (p.first == p.second) { // Single node. int x = p.first; for (int i = P[x].second; i >= P[x].first; i--) { int num = GetElement(i); ans += read(num - 1); update(num, 1); } } else { // Double node. int y = p.first; int x = p.second; for (int i = P[x].first; i <= P[x].second; i++) { int num = GetElement(i); update(num, 1); } for (int i = P[y].first; i <= P[y].second; i++) { int num = GetElement(i); ans += read(num - 1); } } PutLL(0, ans); Send(0); if (my_node) return 0; ans = 0; for (int i = 0; i < total_node; i++) { Receive(i); ans += GetLL(i); } printf("%lld\n", ans); return 0; } |