Poj 1151 Atlanta

Source: Internet
Author: User

Poj_1151

This topic is my first contact with discretization. After reading the related parts of the black book, I designedAlgorithm:

First, the x-y plane is regarded as the straight line where the four sides of the rectangle are located and cut into several blocks. Then, each part is regarded as a point, and then all the rectangles are scanned, mark the vertices covered by the rectangles, and then calculate the area of all the marked vertices.

In this way, the sorting X coordinate is the complexity of O (nlogn), and the sorting y coordinate is O (nlogn). Since the X and Y axes are cut by a maximum of 2 * n knives, therefore, the number of points finally obtained is n ^ 2 orders of magnitude. In this way, the point covered by the rectangle is the complexity of O (N ^ 3, the last scan is O (n ^ 2) Complexity. After the submission, it is TLE.

It is because O (N ^ 3) is too complex, so we must reduce the complexity of this part. Compared with the complexity of the last scan, an idea emerged. Can we calculate the covered area directly at the end of the scan without marking the points covered by the rectangle? If this is feasible, the complexity of O (N ^ 2) may be reduced.

Think about it. We will enumerate X at the end of the scan, and then calculate y from the small to large. If the rectangle is ordered to Y1, we can ensure that we can scan the rectangle to calculate the number of covered areas in the current x range. During the specific calculation, up and down are used to represent the top and bottom of a continuously covered part. At the beginning, it is set to-1, and then the sorted rectangle is scanned one by one, if the Y1 of the current rectangle is larger than up, it means that it will not overlap with the previously covered part, so we can calculate the area of the previously covered part and accumulate it into the result, if the Y1 value of the current rectangle is no larger than the UP value, but Y2 value is greater than the UP value, it means that the area of continuous coverage is extended up again. Just update up.

Later, I re-used the O (N ^ 2) algorithm to open the array, so the previous O (N ^ 3) the reason why the algorithm times out is unclear because the array is too small or because the algorithm complexity is too high, because O (N ^ 3) is about 10 ^ 6, it should also be AC theoretically.

# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
# Define INF 100010
# Define Maxd 220
# Define Zero 1e-8
Struct Square
{
Double X1, Y1, X2, Y2;
} S [maxd];
Int X, N;
Double Left [maxd], right [maxd], TX [maxd];
Int CMP1 ( Const Void * _ P, Const Void * _ Q)
{
Square * P = (square *) _ p, * q = (square *) _ q;
Return P-> Y1 <q-> Y1? - 1 : 1 ;
}
Int Cmp2 ( Const Void * _ P, Const Void * _ Q)
{
Double * P = ( Double *) _ P, * q = ( Double *) _ Q;
Return * P <* Q? - 1 : 1 ;
}
Double FABS ( Double X)
{
Return X < 0 ? -X: X;
}
Int DCMP ( Double X)
{
Return FABS (x) <zero? 0 : (X < 0 ? - 1 : 1 );
}
Void Init ()
{
Int I, J, K = 2 ;
TX [ 0 ] =- 1 , TX [ 1 ] = Inf;
For (I = 0 ; I <X; I ++)
{
Scanf ( " % Lf " , & S [I]. X1, & S [I]. Y1, & S [I]. X2, & S [I]. Y2 );
TX [k ++] = s [I]. x1;
TX [k ++] = s [I]. X2;
}
Qsort (S, X, Sizeof (S [ 0 ]), CMP1 );
Qsort (TX, K, Sizeof (TX [ 0 ]), Cmp2 );
N = 0 ;
For (I = 1 ; I <K; I ++)
If (DCMP (TX [I]-TX [I- 1 ])! = 0 )
{
Left [N] = TX [I- 1 ], Right [N] = TX [I];
++ N;
}
}
Void Solve ()
{
Int I, J, K;
Double Ans = 0 , Down, up;
For (I = 0 ; I <n; I ++)
{
Down = up =- 1 ;
For (J = 0 ; J <X; j ++)
If (DCMP (left [I]-s [J]. X1)> = 0 & DCMP (right [I]-s [J]. x2) <= 0 )
{
If (DCMP (s [J]. Y1-up)> 0 )
{
Ans + = (right [I]-left [I]) * (up-down );
Down = s [J]. Y1, up = s [J]. Y2;
}
Else If (DCMP (s [J]. Y2-up)> 0 )
Up = s [J]. Y2;
}
Ans + = (right [I]-left [I]) * (up-down );
}
Printf ( " Total received area: %. 2lf \ n " , ANS );
}
Int Main ()
{
Int T = 0 ;
For (;;)
{
Scanf ( " % D " , & X );
If (! X)
Break ;
Init ();
Printf ( " Test Case # % d \ n " , ++ T );
Solve ();
Printf ( " \ N " );
}
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.