#include "message.h" #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> #include <deque> #include <unordered_set> #include <unordered_map> #define LOG(args...) //#define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) using namespace std; typedef pair<int,int> para; int n, m; char a[250000], b[250000]; int id; int nodes; int GetStepSize() { // return n+1; // razem kroków nodes*6 return max(10, ((n + nodes - 1)/nodes + 5)/3); } int ReceiveArray(int * mx, int * val, int from) { if(id == 0) { int to = from + GetStepSize(); if(to > n) to = n+1; for(int i = from; i<to; i++) {mx[i] = i; val[i] = 0;} return to-from; } else { Receive(id-1); int to = from + GetInt(id-1); // printf("prec %d-%d\n", from, to); for(int i = from; i<to; i++) {mx[i] = GetInt(id-1); val[i] = GetInt(id-1);} return to-from; } } int SendArray(int * mx, int * val, int from, int to) { if(id<nodes-1) { PutInt(id+1, to-from); for(int i = from; i<to; i++) { PutInt(id+1, mx[i]); PutInt(id+1, val[i]);} Send(id+1); // printf("sent %d-%d\n", from, to); } } int mx[250000], val[250000],mxf[250000], valf[250000]; int m1[250000], m2[250000]; int v1[250000], v2[250000]; void algo() { int mod = m%nodes; int sp[120]; FOR(i, nodes) { if(i == 0) sp[i] = 0; else sp[i] = sp[i-1]; sp[i]+=m/nodes; if(i<mod) sp[i]++; } int from = 0; int to = sp[id]; if(id>0) from = sp[id-1]; // printf("%d->%d\n", from, to); int rec = 0; int sent = 0; int * mp = m1, * mc = m2; int * vp = v1, * vc = v2; for(int i = 0; i<=m; i++) { mp[i] = i; vp[i] = 0; } for(int j = 1; j<=n; j++) { while(rec <= j) { // printf("rec :\n"); int kk = ReceiveArray(mx, val, rec); // printf("rec %d\n", kk); rec+=kk; } if(j-sent >= GetStepSize()) { SendArray(mxf, valf, sent, j); sent = j; } mc[from] = mx[j]; vc[from] = val[j]; LOG("%d>%d[%d] (%d[%d])\n",j, mc[from], vc[from], mp[from], vp[from]); //printf("%d/%d %d[%d] %d[%d]\n", from+1, to, mp[from],vp[from],mc[from],vc[from]); for(int i = from+1; i<=to; i++) { para p1(mc[i-1]+1, vc[i-1]); para p2(mp[i]+1, vp[i]); para p3(mp[i-1]+ (b[i-1] == a[j-1]? 0: 1), vp[i-1]+ (b[i-1]>a[j-1]? 1:0)); para p = min(p1,min(p2,p3)); mc[i] = p.first; vc[i] = p.second; LOG("<%d/%d/%d>", p1.second, p2.second, p3.second); LOG("%d[%d](%c%c) ", mc[i], vc[i], b[i-1], a[j-1]); } LOG("\n"); mxf[j] = mc[to]; valf[j] = vc[to]; swap(mc,mp); swap(vc,vp); } SendArray(mxf, valf, sent, n+1); // // FOR(i, n+1) printf(":%d[%d] ", mx[i], val[i]); // printf("\n"); // // FOR(i, n+1) printf(";%d[%d] ", mxf[i], valf[i]); // printf("\n"); } int main() { id = MyNodeId(); nodes = NumberOfNodes(); scanf("%d%d", &n, &m); scanf("%s%s", a, b); algo(); if(id == nodes-1) { printf("%d %d\n", mxf[n], valf[n]); } 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 | #include "message.h" #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> #include <deque> #include <unordered_set> #include <unordered_map> #define LOG(args...) //#define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) using namespace std; typedef pair<int,int> para; int n, m; char a[250000], b[250000]; int id; int nodes; int GetStepSize() { // return n+1; // razem kroków nodes*6 return max(10, ((n + nodes - 1)/nodes + 5)/3); } int ReceiveArray(int * mx, int * val, int from) { if(id == 0) { int to = from + GetStepSize(); if(to > n) to = n+1; for(int i = from; i<to; i++) {mx[i] = i; val[i] = 0;} return to-from; } else { Receive(id-1); int to = from + GetInt(id-1); // printf("prec %d-%d\n", from, to); for(int i = from; i<to; i++) {mx[i] = GetInt(id-1); val[i] = GetInt(id-1);} return to-from; } } int SendArray(int * mx, int * val, int from, int to) { if(id<nodes-1) { PutInt(id+1, to-from); for(int i = from; i<to; i++) { PutInt(id+1, mx[i]); PutInt(id+1, val[i]);} Send(id+1); // printf("sent %d-%d\n", from, to); } } int mx[250000], val[250000],mxf[250000], valf[250000]; int m1[250000], m2[250000]; int v1[250000], v2[250000]; void algo() { int mod = m%nodes; int sp[120]; FOR(i, nodes) { if(i == 0) sp[i] = 0; else sp[i] = sp[i-1]; sp[i]+=m/nodes; if(i<mod) sp[i]++; } int from = 0; int to = sp[id]; if(id>0) from = sp[id-1]; // printf("%d->%d\n", from, to); int rec = 0; int sent = 0; int * mp = m1, * mc = m2; int * vp = v1, * vc = v2; for(int i = 0; i<=m; i++) { mp[i] = i; vp[i] = 0; } for(int j = 1; j<=n; j++) { while(rec <= j) { // printf("rec :\n"); int kk = ReceiveArray(mx, val, rec); // printf("rec %d\n", kk); rec+=kk; } if(j-sent >= GetStepSize()) { SendArray(mxf, valf, sent, j); sent = j; } mc[from] = mx[j]; vc[from] = val[j]; LOG("%d>%d[%d] (%d[%d])\n",j, mc[from], vc[from], mp[from], vp[from]); //printf("%d/%d %d[%d] %d[%d]\n", from+1, to, mp[from],vp[from],mc[from],vc[from]); for(int i = from+1; i<=to; i++) { para p1(mc[i-1]+1, vc[i-1]); para p2(mp[i]+1, vp[i]); para p3(mp[i-1]+ (b[i-1] == a[j-1]? 0: 1), vp[i-1]+ (b[i-1]>a[j-1]? 1:0)); para p = min(p1,min(p2,p3)); mc[i] = p.first; vc[i] = p.second; LOG("<%d/%d/%d>", p1.second, p2.second, p3.second); LOG("%d[%d](%c%c) ", mc[i], vc[i], b[i-1], a[j-1]); } LOG("\n"); mxf[j] = mc[to]; valf[j] = vc[to]; swap(mc,mp); swap(vc,vp); } SendArray(mxf, valf, sent, n+1); // // FOR(i, n+1) printf(":%d[%d] ", mx[i], val[i]); // printf("\n"); // // FOR(i, n+1) printf(";%d[%d] ", mxf[i], valf[i]); // printf("\n"); } int main() { id = MyNodeId(); nodes = NumberOfNodes(); scanf("%d%d", &n, &m); scanf("%s%s", a, b); algo(); if(id == nodes-1) { printf("%d %d\n", mxf[n], valf[n]); } return 0; } |