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
#include <string.h>
#include <stdlib.h>
//---===---
#include <stdio.h>
#include <unistd.h>

typedef unsigned int uint;

char buf_in[4096000];
int buf_in_head = 0;
int buf_in_tail = 0;

static int getChar(void) {
  while (buf_in_head == buf_in_tail) {
    int rv = read(0, buf_in, sizeof(buf_in));
    if (rv > 0) {
      buf_in_head = 0;
      buf_in_tail = rv;
      break;
    };
    if (rv == 0) return EOF;
  }
  return buf_in[buf_in_head++];
}

// only 1 call is safe, and only if previously getChar() had been called.
static void ungetChar(void) {
  --buf_in_head;
}

static uint getUInt() {
  uint v;
  int c;
  for (;;) {
    c = getChar();
    if (c < '0') continue;
    if (c > '9') continue;
    v = c - '0';
    break;
  };
  for (;;) {
    c = getChar();
    if (c < '0') break;
    if (c > '9') break;
    v *= 10;
    v += c - '0';
  };
  ungetChar();
  return v;
}
//---===---

static int getSmallLetter(void) {
  int c;
  for (;;) {
    c = getChar();
    if (c == EOF) return EOF;
    if (c < 'a') continue;
    if (c > 'z') continue;
    return c;
  };
}

#include "message.h"
// int NumberOfNodes(); // 10 <= NoN <= 100
// int MyNodeId(); // 0 <= x < NoN

// void PutChar(int target, char value);
// void PutInt(int target, int value);
// void PutLL(int target, long long value);
// void Send(int target);

// int Receive(int source); // -1 -> returns source
// char GetChar(int source);
// int GetInt(int source);
// long long GetLL(int source);

int n, m;
char A[100000+1];
char B[100000+1];

#define MOD 1000000LL
long long T[2][100000+1]; // edits * MOD + shocks

int main (void) {
  int NoN = NumberOfNodes();
  int Node = MyNodeId();

  int i, j, p, j_start, j_end, messages, msg, i_start, i_end;
  long long best_runtime_us = 86400 * 1000000LL; // 1 day
  long long best_nodes = -1;
  long long best_pieces = -1;

  // 1 <= n, m <= 100,000
  n = getUInt();
  m = getUInt();
  for (i = 1; i <= n; ++i) A[i] = getSmallLetter();
  for (j = 1; j <= m; ++j) B[j] = getSmallLetter();

  for (j = 0; j <= m; ++j) T[0][j] = j * MOD; // j edits, 0 shocks

  messages = n;

  for (i = 1; i <= NoN; ++i) {
    for (p = 1; p <= 1000; ++p) {
      // 5.7ns compute per cell, actually try 12.3ns
      long long runtime_us = n * 123LL * m * (p + 1) / p / i / 10000;
      // 1.911ms synchronization overhead, actually try 0.500ms
      runtime_us += p * 500LL;
      if (runtime_us < best_runtime_us) {
        best_runtime_us = runtime_us;
        best_nodes = i;
        best_pieces = p;
      };
      if (p >= 10) p += 1;
      if (p >= 100) p += 8;
    };
  };
  NoN = best_nodes;
  messages = best_pieces;

  if (NoN == 1) messages = 1;

  //if (Node == 0) fprintf(stderr, "N:%d M:%d I:%d P:%d\n", n, m, NoN, messages);
  //exit(0);

  // in case we want to use less nodes and we reduced NoN
  if (Node >= NoN) return 0;

  // should not happen, but safety first
  if (messages > 1000) messages = 1000;
  if (messages < 1) messages = 1;

  // 1 .. m in Node pieces
  j_start = m * 1LL * Node / NoN + 1;
  j_end = m * 1LL * (Node + 1) / NoN;

  for (msg = 0; msg < messages; ++msg) {
    // 1 .. n in messages pieces
    i_start = n * 1LL * msg / messages + 1;
    i_end = n * 1LL * (msg + 1) / messages;
    if (Node != 0) Receive(Node - 1);
//...
    for (i = i_start; i <= i_end; ++i) {
      int prev = (i - 1) & 1;
      int cur = i & 1;
      if (Node == 0) {
        T[cur][j_start - 1] = i * MOD; // j_start - 1 == 0
      } else {
        T[cur][j_start - 1] = GetLL(Node - 1);
      };
      for (j = j_start; j <= j_end; ++j) {
        long long u, v, w;
        u = T[prev][j] + MOD; // pay for insert/delete
        v = T[cur][j - 1] + MOD; // pay for delete/insert
        if (A[i] < B[j]) {
          w = T[prev][j - 1] + MOD + 1; // increase
        } else if (A[i] > B[j]) {
          w = T[prev][j - 1] + MOD; // decrease
        } else { /* A[i] == B[j] */
          w = T[prev][j - 1];
        };
        if (u < v) v = u;
        if (v < w) w = v;
        T[cur][j] = w;
      };
      if (Node != NoN - 1) PutLL(Node + 1, T[cur][j_end]);
    };
//...
    if (Node != NoN - 1) Send(Node + 1);
  };

  if (Node == NoN - 1) {
    printf("%lld %lld\n", T[n & 1][m] / MOD, T[n & 1][m] % MOD);
  };

  return 0;
}