Poj 1009, which is a simulated question?

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.