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
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "message.h"
using namespace std;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

namespace {

template<class T> inline int size(const T&c) { return c.size(); }
typedef long long LL;

const int YMULTIPLIER = 2;
const LL SZOK_COST = 1;
const LL EDIT_COST = 1LL<<17;

int gNumberOfNodes;
int gMyNodeId;
int gActiveNodes;

string gText1, gText2;
vector<int> gSplits1, gSplits2;

void Invert(string &s) {
  for (char &c : s) c = 'a' + ('z'-c);
}

void SwapTexts() {
  gText1.swap(gText2);
  Invert(gText1);
  Invert(gText2);
}

void Initialize() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr);
  gNumberOfNodes = NumberOfNodes();
  gMyNodeId = MyNodeId();
  int SZ1, SZ2;
  cin >> SZ1 >> SZ2;

  cin >> gText1 >> gText2;
  assert(size(gText1)==SZ1);
  assert(size(gText2)==SZ2);
  if(SZ1 > SZ2) SwapTexts();
}

LL EstimateCost(int P) {
  // in usec
  // computation
  LL t = LL(size(gText1)) * LL(size(gText2)) / (100*P);
  // chain : 0.2 + 0.02s for each
  if(P>1) t += (P+10)*20000LL;
  // traffic
  t += LL(size(gText1)) * (P-1) / 10;
  return t;
}

void PickActiveNodes() {
  gActiveNodes = 1;
  LL cost = EstimateCost(1);
  for(int i=2;i<=gNumberOfNodes;++i) {
    if(i*YMULTIPLIER > size(gText1)) break;
    LL c = EstimateCost(i);
    if(c < cost) {
      cost = c;
      gActiveNodes = i;
    }
  }
  //cerr << gActiveNodes << " nodes, " << cost << " usec\n";
}

vector<int> ComputeSplits(int sz, int parts) {
  vector<int> res(parts+1, sz/parts);
  res[0]=0;
  int rem = sz%parts;
  FOR(i,1,rem) res[i]++;
  FOR(i,1,parts) res[i]+=res[i-1];
  assert(res[parts] == sz);
  return res;
}

void ComputeSplits() {
  gSplits1 = ComputeSplits(size(gText1), YMULTIPLIER * gActiveNodes);
  gSplits2 = ComputeSplits(size(gText2), gActiveNodes);
}

inline LL ReadNumber() {
  unsigned bottom = GetInt(gMyNodeId-1);
  unsigned char top = GetChar(gMyNodeId-1);
  return (static_cast<LL>(top)<<32)|bottom;
}

inline void WriteNumber(LL x) {
  PutInt(gMyNodeId+1, static_cast<unsigned>(x));
  PutChar(gMyNodeId+1, static_cast<unsigned char>(x>>32));
}

void UpdateCost(vector<LL> &cost, LL cost0, int x1, int x2, char c1) {
  int n = x2-x1;
  LL prev = cost[0];
  cost[0] = cost0;
  REP(i,n) {
    char c2 = gText2[x1+i];
    LL newCost = min(cost[i], cost[i+1]) + EDIT_COST;
    LL replaceCost = prev;
    if(c1 < c2) {
      replaceCost += (EDIT_COST + SZOK_COST);
    } else if(c1>c2) {
      replaceCost += EDIT_COST;
    }
    newCost = min(newCost, replaceCost);
    prev = cost[i+1];
    cost[i+1] = newCost;
  }
}

void DoYourJob() {
  int x1 = gSplits2[gMyNodeId];
  int x2 = gSplits2[gMyNodeId+1];
  int n = x2-x1;
  vector<LL> cost(n+1);
  FOR(i,0,n) cost[i] = (x1+i) * EDIT_COST;
  auto nextReceive = gSplits1.begin();
  auto nextSend = gSplits1.begin() + 1;
  REP(y,size(gText1)) {
    LL cost0;
    if(gMyNodeId==0) {
      cost0 = (y+1) * EDIT_COST;
    } else {
      if(y==*nextReceive) {
        Receive(gMyNodeId-1);
        ++nextReceive;
      }
      cost0 = ReadNumber();
    }
    UpdateCost(cost, cost0, x1, x2, gText1[y]);
    if(gMyNodeId < gActiveNodes-1) {
      WriteNumber(cost[n]);
      if(y+1 == *nextSend) {
        Send(gMyNodeId+1);
        ++nextSend;
      }
    }
  }
  if(gMyNodeId == gActiveNodes-1) {
    cout << (cost[n] / EDIT_COST) << ' ' << (cost[n] % EDIT_COST) << '\n';
  }
}

}  // namespace

int main() {
  Initialize();
  PickActiveNodes();
  if(gMyNodeId >= gActiveNodes) return 0;
  ComputeSplits();
  DoYourJob();
}