ZOJ-1360 | POJ-1328 -- radar installation

Source: Internet
Author: User
Tags radar

Zoj address: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 360

Poj address: http://poj.org/problem? Id = 1328

 

Solution: greedy

1 # include <stdio. h>
2 # include <string. h>
3 # include <stdlib. h>
4 # include <math. h>
5
6 struct P {
7 Double X, Y, left, right;
8} p [1010];
9
10 int CMP (const void * a, const void * B ){
11 struct p * c = (struct p *);
12 struct p * D = (struct p *) B;
13 return C-> left> D-> left? 1:-1;
14}
15
16 int main (){
17 int N, I, j, R, ANS, lose, Case = 1;
18 double D, X, DIS, high;
19 while (scanf ("% d", & N, & R )! = EOF ){
20 if (n = 0 & R = 0 ){
21 break;
22}
23 lose = 0;
24 d = (double) R;
25 For (I = 0; I <n; I ++ ){
26 scanf ("% lf", & P [I]. X, & P [I]. y );
27 if (P [I]. Y> d) {// If the left side of Y at a point is greater than the radar radius, it cannot be overwritten.
28 lose = 1;
29}
30 Dis = SQRT (D * D-P [I]. y * P [I]. y );
31 P [I]. Left = P [I]. X-DIS;
32 P [I]. Right = P [I]. x + DIS;
33}
34 if (lose = 1 ){
35 printf ("case % d:-1 \ n", Case ++ );
36 continue;
37}
38 qsort (p, N, sizeof (P [0]), CMP );
39 ans = 1;
40 high = P [0]. Right; // set the right endpoint of the first vertex to the farthest vertex.
41 for (I = 1; I <n; I ++ ){
42 if (P [I]. Left <= high ){
43 high = P [I]. Right <High? P [I]. Right: high; // If the left endpoint of the vertex is within the farthest vertex, update the farthest vertex.
44}
45 else {// If the left endpoint is not within the farthest point, the number of radars + 1 will update the farthest point
46 ans ++;
47 high = P [I]. Right;
48}
49}
50 printf ("case % d: % d \ n", Case ++, ANS );
51}
52 return 0;

53}

ZOJ-1360 | POJ-1328 -- radar installation

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.