#include "skup.h" #include "message.h" #undef PutInt #undef PutLL #include <bits/stdc++.h> #include <unordered_map> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } //#ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) //#else //#define debug(...) (__VA_ARGS__) //#endif inline int top(int a, int b) { return (a + b - 1) / b; } int nodes = NumberOfNodes(); int myId = MyNodeId(); int companies = NumberOfCompanies(); VI myCompanies; const int chunk = 15000; const int big = nodes * 1001 + 20; void _sendVector(VI dupcia, int dest) { assert(SZ(dupcia) == big); int sz = SZ(dupcia); for (int i = 0; i < sz; i += chunk) { for (int j = i; j < i + chunk && j < sz; j++) { PutInt(dest, dupcia[j]); } Send(dest); } } VI _receiveVector(int src, int sz) { assert(sz == big); VI res; for (int i = 0; i < sz; i += chunk) { Receive(src); for (int j = i; j < i + chunk && j < sz; j++) { res.push_back(GetInt(src)); } } return res; } void sendVector(VI dupcia, int dest) { VI myVector; myVector.push_back(SZ(dupcia)); myVector.insert(myVector.end(), ALL(dupcia)); myVector.resize(big); _sendVector(myVector, dest); } VI receiveVector(int src) { VI res = _receiveVector(src, big); int idx = 0; VI dupsko; bool found = false; int sz = INT_MAX; for (int i = 0; i < sz; i += chunk) { if (!found) { found = true; sz = res[idx++]; } for (int j = i; j < i + chunk && j < sz; j++) { dupsko.push_back(res[idx++]); } } return dupsko; } unordered_map<int, int> dupsko; struct companyInfo { int country; VI prices; void addToBuffer(VI & acc) { acc.push_back(SZ(prices)); REP(i, SZ(prices)) { acc.push_back(prices[i]); } acc.push_back(country); } companyInfo() { country = myId; prices = myCompanies; } companyInfo(VI & acc, int & idx) { prices.resize(acc[idx++]); REP(i, SZ(prices)) { prices[i] = acc[idx++]; } country = acc[idx++]; } bool valid() { return !prices.empty(); } bool operator<(const companyInfo & rhs) const { return country < rhs.country; } }; struct info1 { set<companyInfo> sentInfo; set<companyInfo> notSent; int gotAll; info1() { notSent.insert(companyInfo()); gotAll = 0; } void broadcastInfo() { VI message; message.push_back(SZ(notSent)); for (companyInfo a : notSent) { a.addToBuffer(message); sentInfo.insert(a); } notSent.clear(); if (sentInfo.size() == nodes) { gotAll |= 1 << myId; } message.push_back(gotAll); REP(i, nodes) { if (i == myId) continue; sendVector(message, i); } } void broadcastInfo2() { REP(i, nodes) { if (i == myId) { continue; } PutInt(i, gotAll); Send(i); } } void receiveInfo2() { REP(i, nodes) { if (i == myId) { continue; } Receive(i); gotAll |= GetInt(i); } } void receiveInfo() { REP(i, nodes) { if (i == myId) continue; VI acc = receiveVector(i); if (acc.empty()) { continue; } int idx = 0; int sz = acc[idx++]; REP(j, sz) { companyInfo a(acc, idx); if (!sentInfo.count(a)) { notSent.insert(a); } } gotAll |= acc[idx++]; } } LL getResult() { vector<int> acc; for (companyInfo c : sentInfo) { acc.insert(acc.end(), ALL(c.prices)); } sort(ALL(acc), greater<int>()); LL res = 0; REP(i, SZ(acc)) { res += (LL)(i + 1) * acc[i]; } return res; } void printResult() { if (SZ(sentInfo) != nodes) return; REP(i, nodes) { if (gotAll & (1 << i)) { if (i == myId) { cout << getResult() << "\n"; } return; } } } }; int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); myCompanies.resize(companies); REP(i, companies) { myCompanies[i] = GetShareCost(i); } info1 myInfo; REP(i, nodes) { myInfo.broadcastInfo(); myInfo.receiveInfo(); } REP(j, nodes) { myInfo.broadcastInfo2(); myInfo.receiveInfo2(); } myInfo.printResult(); 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | #include "skup.h" #include "message.h" #undef PutInt #undef PutLL #include <bits/stdc++.h> #include <unordered_map> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } //#ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) //#else //#define debug(...) (__VA_ARGS__) //#endif inline int top(int a, int b) { return (a + b - 1) / b; } int nodes = NumberOfNodes(); int myId = MyNodeId(); int companies = NumberOfCompanies(); VI myCompanies; const int chunk = 15000; const int big = nodes * 1001 + 20; void _sendVector(VI dupcia, int dest) { assert(SZ(dupcia) == big); int sz = SZ(dupcia); for (int i = 0; i < sz; i += chunk) { for (int j = i; j < i + chunk && j < sz; j++) { PutInt(dest, dupcia[j]); } Send(dest); } } VI _receiveVector(int src, int sz) { assert(sz == big); VI res; for (int i = 0; i < sz; i += chunk) { Receive(src); for (int j = i; j < i + chunk && j < sz; j++) { res.push_back(GetInt(src)); } } return res; } void sendVector(VI dupcia, int dest) { VI myVector; myVector.push_back(SZ(dupcia)); myVector.insert(myVector.end(), ALL(dupcia)); myVector.resize(big); _sendVector(myVector, dest); } VI receiveVector(int src) { VI res = _receiveVector(src, big); int idx = 0; VI dupsko; bool found = false; int sz = INT_MAX; for (int i = 0; i < sz; i += chunk) { if (!found) { found = true; sz = res[idx++]; } for (int j = i; j < i + chunk && j < sz; j++) { dupsko.push_back(res[idx++]); } } return dupsko; } unordered_map<int, int> dupsko; struct companyInfo { int country; VI prices; void addToBuffer(VI & acc) { acc.push_back(SZ(prices)); REP(i, SZ(prices)) { acc.push_back(prices[i]); } acc.push_back(country); } companyInfo() { country = myId; prices = myCompanies; } companyInfo(VI & acc, int & idx) { prices.resize(acc[idx++]); REP(i, SZ(prices)) { prices[i] = acc[idx++]; } country = acc[idx++]; } bool valid() { return !prices.empty(); } bool operator<(const companyInfo & rhs) const { return country < rhs.country; } }; struct info1 { set<companyInfo> sentInfo; set<companyInfo> notSent; int gotAll; info1() { notSent.insert(companyInfo()); gotAll = 0; } void broadcastInfo() { VI message; message.push_back(SZ(notSent)); for (companyInfo a : notSent) { a.addToBuffer(message); sentInfo.insert(a); } notSent.clear(); if (sentInfo.size() == nodes) { gotAll |= 1 << myId; } message.push_back(gotAll); REP(i, nodes) { if (i == myId) continue; sendVector(message, i); } } void broadcastInfo2() { REP(i, nodes) { if (i == myId) { continue; } PutInt(i, gotAll); Send(i); } } void receiveInfo2() { REP(i, nodes) { if (i == myId) { continue; } Receive(i); gotAll |= GetInt(i); } } void receiveInfo() { REP(i, nodes) { if (i == myId) continue; VI acc = receiveVector(i); if (acc.empty()) { continue; } int idx = 0; int sz = acc[idx++]; REP(j, sz) { companyInfo a(acc, idx); if (!sentInfo.count(a)) { notSent.insert(a); } } gotAll |= acc[idx++]; } } LL getResult() { vector<int> acc; for (companyInfo c : sentInfo) { acc.insert(acc.end(), ALL(c.prices)); } sort(ALL(acc), greater<int>()); LL res = 0; REP(i, SZ(acc)) { res += (LL)(i + 1) * acc[i]; } return res; } void printResult() { if (SZ(sentInfo) != nodes) return; REP(i, nodes) { if (gotAll & (1 << i)) { if (i == myId) { cout << getResult() << "\n"; } return; } } } }; int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); myCompanies.resize(companies); REP(i, companies) { myCompanies[i] = GetShareCost(i); } info1 myInfo; REP(i, nodes) { myInfo.broadcastInfo(); myInfo.receiveInfo(); } REP(j, nodes) { myInfo.broadcastInfo2(); myInfo.receiveInfo2(); } myInfo.printResult(); return 0; } |