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<iostream>
#include<utility>

#include "message.h"

std::pair<int, int> state[2][100001], htab[100001];
char on[100001], om[100001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	int n, m;
	std::cin >> n >> m >> on >> om;

	int number = NumberOfNodes();
	int id = MyNodeId();

	int tmp = std::min(n, m);
	if(id >= tmp)
		return 0;
	if(number > tmp)
		number = tmp;

	int hspan = n / number;
	int hstart = id * hspan;
	int hend = id == number - 1 ? n : (id + 1) * hspan;
	int wspan = m / number;

	for(int i = hstart; i <= hend; ++i)
		htab[i] = std::make_pair(i, 0);

	std::pair<int, int> *oldstate = state[0], *newstate = state[1];

	for(int i = 0; i < number; ++i)
	{
		oldstate[0] = htab[hstart];

		int wstart = i * wspan;
		int wend = i == number - 1 ? m : (i + 1) * wspan;
		int wsize = wend - wstart;

		if(id == 0)
			for(int j = 1; j <= wsize; ++j)
				oldstate[j] = std::make_pair(wstart + j, 0);
		else
		{
			int source = id - 1;
			Receive(source);
			for(int j = 1; j <= wsize; ++j)
			{
				oldstate[j].first = GetInt(source);
				oldstate[j].second = GetInt(source);
			}
		}

		htab[hstart] = oldstate[wsize];

		for(int j = hstart + 1; j <= hend; ++j)
		{
			newstate[0] = htab[j];

			for(int k = 1; k <= wsize; ++k)
			{
				newstate[k] = std::min(oldstate[k], newstate[k - 1]);
				++newstate[k].first;
				std::pair<int, int> tmp = oldstate[k - 1];
				if(on[j - 1] != om[wstart + k - 1])
				{
					++tmp.first;
					if(on[j - 1] < om[wstart + k - 1])
						++tmp.second;
				}
				newstate[k] = std::min(newstate[k], tmp);
			}

			htab[j] = newstate[wsize];

			std::swap(oldstate, newstate);
		}

		if(id != number - 1)
		{
			int target = id + 1;
			for(int j = 1; j <= wsize; ++j)
			{
				PutInt(target, oldstate[j].first);
				PutInt(target, oldstate[j].second);
			}
			Send(target);
		}
	}

	if(id == number - 1)
		std::cout << htab[hend].first << ' ' << htab[hend].second << std::endl;

	return 0;
}