Poj 1328 Radar Installation, pojradar
Radar InstallationTime Limit: 1000 MSMemory Limit: 10000 KTotal Submissions: 51377 Accepted: 11527
Idea: Each island has a range on the X axis that can be covered by radar, x1 is the left endpoint that can be covered, x2 is the right endpoint that can be covered, x1, x2 is about the X symmetry of the island.
Sort the island by x first, and then greedy.
Place len [I] in the interval of the first Island. right and the island behind len [k]. if left is greater than the former, it will continue to compare with the latter. Otherwise, it will update the range to the latter's range. At this time, ans + 1 (Note that if there is an island range
Len [k]. right <The former interval len [I]. right is updated to len [k]. right;
In short, the radar should try its best to the right side while meeting the current situation.
# Include <iostream> # include <cstdio> # include <cmath> # include <algorithm> # include <stdlib. h> using namespace std; struct Class {double x; double y;} zb [1005]; struct Std {double s; double e;} len [1005]; double r; int ans, n; bool cmp (Class a, Class B) {return. x <B. x;} void worklen () // calculate the corresponding radar-capable area of each island {for (int I = 0; I <n; I ++) {/* cout <len [I]. s <"" <len [I]. e <endl; */len [I]. s = zb [I]. x-sqrt (r * r-(zb [I]. y * zb [I]. y); len [I]. e = zb [I]. x + sqrt (r * r-(zb [I]. y * zb [I]. y) ;}} void work () {int j; int l = 0; for (j = 1; j <n; j ++) {if (len [j]. e <len [l]. e) {l = j; // when the radar placement area on island j is smaller than that on island l, consider the smallest value as the comparison standard} else if (len [l]. e <len [j]. s) {ans ++; l = j ;}return ;}int main () {int I, flag, t = 1; while (cin >>n> r, n | r) {flag = 0; ans = 1; for (I = 0; I <n; I ++) cin> zb [I]. x> zb [I]. y; for (I = 0; I <n; I ++) if (fabs (zb [I]. y)> r) {flag = 1;} if (flag) {cout <"Case" <t <":" <"-1" <endl; t ++;} else {sort (zb, zb + n, cmp);/* for (I = 0; I <n; I ++) cout <zb [I]. x <"" <zb [I]. y <endl; */worklen (); work (); cout <"Case" <t <":" <ans <endl; t ++ ;}} return 0 ;}
Poj1328 Radar Installation greedy proof
Greedy ideas:
First, calculate the radar coverage range of each island, sort by left endpoint, and after sorting, the first radar is established at the right endpoint of the first range.
Then judge the left endpoint of each interval in sequence. If it is on the right side of the newly established radar, a new radar is required and also built on the right side of the interval.
If the left endpoint is on the left of the radar, the right end of the range is on the left or right of the radar. If it is on the right, the Radar position remains unchanged. If it is on the left, now the radar cannot cover, so we need to put the radar at the right endpoint of the range, so that it can cover both the original Island and the current island.
Poj1328 what's wrong with me?
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
Const int n= 1000;
Const double EPS = 1e-7;
Struct E {double a, B;} e [N];
Int n, d;
Bool ava [N];
Double p [N];
Inline int dblcmp (double a, double B ){
If (fabs (a-B) <EPS) return 0;
If (a-B> 0) return 1;
Return-1;
}
Bool operator <(const E & a, const E & B ){
If (dblcmp (a. a, B. a) = 0)
Return dblcmp (a. B, B. B) = 1;
Return dblcmp (a. a, B. a) =-1;
}
Int main (){
Int I, j, x, y;
Int tc = 0;
While (scanf ("% d", & n, & d), n + d ){
Tc ++;
Int OK = 1;
For (I = 0; I <n; ++ I ){
Scanf ("% d", & x, & y );
If (y> d) OK = 0;
Double offset = sqrt (d * d-y * y );
E [I]. a = x-offset, e [I]. B = x + offset;
Ava [I] = 1;
}
If (! OK) {printf ("Case % d:-1 \ n", tc); continue ;}
Sort (e, e + n );
Int stack [N], top = 0;
Stack [top ++] = 0;
For (I = 1; I <n; ++ I ){
While (top> 0 & dblcmp (e [stack [top-1]. B, e [I]. B )! =-1 ){
Ava [stack [-- top] = 0;
}
Stack [top ++] = I;
}
For (I = 0 ;! Ava [I]; ++ I );
P [I] = e [I]. B;
Int cnt = 1;
For (I = I + 1; I <n; ++ I) if (ava [I]) {
For (j = I-1 ;! Ava [j]; j --);
If (p [j]> = e [I]. a) p [I] = p [j];
Else {p [I] = e [I]. B; cnt ++ ;}
}
Printf ("Case % d: % d \ n", tc, cnt );
}
Return 0;
}... Remaining full text>