POJ1328 Radar Installation greedy

Source: Internet
Author: User
Tags radar

A: There is a coastline. There is a sea above the coastline. There are some islands in the sea. The locations of these islands are known. There are radars on the coastline, and the radar coverage radius is known, ask how many radars are needed to cover all the islands.
Idea: the coordinates of each island are known, and the radar radius is used as the radius to draw a circle. There are two intersections with the X axis. That is to say, to cover the island, the radar's location range is the two intersections. Therefore, it is converted into a coverage range.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <cmath>
# Include <string. h>
Using namespace std;
 
Const int n= 1010;
Struct point {
Int x, y;
} Pp [N];
Struct line {
Double sp, ep;
} Ll [N];
Bool cmp (line a, line B ){
If (a. ep = B. ep)
Return a. sp <B. sp;
Return a. ep <B. ep;
} Www.2cto.com
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int n, len, ca = 1;
While (scanf ("% d", & n, & len )){
Bool islen = false;
If (len <0 ){
Islen = true;
}
If (n + len = 0 & len> = 0)
Break;
Bool flag = false;
For (int I = 0; I <n; ++ I ){
Scanf ("% d", & pp [I]. x, & pp [I]. y );
If (pp [I]. y> len)
Flag = true;
Double vx = sqrt (len * len-pp [I]. y * pp [I]. y) * 1.0 );
Ll [I]. sp = pp [I]. x-vx;
Ll [I]. ep = pp [I]. x + vx;
}
If (flag | islen ){
Printf ("Case % d:-1 \ n", ca ++ );
Continue;
}
Sort (ll, ll + n, cmp );
Bool isuse [N];
Memset (isuse, false, sizeof (isuse ));
Int ans = 0;
For (int I = 0; I <n; ++ I ){
If (isuse [I])
Continue;
Isuse [I] = true;
Ans ++;
For (int j = I + 1; j <n; ++ j ){
If (isuse [j])
Continue;
If (ll [j]. sp <= ll [I]. ep ){
Isuse [j] = true;
}
}
}
Printf ("Case % d: % d \ n", ca ++, ans );
}
Return 0;
}

Related Article

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.