Discrete-ACM is a powerful tool

Source: Internet
Author: User

I have been crying for a few days. It's strange that I didn't learn it well before...

Let's take a question as an example. PKU: 1151. I used to write a blog about this question in matrix67. I would like to refer to it:

Voj1056 (http://www.vijos.cn/Problem_Show.asp? Id = 1056) is always a classic problem of discretization. N rectangles on a given plane (the coordinates are integers, and there may be overlapping parts between rectangles. The common idea is to open a two-dimensional Boolean array equivalent to the two-dimensional coordinate to simulate the "Overwrite" of the rectangle (fill the position of the rectangle with true ). Unfortunately, this idea has some problems here, because the coordinate range in this question is quite large (the coordinate range is an integer ranging from-10 ^ 8 to 10 ^ 8 ). However, we found that the number of rectangles n <= 100 is far smaller than the coordinate range. Each rectangle "uses" two values on the horizontal and vertical coordinates. The coordinates of the 100 rectangles do not use the 200 values between-10 ^ 8 and 10 ^ 8. That is to say, there are actually only a few useful values. These values will be used as new coordinate values to re-divide the whole plane, saving some coordinate values in the middle without affecting. We can "discretization" the coordinate range to the number between 1 and 200, so a two-dimensional array of 200*200 is enough. The implementation method is as described in "post-sorting" at the beginning of this article ". Sort the abscissa (or ordinate) and map it to an integer ranging from 1 to 2N. At the same time, record the actual distance between every two adjacent coordinates of the new coordinate before discretization. This question also has room for optimization.

I will explain how to optimize it:

When we define a boolean type to solve this problem in a small scope, if it is such a figure:

Oh, a poor figure .. (This is the drawing software)

The coordinates correspond to: (0, 0)-(2, 2), (2, 2)-(4, 4 );

When we use Boolean enumeration, the state in the upper left corner of a small grid is enumerated each time to replace this unit area. For example:

The left matrix is 11000.

11110

01110

01110

Each grid represents 1, but how to write the program, you can indicate the state of a corner of each grid, so that it is OK, I began to tangle here for a long time

However, this question, N and m are so big that the General Mark is certainly not a play,

So, discretization:

Shenma is discrete, that is, It maps the positions of the points,

Here is: We sort the vertices and map their positions to their sorted sequence numbers. (It seems like a detour) 23333;

And so on,

Then we found that a new image appeared,

Then, use the preceding method to enumerate the label status;

Final statistics;

# Include <iostream> # include <algorithm> # include <string. h> # include <cstdio> # include <math. h> using namespace STD; Double X [201], Y [201], s [101] [4]; int XY [201] [201]; int N, CAS = 0; double sum; int main () {int I, j, k; while (CIN> N) {If (n = 0) break; CAS ++; k = 0; sum = 0.0; memset (xy, 0, sizeof (xy); for (I = 1; I <= N; I ++) {CIN> S [I] [0]> S [I] [1]> S [I] [2]> S [I] [3]; X [k] = s [I] [0]; y [k] = s [I] [1]; k ++; X [k] = s [I] [2]; y [k] = s [I] [3]; k ++;} Sort (X, X + 2 * n); sort (Y, Y + 2 * n); For (INT I = 1; I <= N; I ++) {int I1 = lower_bound (x, x + 2 * n, s [I] [0])-X; // binary search, like a common for statement, int J1 = lower_bound (Y, Y + 2 * n, s [I] [1])-y; int I2 = lower_bound (X, X + 2 * n, s [I] [2])-X; int J2 = lower_bound (Y, Y + 2 * n, s [I] [3]) -Y; For (INT p1 = I1; P1 <I2; P1 ++) // indicates the status. Remember that we mark the status as a square, so P1 <I2, not <= for (int p = J1; P <J2; P ++) XY [P1] [p] = 1 ;}for (INT I = 0; I <2 * n; I ++) // counts for (Int J = 0; j <2 * n; j ++) if (XY [I] [J]) {sum + = (X [I + 1]-X [I]) * (Y [J + 1]-y [J]);} printf ("Test Case # % d \ n", CAS); printf ("total occupied ed area: %. 2f \ n ", sum); printf (" \ n ");} return 0 ;}
View code

 

Finally, I have to post code, Nima. What do I do not understand?

There are also children who fall into discrete cups like me. Please leave a message and solve it together. ^ & ^

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.