#include <algorithm> #include <cstdio> #include <utility> #include <vector> #include "message.h" using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) typedef pair<int, int> P; char a[100001], b[100001]; int dist[2][100001], shock[2][100001]; #define DIST(i, j) dist[(i)&1][j] #define SHOCK(i, j) shock[(i)&1][j] int ii, jj, kk; int batchSize; vector<P> outBuffer, inBuffer; int inPos; P getNext() { int s = jj - 1; if (s < 0) return make_pair(++ii, 0); if (inPos == int(inBuffer.size())) { Receive(s); int bufSize = GetInt(s); inBuffer.clear(); inPos = 0; REP(x,bufSize) { P p; p.first = GetInt(s); p.second = GetInt(s); inBuffer.push_back(p); } } P p = inBuffer[inPos++]; return p; } void flushBuffer() { int t = jj + 1; int bufSize = outBuffer.size(); if (!bufSize) return; PutInt(t, bufSize); REP(x,bufSize) { PutInt(t, outBuffer[x].first); PutInt(t, outBuffer[x].second); } Send(t); outBuffer.clear(); } void putNext(const P& p) { if (jj + 1 == kk) return; outBuffer.push_back(p); if (int(outBuffer.size()) == batchSize) flushBuffer(); } int main() { int n, m; scanf("%d%d\n", &n, &m); bool flipped = 0; if (n >= m) { gets(a); gets(b); } else { swap(n, m); gets(b); gets(a); flipped = 1; } batchSize = max((n + 949) / 950, 10); jj = MyNodeId(); kk = NumberOfNodes(); int start = m * jj / kk, end = m * (jj + 1) / kk; FOR(j,start,end+1) DIST(0, j) = j; FOR(i,1,n+1) { P next = getNext(); DIST(i, start) = next.first; SHOCK(i, start) = next.second; FOR(j,start+1,end+1) { P pi = make_pair(DIST(i - 1, j) + 1, SHOCK(i - 1, j)); P pj = make_pair(DIST(i, j - 1) + 1, SHOCK(i, j - 1)); P pij = make_pair(DIST(i - 1, j - 1), SHOCK(i - 1, j - 1)); if (a[i - 1] != b[j - 1]) { ++pij.first; bool disgusting = flipped ? a[i - 1] > b[j - 1] : a[i - 1] < b[j - 1]; if (disgusting) ++pij.second; } P p = min(min(pi, pj), pij); DIST(i, j) = p.first; SHOCK(i, j) = p.second; } putNext(make_pair(DIST(i, end), SHOCK(i, end))); } flushBuffer(); if (jj == kk - 1) printf("%d %d\n", DIST(n, m), SHOCK(n, m)); }
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 | #include <algorithm> #include <cstdio> #include <utility> #include <vector> #include "message.h" using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) typedef pair<int, int> P; char a[100001], b[100001]; int dist[2][100001], shock[2][100001]; #define DIST(i, j) dist[(i)&1][j] #define SHOCK(i, j) shock[(i)&1][j] int ii, jj, kk; int batchSize; vector<P> outBuffer, inBuffer; int inPos; P getNext() { int s = jj - 1; if (s < 0) return make_pair(++ii, 0); if (inPos == int(inBuffer.size())) { Receive(s); int bufSize = GetInt(s); inBuffer.clear(); inPos = 0; REP(x,bufSize) { P p; p.first = GetInt(s); p.second = GetInt(s); inBuffer.push_back(p); } } P p = inBuffer[inPos++]; return p; } void flushBuffer() { int t = jj + 1; int bufSize = outBuffer.size(); if (!bufSize) return; PutInt(t, bufSize); REP(x,bufSize) { PutInt(t, outBuffer[x].first); PutInt(t, outBuffer[x].second); } Send(t); outBuffer.clear(); } void putNext(const P& p) { if (jj + 1 == kk) return; outBuffer.push_back(p); if (int(outBuffer.size()) == batchSize) flushBuffer(); } int main() { int n, m; scanf("%d%d\n", &n, &m); bool flipped = 0; if (n >= m) { gets(a); gets(b); } else { swap(n, m); gets(b); gets(a); flipped = 1; } batchSize = max((n + 949) / 950, 10); jj = MyNodeId(); kk = NumberOfNodes(); int start = m * jj / kk, end = m * (jj + 1) / kk; FOR(j,start,end+1) DIST(0, j) = j; FOR(i,1,n+1) { P next = getNext(); DIST(i, start) = next.first; SHOCK(i, start) = next.second; FOR(j,start+1,end+1) { P pi = make_pair(DIST(i - 1, j) + 1, SHOCK(i - 1, j)); P pj = make_pair(DIST(i, j - 1) + 1, SHOCK(i, j - 1)); P pij = make_pair(DIST(i - 1, j - 1), SHOCK(i - 1, j - 1)); if (a[i - 1] != b[j - 1]) { ++pij.first; bool disgusting = flipped ? a[i - 1] > b[j - 1] : a[i - 1] < b[j - 1]; if (disgusting) ++pij.second; } P p = min(min(pi, pj), pij); DIST(i, j) = p.first; SHOCK(i, j) = p.second; } putNext(make_pair(DIST(i, end), SHOCK(i, end))); } flushBuffer(); if (jj == kk - 1) printf("%d %d\n", DIST(n, m), SHOCK(n, m)); } |