//Maciej Poleski #ifdef DEBUG #define _GLIBCXX_CONCEPT_CHECKS #include <iostream> #include <fstream> #include <cstdlib> #include <cassert> namespace { namespace Wrapper { std::ifstream in; std::ofstream out; } void init(int argc, char **argv) { if(argc != 3) { std::cerr << "Potrzeba dokładnie dwóch argumentów\n"; std::abort(); } Wrapper::in.open(argv[1]); Wrapper::out.open(argv[2]); } } #define check(x) assert(x) #else #ifndef NDEBUG #define NDEBUG #endif #define check(x) #include <iostream> namespace { namespace Wrapper { std::istream &in = std::cin; std::ostream &out = std::cout; } } #endif #include <cstdint> namespace { namespace Wrapper { typedef std::uint_fast64_t uint_fast64_t; typedef std::uint_fast32_t uint_fast32_t; typedef std::uint_fast16_t uint_fast16_t; typedef std::uint_fast8_t uint_fast8_t; typedef std::uint64_t uint64_t; typedef std::uint32_t uint32_t; typedef std::uint16_t uint16_t; typedef std::uint8_t uint8_t; typedef std::int_fast64_t int_fast64_t; typedef std::int_fast32_t int_fast32_t; typedef std::int_fast16_t int_fast16_t; typedef std::int_fast8_t int_fast8_t; typedef std::int64_t int64_t; typedef std::int32_t int32_t; typedef std::int16_t int16_t; typedef std::int8_t int8_t; typedef std::size_t size_t; } } #include <string> #include <algorithm> #include <limits> #include <locale> #include <cstring> #include <utility> #include <cstdlib> #include <random> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <list> #include <iomanip> #include <set> #include <map> #include <memory> #include <functional> #include <unordered_map> #include <unordered_set> #include <complex> #include <type_traits> #include "message.h" namespace { using namespace Wrapper; // Numeruje od 0 static std::pair<uint_fast32_t, uint_fast32_t> getXY(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny) { uint_fast32_t result = 0; uint_fast32_t rx = 0, ry = 0; while(result < n) { if(nx >= ny) { if(ry < ny - 1) { result += ry + 2; ry += 1; } else { result += std::min(ny, nx - rx - 1); rx += 1; } } else { if(ry < nx - 1) { result += ry + 2; ry += 1; } else if(ry < ny - 1) { result += nx; ry += 1; } else { result += nx - rx - 1; rx += 1; } } } uint_fast32_t diff = result - n; rx += diff; ry -= diff; return std::make_pair(rx, ry); } static uint_fast32_t getRightNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + std::min(x, n - ny - 1) + std::min(y + 1, nx - x - 1); } static uint_fast32_t getBottomNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + std::min(x, n - ny - 1) + std::min(y + 1, nx - x - 1) + 1; } static int_fast32_t getTopNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n - (std::min(nx - x, y + 1) + std::min(x, ny - y)); } static int_fast32_t getLeftNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + 1 - (std::min(nx - x, y + 1) + std::min(x, ny - y)); } static constexpr uint_fast32_t tasksPerNode = 500; // Sprawdzić limit trasmisji danych static_assert(tasksPerNode <= 500, "Węzeł może wysłać 1000 wiadomości, zadanie wymaga 2"); static const auto numberOfNodes = NumberOfNodes(); static const auto myNodeId = MyNodeId(); struct Info { uint_fast32_t max; uint_fast32_t arg; }; static Info bestInfo(const Info &lhs, const Info &rhs) { if((lhs.max < rhs.max) || ((lhs.max == rhs.max) && (lhs.arg < rhs.arg))) { return lhs; } return rhs; } static std::unordered_map<uint_fast32_t, Info*> top_cache; void receiveFromTop(uint_fast32_t node, Info *&result) { while(top_cache.find(node) == top_cache.end()) { const auto n=Receive(node%numberOfNodes); const uint_fast32_t rn=GetInt(n); const uint_fast32_t size=GetInt(n); Info *vec=new Info[size]; for(uint_fast32_t i=0;i<size;++i) { Info ii; ii.max=GetInt(n); ii.arg=GetInt(n); vec[i]=ii; } top_cache[rn]=vec; } delete [] result; result = top_cache[node]; top_cache.erase(node); } static std::unordered_map<uint_fast32_t, Info*> left_cache; void receiveFromLeft(uint_fast32_t node, Info *&result) { while(left_cache.find(node) == left_cache.end()) { const auto n=Receive(node%numberOfNodes); const uint_fast32_t rn=GetInt(n); const uint_fast32_t size=GetInt(n); Info *vec=new Info[size]; for(uint_fast32_t i=0;i<size;++i) { Info ii; ii.max=GetInt(n); ii.arg=GetInt(n); vec[i]=ii; } left_cache[rn]=vec; } delete [] result; result = left_cache[node]; left_cache.erase(node); } inline static void solution() { using std::swap; // int nx=3; // int ny=5; // for(int y=0;y<ny;++y) // { // for(int x=0;x<nx;++x) // { // out<<getTopNode(0,nx,ny,x,y)<<' '; // } // out<<'\n'; // } uint_fast32_t n, m; in >> n >> m; char *stringA = new char[n + 2]; char *stringB = new char[m + 2]; in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in.read(stringA + 1, n); in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in.read(stringB + 1, m); stringA[n + 1] = '\0'; stringB[m + 1] = '\0'; const uint_fast32_t taskPerDimHint = std::sqrt(tasksPerNode * numberOfNodes); const uint_fast32_t taskSizeForDimA = std::max<uint_fast32_t>(1, n / taskPerDimHint + !!(n % taskPerDimHint)); const uint_fast32_t taskSizeForDimB = std::max<uint_fast32_t>(1, m / taskPerDimHint + !!(m % taskPerDimHint)); const uint_fast32_t dimASize = n / taskSizeForDimA + !!(n % taskSizeForDimA); const uint_fast32_t dimBSize = m / taskSizeForDimB + !!(m % taskSizeForDimB); for(uint_fast32_t id = myNodeId;; id += numberOfNodes) { if(id >= dimASize * dimBSize) { break; // Grid został obliczony } auto p = getXY(id, dimASize, dimBSize); const uint_fast32_t x = p.first; const uint_fast32_t y = p.second; const char *aBegin = stringA + 1 + taskSizeForDimA * x; const char *aEnd = std::min<const char *>(stringA + 1 + n, aBegin + taskSizeForDimA); const char *bBegin = stringB + 1 + taskSizeForDimB * y; const char *bEnd = std::min<const char *>(stringB + 1 + m, bBegin + taskSizeForDimB); Info *aInfo = new Info[taskSizeForDimA + 1]; Info *bInfo = new Info[taskSizeForDimB]; Info *prevAInfo = new Info[taskSizeForDimA + 1]; Info *resultBInfo = new Info[taskSizeForDimB]; // Być może przycięte na brzegu if(y == 0) { uint_fast32_t i = aBegin - stringA - 1; for(auto ptr = prevAInfo, e = prevAInfo + (aEnd - aBegin) + 1; ptr != e; ++ptr) { ptr->max = i++; ptr->arg = 0; } } else { const uint_fast32_t topNode = getTopNode(id, dimASize, dimBSize, x, y); receiveFromTop(topNode,prevAInfo); } if(x == 0) { uint_fast32_t i = bBegin - stringB; for(auto ptr = bInfo, e = bInfo + (bEnd - bBegin); ptr != e; ++ptr) { ptr->max = i++; ptr->arg = 0; } } else { const uint_fast32_t leftNode = getLeftNode(id, dimASize, dimBSize, x, y); receiveFromLeft(leftNode,bInfo); } // Klasyczna odległość edycyjna: auto bInfoPtr = bInfo; auto bInfoResultPtr = resultBInfo; for(auto bPtr = bBegin; bPtr != bEnd; ++bPtr) { uint_fast32_t aInfoIdx = 0; aInfo[aInfoIdx++] = *bInfoPtr++; for(auto aPtr = aBegin; aPtr != aEnd; ++aPtr) { if(*bPtr == *aPtr) { aInfo[aInfoIdx] = prevAInfo[aInfoIdx - 1]; } else { Info best = prevAInfo[aInfoIdx]; best.max += 1; Info temp = aInfo[aInfoIdx - 1]; temp.max += 1; best = bestInfo(best, temp); temp = prevAInfo[aInfoIdx - 1]; temp.max += 1; if(*aPtr < *bPtr) { temp.arg += 1; // Załamanie nerwowe } best = bestInfo(best, temp); aInfo[aInfoIdx] = best; } aInfoIdx += 1; } *bInfoResultPtr++ = aInfo[aInfoIdx - 1]; swap(aInfo, prevAInfo); } // Ostatni wiersz jest w prevAInfo if(x < dimASize - 1) { // Przesyłam dalej const uint_fast32_t rightNode = getRightNode(id, dimASize, dimBSize, x, y); const auto node = rightNode % numberOfNodes; PutInt(node, id); PutInt(node,(bEnd - bBegin)); for(auto ptr = resultBInfo, e = resultBInfo + (bEnd - bBegin); ptr != e; ++ptr) { PutInt(node, ptr->max); PutInt(node, ptr->arg); } Send(node); } if(y < dimBSize - 1) { const uint_fast32_t bottomNode = getBottomNode(id, dimASize, dimBSize, x, y); const auto node = bottomNode % numberOfNodes; PutInt(node, id); PutInt(node,(aEnd - aBegin) + 1); for(auto ptr = prevAInfo, e = prevAInfo + (aEnd - aBegin) + 1; ptr != e; ++ptr) { PutInt(node, ptr->max); PutInt(node, ptr->arg); } Send(node); } if((x == dimASize - 1) && (y == dimBSize - 1)) { const auto lastIdx = aEnd - aBegin; out << prevAInfo[lastIdx].max << ' ' << prevAInfo[lastIdx].arg << '\n'; } delete [] resultBInfo; delete [] prevAInfo; delete [] bInfo; delete [] aInfo; } delete [] stringB; delete [] stringA; } } // namespace int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); #ifdef DEBUG init(argc, argv); #else (void)argc; (void)argv; #endif solution(); 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | //Maciej Poleski #ifdef DEBUG #define _GLIBCXX_CONCEPT_CHECKS #include <iostream> #include <fstream> #include <cstdlib> #include <cassert> namespace { namespace Wrapper { std::ifstream in; std::ofstream out; } void init(int argc, char **argv) { if(argc != 3) { std::cerr << "Potrzeba dokładnie dwóch argumentów\n"; std::abort(); } Wrapper::in.open(argv[1]); Wrapper::out.open(argv[2]); } } #define check(x) assert(x) #else #ifndef NDEBUG #define NDEBUG #endif #define check(x) #include <iostream> namespace { namespace Wrapper { std::istream &in = std::cin; std::ostream &out = std::cout; } } #endif #include <cstdint> namespace { namespace Wrapper { typedef std::uint_fast64_t uint_fast64_t; typedef std::uint_fast32_t uint_fast32_t; typedef std::uint_fast16_t uint_fast16_t; typedef std::uint_fast8_t uint_fast8_t; typedef std::uint64_t uint64_t; typedef std::uint32_t uint32_t; typedef std::uint16_t uint16_t; typedef std::uint8_t uint8_t; typedef std::int_fast64_t int_fast64_t; typedef std::int_fast32_t int_fast32_t; typedef std::int_fast16_t int_fast16_t; typedef std::int_fast8_t int_fast8_t; typedef std::int64_t int64_t; typedef std::int32_t int32_t; typedef std::int16_t int16_t; typedef std::int8_t int8_t; typedef std::size_t size_t; } } #include <string> #include <algorithm> #include <limits> #include <locale> #include <cstring> #include <utility> #include <cstdlib> #include <random> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <list> #include <iomanip> #include <set> #include <map> #include <memory> #include <functional> #include <unordered_map> #include <unordered_set> #include <complex> #include <type_traits> #include "message.h" namespace { using namespace Wrapper; // Numeruje od 0 static std::pair<uint_fast32_t, uint_fast32_t> getXY(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny) { uint_fast32_t result = 0; uint_fast32_t rx = 0, ry = 0; while(result < n) { if(nx >= ny) { if(ry < ny - 1) { result += ry + 2; ry += 1; } else { result += std::min(ny, nx - rx - 1); rx += 1; } } else { if(ry < nx - 1) { result += ry + 2; ry += 1; } else if(ry < ny - 1) { result += nx; ry += 1; } else { result += nx - rx - 1; rx += 1; } } } uint_fast32_t diff = result - n; rx += diff; ry -= diff; return std::make_pair(rx, ry); } static uint_fast32_t getRightNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + std::min(x, n - ny - 1) + std::min(y + 1, nx - x - 1); } static uint_fast32_t getBottomNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + std::min(x, n - ny - 1) + std::min(y + 1, nx - x - 1) + 1; } static int_fast32_t getTopNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n - (std::min(nx - x, y + 1) + std::min(x, ny - y)); } static int_fast32_t getLeftNode(const uint_fast32_t n, const uint_fast32_t nx, const uint_fast32_t ny, const uint_fast32_t x, const uint_fast32_t y) { return n + 1 - (std::min(nx - x, y + 1) + std::min(x, ny - y)); } static constexpr uint_fast32_t tasksPerNode = 500; // Sprawdzić limit trasmisji danych static_assert(tasksPerNode <= 500, "Węzeł może wysłać 1000 wiadomości, zadanie wymaga 2"); static const auto numberOfNodes = NumberOfNodes(); static const auto myNodeId = MyNodeId(); struct Info { uint_fast32_t max; uint_fast32_t arg; }; static Info bestInfo(const Info &lhs, const Info &rhs) { if((lhs.max < rhs.max) || ((lhs.max == rhs.max) && (lhs.arg < rhs.arg))) { return lhs; } return rhs; } static std::unordered_map<uint_fast32_t, Info*> top_cache; void receiveFromTop(uint_fast32_t node, Info *&result) { while(top_cache.find(node) == top_cache.end()) { const auto n=Receive(node%numberOfNodes); const uint_fast32_t rn=GetInt(n); const uint_fast32_t size=GetInt(n); Info *vec=new Info[size]; for(uint_fast32_t i=0;i<size;++i) { Info ii; ii.max=GetInt(n); ii.arg=GetInt(n); vec[i]=ii; } top_cache[rn]=vec; } delete [] result; result = top_cache[node]; top_cache.erase(node); } static std::unordered_map<uint_fast32_t, Info*> left_cache; void receiveFromLeft(uint_fast32_t node, Info *&result) { while(left_cache.find(node) == left_cache.end()) { const auto n=Receive(node%numberOfNodes); const uint_fast32_t rn=GetInt(n); const uint_fast32_t size=GetInt(n); Info *vec=new Info[size]; for(uint_fast32_t i=0;i<size;++i) { Info ii; ii.max=GetInt(n); ii.arg=GetInt(n); vec[i]=ii; } left_cache[rn]=vec; } delete [] result; result = left_cache[node]; left_cache.erase(node); } inline static void solution() { using std::swap; // int nx=3; // int ny=5; // for(int y=0;y<ny;++y) // { // for(int x=0;x<nx;++x) // { // out<<getTopNode(0,nx,ny,x,y)<<' '; // } // out<<'\n'; // } uint_fast32_t n, m; in >> n >> m; char *stringA = new char[n + 2]; char *stringB = new char[m + 2]; in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in.read(stringA + 1, n); in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in.read(stringB + 1, m); stringA[n + 1] = '\0'; stringB[m + 1] = '\0'; const uint_fast32_t taskPerDimHint = std::sqrt(tasksPerNode * numberOfNodes); const uint_fast32_t taskSizeForDimA = std::max<uint_fast32_t>(1, n / taskPerDimHint + !!(n % taskPerDimHint)); const uint_fast32_t taskSizeForDimB = std::max<uint_fast32_t>(1, m / taskPerDimHint + !!(m % taskPerDimHint)); const uint_fast32_t dimASize = n / taskSizeForDimA + !!(n % taskSizeForDimA); const uint_fast32_t dimBSize = m / taskSizeForDimB + !!(m % taskSizeForDimB); for(uint_fast32_t id = myNodeId;; id += numberOfNodes) { if(id >= dimASize * dimBSize) { break; // Grid został obliczony } auto p = getXY(id, dimASize, dimBSize); const uint_fast32_t x = p.first; const uint_fast32_t y = p.second; const char *aBegin = stringA + 1 + taskSizeForDimA * x; const char *aEnd = std::min<const char *>(stringA + 1 + n, aBegin + taskSizeForDimA); const char *bBegin = stringB + 1 + taskSizeForDimB * y; const char *bEnd = std::min<const char *>(stringB + 1 + m, bBegin + taskSizeForDimB); Info *aInfo = new Info[taskSizeForDimA + 1]; Info *bInfo = new Info[taskSizeForDimB]; Info *prevAInfo = new Info[taskSizeForDimA + 1]; Info *resultBInfo = new Info[taskSizeForDimB]; // Być może przycięte na brzegu if(y == 0) { uint_fast32_t i = aBegin - stringA - 1; for(auto ptr = prevAInfo, e = prevAInfo + (aEnd - aBegin) + 1; ptr != e; ++ptr) { ptr->max = i++; ptr->arg = 0; } } else { const uint_fast32_t topNode = getTopNode(id, dimASize, dimBSize, x, y); receiveFromTop(topNode,prevAInfo); } if(x == 0) { uint_fast32_t i = bBegin - stringB; for(auto ptr = bInfo, e = bInfo + (bEnd - bBegin); ptr != e; ++ptr) { ptr->max = i++; ptr->arg = 0; } } else { const uint_fast32_t leftNode = getLeftNode(id, dimASize, dimBSize, x, y); receiveFromLeft(leftNode,bInfo); } // Klasyczna odległość edycyjna: auto bInfoPtr = bInfo; auto bInfoResultPtr = resultBInfo; for(auto bPtr = bBegin; bPtr != bEnd; ++bPtr) { uint_fast32_t aInfoIdx = 0; aInfo[aInfoIdx++] = *bInfoPtr++; for(auto aPtr = aBegin; aPtr != aEnd; ++aPtr) { if(*bPtr == *aPtr) { aInfo[aInfoIdx] = prevAInfo[aInfoIdx - 1]; } else { Info best = prevAInfo[aInfoIdx]; best.max += 1; Info temp = aInfo[aInfoIdx - 1]; temp.max += 1; best = bestInfo(best, temp); temp = prevAInfo[aInfoIdx - 1]; temp.max += 1; if(*aPtr < *bPtr) { temp.arg += 1; // Załamanie nerwowe } best = bestInfo(best, temp); aInfo[aInfoIdx] = best; } aInfoIdx += 1; } *bInfoResultPtr++ = aInfo[aInfoIdx - 1]; swap(aInfo, prevAInfo); } // Ostatni wiersz jest w prevAInfo if(x < dimASize - 1) { // Przesyłam dalej const uint_fast32_t rightNode = getRightNode(id, dimASize, dimBSize, x, y); const auto node = rightNode % numberOfNodes; PutInt(node, id); PutInt(node,(bEnd - bBegin)); for(auto ptr = resultBInfo, e = resultBInfo + (bEnd - bBegin); ptr != e; ++ptr) { PutInt(node, ptr->max); PutInt(node, ptr->arg); } Send(node); } if(y < dimBSize - 1) { const uint_fast32_t bottomNode = getBottomNode(id, dimASize, dimBSize, x, y); const auto node = bottomNode % numberOfNodes; PutInt(node, id); PutInt(node,(aEnd - aBegin) + 1); for(auto ptr = prevAInfo, e = prevAInfo + (aEnd - aBegin) + 1; ptr != e; ++ptr) { PutInt(node, ptr->max); PutInt(node, ptr->arg); } Send(node); } if((x == dimASize - 1) && (y == dimBSize - 1)) { const auto lastIdx = aEnd - aBegin; out << prevAInfo[lastIdx].max << ' ' << prevAInfo[lastIdx].arg << '\n'; } delete [] resultBInfo; delete [] prevAInfo; delete [] bInfo; delete [] aInfo; } delete [] stringB; delete [] stringA; } } // namespace int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); #ifdef DEBUG init(argc, argv); #else (void)argc; (void)argv; #endif solution(); return 0; } |