#include <algorithm> #include <iostream> #include <utility> using namespace std; #include "message.h" namespace { struct Cost { int physical; int emotional; constexpr Cost(int p=0, int e=0): physical{p}, emotional{e} { } }; constexpr Cost operator+(Cost const& a, Cost const& b) { return Cost{a.physical + b.physical, a.emotional + b.emotional}; } Cost& operator+=(Cost& a, Cost const& b) { return a = a + b; } constexpr bool operator==(Cost const& a, Cost const& b) { return a.physical == b.physical && a.emotional == b.emotional; } constexpr bool operator!=(Cost const& a, Cost const& b) { return !(a == b); } constexpr bool operator<(Cost const& a, Cost const& b) { return a.physical != b.physical ? a.physical < b.physical : a.emotional < b.emotional; } constexpr bool operator>(Cost const& a, Cost const& b) { return b < a; } constexpr bool operator<=(Cost const& a, Cost const& b) { return !(a > b); } constexpr bool operator>=(Cost const& a, Cost const& b) { return !(a < b); } template <typename OS> OS& operator<<(OS& s, Cost const& c) { return s << c.physical << ' ' << c.emotional; } void put(int target, Cost const& c) { PutInt(target, c.physical); PutInt(target, c.emotional); } Cost get(int source) { int p = GetInt(source); int e = GetInt(source); return {p, e}; } void solve(string const& a, string const& b, int l, int r, int split) { int const k = NumberOfNodes(); int const id = MyNodeId(); int const n = a.size(); vector<Cost> prev(n+1); int rc = 0; if (id == 0) { rc = n; iota(prev.begin(), prev.end(), 0); } int sc = 0; if (id == k-1) sc = n; vector<Cost> next(n+1); vector<Cost> tabc(r-l+1); iota(tabc.begin(), tabc.end(), l-1); vector<Cost> tabn(r-l+1); for (int i = 1; i <= n; ++i) { if (i > rc) { int const q = min(rc + 1 + split, n); Receive(id-1); while (rc < q) { ++rc; prev[rc] = get(id-1); } } char const x = a[i-1]; tabn.front() = prev[i]; for (int j = l; j < r; ++j) { char const y = b[j-1]; int const q = j-l+1; tabn[q] = min(tabc[q] + 1, tabn[q-1] + 1); if (x == y) { tabn[q] = min(tabn[q], tabc[q-1]); } else if (x > y) { tabn[q] = min(tabn[q], tabc[q-1] + 1); } else { tabn[q] = min(tabn[q], tabc[q-1] + Cost{1,1}); } } swap(tabn, tabc); next[i] = tabc.back(); if (id < k-1 && (i-sc > split || i == n)) { for (int q = sc+1; q <= i; ++q) { put(id+1, next[q]); } Send(id+1); sc = i; } } if (id == k-1) { cout << next.back() << endl; } } } int main() { int const k = NumberOfNodes(); int const id = MyNodeId(); int n, m; cin >> n >> m; string a, b; cin >> a >> b; int const split = 1000; int const l = (id + 0LL) * m / k + 1; int const r = (id + 1LL) * m / k + 1; solve(a, b, l, r, split); 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 | #include <algorithm> #include <iostream> #include <utility> using namespace std; #include "message.h" namespace { struct Cost { int physical; int emotional; constexpr Cost(int p=0, int e=0): physical{p}, emotional{e} { } }; constexpr Cost operator+(Cost const& a, Cost const& b) { return Cost{a.physical + b.physical, a.emotional + b.emotional}; } Cost& operator+=(Cost& a, Cost const& b) { return a = a + b; } constexpr bool operator==(Cost const& a, Cost const& b) { return a.physical == b.physical && a.emotional == b.emotional; } constexpr bool operator!=(Cost const& a, Cost const& b) { return !(a == b); } constexpr bool operator<(Cost const& a, Cost const& b) { return a.physical != b.physical ? a.physical < b.physical : a.emotional < b.emotional; } constexpr bool operator>(Cost const& a, Cost const& b) { return b < a; } constexpr bool operator<=(Cost const& a, Cost const& b) { return !(a > b); } constexpr bool operator>=(Cost const& a, Cost const& b) { return !(a < b); } template <typename OS> OS& operator<<(OS& s, Cost const& c) { return s << c.physical << ' ' << c.emotional; } void put(int target, Cost const& c) { PutInt(target, c.physical); PutInt(target, c.emotional); } Cost get(int source) { int p = GetInt(source); int e = GetInt(source); return {p, e}; } void solve(string const& a, string const& b, int l, int r, int split) { int const k = NumberOfNodes(); int const id = MyNodeId(); int const n = a.size(); vector<Cost> prev(n+1); int rc = 0; if (id == 0) { rc = n; iota(prev.begin(), prev.end(), 0); } int sc = 0; if (id == k-1) sc = n; vector<Cost> next(n+1); vector<Cost> tabc(r-l+1); iota(tabc.begin(), tabc.end(), l-1); vector<Cost> tabn(r-l+1); for (int i = 1; i <= n; ++i) { if (i > rc) { int const q = min(rc + 1 + split, n); Receive(id-1); while (rc < q) { ++rc; prev[rc] = get(id-1); } } char const x = a[i-1]; tabn.front() = prev[i]; for (int j = l; j < r; ++j) { char const y = b[j-1]; int const q = j-l+1; tabn[q] = min(tabc[q] + 1, tabn[q-1] + 1); if (x == y) { tabn[q] = min(tabn[q], tabc[q-1]); } else if (x > y) { tabn[q] = min(tabn[q], tabc[q-1] + 1); } else { tabn[q] = min(tabn[q], tabc[q-1] + Cost{1,1}); } } swap(tabn, tabc); next[i] = tabc.back(); if (id < k-1 && (i-sc > split || i == n)) { for (int q = sc+1; q <= i; ++q) { put(id+1, next[q]); } Send(id+1); sc = i; } } if (id == k-1) { cout << next.back() << endl; } } } int main() { int const k = NumberOfNodes(); int const id = MyNodeId(); int n, m; cin >> n >> m; string a, b; cin >> a >> b; int const split = 1000; int const l = (id + 0LL) * m / k + 1; int const r = (id + 1LL) * m / k + 1; solve(a, b, l, r, split); return 0; } |