ToJ 1115 poj 1328 radar installation greedy C/C ++

Source: Internet
Author: User
Tags radar

Greedy ideas:

The CoR [I] array stores the ranges that can be placed by radar covering the island, And then sorts them by the Left endpoint from small to large logarithm groups.

As long as the radar is placed in the CoR [I] storage range of an island, the island can be covered by this radar.

After sorting, there are only three adjacent intervals.

1. The second interval is completely behind the first interval.

2. the back part of the first interval overlaps with the front part of the second interval.

3. The second interval is included in the first interval.

First, place the first radar at the right endpoint of the first range.

1. if the second interval and the first interval belong to the first condition of the adjacent interval, the right endpoint of the first interval is obviously not in the second interval, A new radar is needed at the right endpoint of the second interval.

2. if the second interval and the first interval belong to the second condition, the right endpoint of the first interval is within the second interval, so the second island can be covered by this radar, no new radar is required.

3. in the third case, the right endpoint of the first interval is not in the second interval, but the right endpoint of the second interval is within the first interval, therefore, the radar is placed at the right endpoint of the second range and no new radar is needed.

After processing the second interval, the new radar (or the original radar is used without the new radar) is used as the standard to process the third interval.

Process the fourth and fifth parts in sequence until the processing is completed.

If the Y coordinate of an island is greater than D, the island cannot be overwritten by radar and output-1.

Source code

problem: 1328 User: yueashuxia
memory: 696 k time: 16 Ms
language: G ++ result: accepted
# Include <stdio. h> # include <string. h> # include <math. h >#include <iostream >#include <algorithm> using namespace STD; struct node {float L, R;} cor [1005]; bool CMP (node A, Node B) {return. L <B. l ;}int main () {int I, res, NO = 1, n; float D, D, X, Y, R; bool flag; while (scanf ("% d % F", & N, & D), N | D) {for (I = Flag = 0; I <n; I ++) {scanf ("% F", & X, & Y); If (Y> d) Flag = 1; if (flag = 0) {d = SQRT (D * D-y * Y); Cor [I]. L = x-D; Cor [I]. R = x + D ;}}if (FLAG) {printf ("case % d:-1 \ n", no ++); continue;} Sort (Cor, cor + N, CMP); Res = 1; r = cor [0]. r; for (I = 1; I <n; I ++) {If (Cor [I]. l> r) {res ++; r = cor [I]. r;} else if (Cor [I]. r <r) {r = cor [I]. r ;}} printf ("case % d: % d \ n", no ++, Res);} // system ("pause"); 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.