Test instructions: Give a top view. Gives the left subscript, length, width, height of n buildings. Now, looking from the south, can you see the buildings?
Sample Input
14160 0 30 60 30125 0 32 28 6095 0 27 28 4070 35 19 55 900 0 60 35 800 40 29 20 6035 40 25 45 800 67 25 20 500 92 90 20 80 95 38 55 12 5095 60 60 13 3095 80 45 25 50165 65 15 15 25165 85 10 15 350
Sample Output
For map #1, the visible buildings is numbered as Follows:5 9 4 3 10 2 1 14
The unique () function is a deduplication function, and the unique function of STL is to remove adjacent repeating elements (one only)
Another feature that is easy to overlook is that it does not really remove duplicate elements.
eg.
int num[100];
Unique (num,mun+n) returns NUM to the end of the address, the reason is not really to delete the duplicate elements, in fact, the function is to repeat the elements to the back,
It is then saved to the original array, and then returns the address of the last element after the deduplication, since unique removes the adjacent repeating element, so it is usually ordered before it is used.
Discretization of
The basic idea is to "consider only the values I need" in a number of possible situations.
Eg.1
The coordinates of n points on a given plane, to be able to cover the smallest rectangular area of all these points. This problem is difficult to find, and the rectangle can be tilted (edges do not have to be parallel to the axes).
the tilt placement here is very bad because we don't know how many degrees the rectangle will eventually tilt. Assuming we know that the angle of the rectangle is α, the answer is simple:
When the rectangular area is minimum, four edges must be next to a certain point. That is to say, the slope of the four sides is already known, just let these edges from the outside to keep approaching the point set until they hit a point.
You don't have to know exactly how this should be done, just understand that this can be calculated in some way, after all, our focus is on the process below.
Our algorithm is obvious: the angle of the enumeration rectangle, for each dip, we can calculate the smallest rectangular area, and finally take a minimum value.
Is the algorithm correct? We cannot say whether it is correct, because it is simply impossible to achieve. The inclination of a rectangle is a real number, it has countless possibilities, and you can never enumerate each case. We say that the inclination of a rectangle is a "continuous" variable, which is the root cause of the inclination we cannot enumerate. We need a way to turn this "continuous" variable into a single value into a "discrete" variable. This process is called discretization.
We can prove that the minimum area of the rectangle requires not only four edges have a point, but also requires at least one edge of two or more than two points. Imagine, if there is only one point on each side, then we can always rotate the rectangle a little bit so that the rectangle becomes "loose", so that a smaller rectangle is available. So we find that the slope of one edge of a rectangle must be the same as that of a two-point line. If we calculate the inclination of all the straight lines over two points, then the value of α is only possible if the angle of the dip or it minus 90 degrees (the line is tilted in the direction of "\") so C (n,2). We say that this "dip" has been "discretized" by us. Although this algorithm still has the room to optimize, but at this time we have reached the beginning of this article to say the purpose.
Eg.2
The main idea is that n rectangles on a given plane (the coordinates are integers, there may be overlapping portions between rectangles and rectangles), and the total area of coverage is calculated. The usual idea is to open a two-dimensional Boolean array that is equivalent to two-dimensional coordinates to simulate the "overlay" of the rectangle (fill the position of the rectangle with true). Unfortunately, this idea has some problems here, because the range of coordinates in this topic is quite large (the integer coordinates range from -10^8 to 10^8). But we find that the number of rectangles is n<=100 far less than the coordinate range. Each rectangle "uses" two values on the horizontal ordinate, and the coordinates of the 100 rectangles also use 200 values between -10^8 and 10^8. In other words, the actual useful value is actually only a few. These values will be re-dividing the entire plane as new coordinate values, eliminating the influence of several coordinate values in the middle. We can "scatter" the coordinate range to a number between 1 and 200, so a 200*200 two-dimensional array is sufficient. The implementation method is "post-processing" as described at the beginning of this article. The horizontal axis (or ordinate) is sorted once and mapped to an integer from 1 to 2n, while recording the actual distance between each two adjacent coordinates of the new coordinate before discretization. The problem also has room for optimization.
#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;Const intmaxn= -+5;structbuilding{intID; Doublex,y,w,d,h; BOOL operator< (Constbuilding& RHS)Const{//Overloaded Operators < returnx<rhs.x| | (X==RHS.X&&Y<RHS.Y);//true x is not blocked by RHS (X is the same as the left of rhs.x or X is the same as rhs.x, but X is in front of rhs.x, i.e. Y<RHS.Y)}}B[MAXN];intN;Doublex[maxn*2];//used to store the left and right borders of each buildingBOOLCoverintIDoublemx//determine if the building I is visible at the X=MX place{ returnb[i].x<=mx&&b[i].x+b[i].w>=mx;//True when B[i] is visible at MX}BOOLVisibleintIDoubleMX) { if(!cover (I,MX))return false;//B[i] not visible at MX for(intk=0; k<n;k++) if(B[k].y<b[i].y&&b[k].h>=b[i].h&&cover (K,MX))return false;//B[i] blocked by b[k] return true;//b[i] visible in MX} intMain () {intKase=0; while(SCANF ("%d", &n) = =1&&n) {//N Buildings for(intI=0; i<n;i++) {scanf ("%LF%LF%LF%LF%LF",&b[i].x,&b[i].y,&b[i].w,&b[i].d,&b[i].h); X[i*2]=b[i].x;//Construction left Borderx[i*2+1]=B[I].X+B[I].W;//Building Right Borderb[i].id=i+1; } sort (B,b+N);//by overloading operators in structs < sorting, x small in front, x same as Y small in front /*cout<<endl; for (int i=0;i<n;i++) cout<<b[i].id<< "" <<b[i].x<< "<<b[i].y<<" "<< ;b[i].h<< "" <<b[i].d<<endl;*/Sort (x,x+n*2); intM=unique (x,x+n*2)-X;//after the x-coordinate is sorted, I get the M coordinates .//the first m of X has a boundary between the left and right left bounds of the complex//for (int i=0;i<n*2;i++) cout<<x[i]<<endl; if(kase++) printf ("\ n"); printf ("For map #%d,the visible buildings is numbered as follows:\n%d", kase,b[0].id);//the lower left corner of the building must be seen for(intI=1; i<n;i++){ BOOLvis=false; for(intj=0; j<m-1; j + +){ if(B[i].x+b[i].w<x[j]) Break;//Add this sentence, simplify the program, when the right side of the b[i] building is less than x[j] when the point is definitely not visible if(Visible (I, (x[j]+x[j+1])/2)){//true B[i] visible at the midpoint between the left and right bordersvis=true; Break; } } if(Vis) printf ("%d", b[i].id); } printf ("\ n"); } //System ("pause"); return 0;}
UVA 221-urban elevations (discretized)!!!!!!