I think the question is a simulated question, but the simulation will definitely time out and exceed the memory. But the compression is hard to understand.
After reading the ideas of many people, I feel that this is a good idea. It is to calculate the changed point as an intermediate point in the 3*3 matrix. At the same time, the related situations of the surrounding eight points are also calculated.
This is equivalent to recording the values of the first vertex of the change point, including the maximum absolute value and the last vertex.
At the end, you only need to compare whether the values between two adjacent points are the same. If they are different, subtract the positions and obtain the number of such points. In the same way, continue to find the next one.
Good idea! Why can't you think of it. I still have more exercises.
#include <iostream>#include <cmath>#include <algorithm>using namespace std;int width;// the width of the Mapint total = 0;//the total num of pixelstruct Map{int pos;//position of the pixelint code;//the code of the pixel}pixMap[9010];int mMap[1010][2];int cmp(const void *a, const void *b){struct Map * x = (struct Map *)a;struct Map * y = (struct Map *)b;return x->pos - y->pos;}int GetValue(int pos){int i = 0;int p = 0;while (p < pos){p += mMap[i ++][1];}return mMap[i - 1][0];}int GetCode(int pos){int code = GetValue(pos);int Max = 0;int row = (pos - 1) / width;//the int col = (pos - 1) % width;for (int i = row - 1; i <= row + 1; ++ i){for (int j = col - 1; j <= col + 1; ++ j){int tpos = i * width + j;if (i < 0 || j < 0 || j >= width || tpos >= total || tpos == pos - 1){continue;}int tcode = GetValue(tpos + 1);if (Max < abs(tcode - code)){Max = abs(tcode - code);}//pixMap[k].pos = tpos + 1;//pixMap[k ++].code = GetCode(tpos + 1);}}return Max;}int main(){int k;int pix, pixNum;while (cin >> width && width){k = 0;total = 0;while(cin >> pix >> pixNum && pixNum){mMap[k][0] = pix;mMap[k ++][1] = pixNum;total += pixNum;}int pairNum = k;cout << width << endl;int pos = 1;k = 0;for (int p = 0; p <= pairNum; ++ p){int row = (pos - 1) / width;//the int col = (pos - 1) % width;for (int i = row - 1; i <= row + 1; ++ i){for (int j = col - 1; j <= col + 1; ++ j){int tpos = i * width + j;if (i < 0 || j < 0 || j >= width || tpos >= total){continue;}pixMap[k].pos = tpos + 1;pixMap[k ++].code = GetCode(tpos + 1);}}pos += mMap[p][1];}qsort(pixMap, k, sizeof(pixMap[0]), cmp);struct Map tmp = pixMap[0];for (int i = 0; i < k; ++ i){if (tmp.code == pixMap[i].code){continue;}cout << tmp.code << ' ' << pixMap[i].pos - tmp.pos << endl;tmp = pixMap[i];}cout << tmp.code << ' ' << total - tmp.pos + 1 << endl;cout << "0 0" << endl;}cout << 0 << endl;return 0;}