// Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #include "bits/stdc++.h" #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <algorithm> #include <queue> #include <list> #include <string> #include <vector> #include <unistd.h> using namespace std; #if 0 #define MYDEBUG_ANY(f, ...) fprintf(stderr, ("L%03u - %s() " f), __LINE__, __FUNCTION__, ##__VA_ARGS__) #define MYDEBUG_ID(id, f, ...) if(nodeId==(id)) { fprintf(stderr, ("L%03u - %s() " f), __LINE__, __FUNCTION__, ##__VA_ARGS__) } #else #define MYDEBUG_ANY(f, ...) #define MYDEBUG_ID(id, f, ...) #endif using u8 = unsigned char; using u32 = unsigned int; using u64 = unsigned long long; typedef enum MsgType { MsgExit, MsgGroups } MsgType; int nodes; int nodeId; int n; u64 result; u8 maxGroup = 0; struct People { u32 height; u32 number; People(u32 h, u32 n) : height(h), number(n) {} }; std::list<People> peoples; u32 merge(u32 height, u32 number) { u32 listSize = 0; if(peoples.empty()) { peoples.emplace_front(height, number); ++listSize; } else { auto it = peoples.begin(); if(height > it->height) { peoples.emplace_front(height, number); ++listSize; } else if(height == it->height) { it->number += number; } else { result += it->number * number; for(it++; it != peoples.end(); it++) { if(height > it->height) { peoples.emplace(it, height, number); ++listSize; break; } else if(height == it->height) { it->number++; break; } result += it->number * number; } if(it == peoples.end()) { peoples.emplace_back(height, 1); ++listSize; } } } return listSize; } bool createGroups() { const u32 groupSize = 1 + n / nodes; const u32 first = nodeId * groupSize; const u32 last = std::min<u32>(n, first + groupSize); u32 listSize = 0; MYDEBUG_ANY("first %u last %u\n", first, last); if(first >= n) { // stop if not enought rows for this node PutChar(0, MsgGroups); PutLL(0, result); PutInt(0, listSize); // listSize Send(0); //MYDEBUG_ANY("not enought rows - creatGroups() stopped"); return false; } for(u32 k=first; k<last; ++k) { u32 h = GetElement(k); listSize += merge(h, 1); } if(0 == nodeId) { if(last == n) { return false; } } else { PutChar(0, MsgGroups); PutLL(0, result); PutInt(0, listSize); for(auto it = peoples.crbegin(); it != peoples.crend(); it++) { PutInt(0, it->height); PutInt(0, it->number); MYDEBUG_ANY("sending h %u n %u (result %llu)\n", it->height, it->number, result); } Send(0); } return true; } bool mergeGroups(const int source) { static bool rcvd[100] = {}; static std::vector<std::pair<u32,u32> > h_n[100]; u64 res = GetLL(source); result += res; const u32 listSize = GetInt(source); //MYDEBUG_ANY("received groups from source=%d res=%llu listSize=%u\n", source, res, listSize); h_n[source].reserve(listSize); for(u32 i=0; i<listSize; ++i) { u32 h = GetInt(source); u32 n = GetInt(source); h_n[source].emplace_back(h,n); } rcvd[source] = true; for(int i=1; i<nodes; ++i) { if(false == rcvd[i]) { //MYDEBUG_ANY("still not rcvd from %d - break merging\n", i); return true; } } for(int i=1; i<nodes; ++i) { const auto &vec = h_n[i]; for(const auto &p : vec) { MYDEBUG_ANY("merge from source %d HEI %u NUM %u\n", i, p.first, p.second); merge(p.first, p.second); } } return false; } bool processMsg() { for(;;) { const int source = Receive(-1); switch(static_cast<MsgType>(GetChar(source))) { case MsgGroups: return mergeGroups(source); case MsgExit: return false; default: break; } } } int main() { nodes = NumberOfNodes(); nodeId = MyNodeId(); n = GetN(); bool process = createGroups(); if(nodeId == 0) { while(process) { process = processMsg(); } for(int i=1; i<nodes; ++i) { PutChar(i, MsgExit); Send(i); } cout << result << "\n"; } else { processMsg(); } }
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 | // Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #include "bits/stdc++.h" #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <algorithm> #include <queue> #include <list> #include <string> #include <vector> #include <unistd.h> using namespace std; #if 0 #define MYDEBUG_ANY(f, ...) fprintf(stderr, ("L%03u - %s() " f), __LINE__, __FUNCTION__, ##__VA_ARGS__) #define MYDEBUG_ID(id, f, ...) if(nodeId==(id)) { fprintf(stderr, ("L%03u - %s() " f), __LINE__, __FUNCTION__, ##__VA_ARGS__) } #else #define MYDEBUG_ANY(f, ...) #define MYDEBUG_ID(id, f, ...) #endif using u8 = unsigned char; using u32 = unsigned int; using u64 = unsigned long long; typedef enum MsgType { MsgExit, MsgGroups } MsgType; int nodes; int nodeId; int n; u64 result; u8 maxGroup = 0; struct People { u32 height; u32 number; People(u32 h, u32 n) : height(h), number(n) {} }; std::list<People> peoples; u32 merge(u32 height, u32 number) { u32 listSize = 0; if(peoples.empty()) { peoples.emplace_front(height, number); ++listSize; } else { auto it = peoples.begin(); if(height > it->height) { peoples.emplace_front(height, number); ++listSize; } else if(height == it->height) { it->number += number; } else { result += it->number * number; for(it++; it != peoples.end(); it++) { if(height > it->height) { peoples.emplace(it, height, number); ++listSize; break; } else if(height == it->height) { it->number++; break; } result += it->number * number; } if(it == peoples.end()) { peoples.emplace_back(height, 1); ++listSize; } } } return listSize; } bool createGroups() { const u32 groupSize = 1 + n / nodes; const u32 first = nodeId * groupSize; const u32 last = std::min<u32>(n, first + groupSize); u32 listSize = 0; MYDEBUG_ANY("first %u last %u\n", first, last); if(first >= n) { // stop if not enought rows for this node PutChar(0, MsgGroups); PutLL(0, result); PutInt(0, listSize); // listSize Send(0); //MYDEBUG_ANY("not enought rows - creatGroups() stopped"); return false; } for(u32 k=first; k<last; ++k) { u32 h = GetElement(k); listSize += merge(h, 1); } if(0 == nodeId) { if(last == n) { return false; } } else { PutChar(0, MsgGroups); PutLL(0, result); PutInt(0, listSize); for(auto it = peoples.crbegin(); it != peoples.crend(); it++) { PutInt(0, it->height); PutInt(0, it->number); MYDEBUG_ANY("sending h %u n %u (result %llu)\n", it->height, it->number, result); } Send(0); } return true; } bool mergeGroups(const int source) { static bool rcvd[100] = {}; static std::vector<std::pair<u32,u32> > h_n[100]; u64 res = GetLL(source); result += res; const u32 listSize = GetInt(source); //MYDEBUG_ANY("received groups from source=%d res=%llu listSize=%u\n", source, res, listSize); h_n[source].reserve(listSize); for(u32 i=0; i<listSize; ++i) { u32 h = GetInt(source); u32 n = GetInt(source); h_n[source].emplace_back(h,n); } rcvd[source] = true; for(int i=1; i<nodes; ++i) { if(false == rcvd[i]) { //MYDEBUG_ANY("still not rcvd from %d - break merging\n", i); return true; } } for(int i=1; i<nodes; ++i) { const auto &vec = h_n[i]; for(const auto &p : vec) { MYDEBUG_ANY("merge from source %d HEI %u NUM %u\n", i, p.first, p.second); merge(p.first, p.second); } } return false; } bool processMsg() { for(;;) { const int source = Receive(-1); switch(static_cast<MsgType>(GetChar(source))) { case MsgGroups: return mergeGroups(source); case MsgExit: return false; default: break; } } } int main() { nodes = NumberOfNodes(); nodeId = MyNodeId(); n = GetN(); bool process = createGroups(); if(nodeId == 0) { while(process) { process = processMsg(); } for(int i=1; i<nodes; ++i) { PutChar(i, MsgExit); Send(i); } cout << result << "\n"; } else { processMsg(); } } |