首页 > 其他分享 >POJ--1328 Radar Installation(贪心)

POJ--1328 Radar Installation(贪心)

时间:2023-05-01 09:33:52浏览次数:54  
标签:Case typedef Installation radar -- 1328 int sea include

记录
0:50 2023-5-1

http://poj.org/problem?id=1328

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.

Figure A Sample Input of Radar Installations

Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

贪心,我一开始的思路是错误的。一开始想先将点从左到右排序起来,让后寻找可以覆盖这个点的雷达,越右边越好,这样可以尽可能的覆盖多的点。这样想是不对的,用的雷达会多。
合理的做法是,以岛为中心画圆,和x轴有最多俩个交点,这个区间(使用一元二次函数准确计算为实数)内的点都是可以选的,将所有岛对应的区间算出来,排序后计算重叠的局域,有多少个重叠的区域就有多少的个解。
注意:求重叠区域后需要将判断的范围缩小到重叠区域

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<cmath>
using namespace std;
#define MAX_N 1000
typedef long long ll;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;
typedef struct {
    int x;
    int y;
} P;
typedef struct {
    float x;
    float y;
} Q;
P v[MAX_N + 1];
int k = 0;
Q region[MAX_N + 1];
int N, D;
int Case = 1;

bool comp(Q lhs, Q rhs) {
    return (lhs.x < rhs.x) || (lhs.x == rhs.x && lhs.y > rhs.y);
}

void solve() { 
    int result = N;
    k = 0;
    for(int i = 0; i < N; i++) {
        int x = v[i].x;
        if(D <= 0 || v[i].y < 0) {
            result = -1;
            break;
        }

        float a = 1, b = -2 * v[i].x, c = v[i].x * v[i].x + v[i].y * v[i].y - D * D;
        Q q;
        q.x = (-b - sqrt(b * b - 4 * a *c)) / (2 * a);
        q.y = (-b + sqrt(b * b - 4 * a *c)) / (2 * a);
        if((b * b - 4 * a *c) < 0) {
            result = -1;
            break;
        }
        region[k] = q;
        k++;
    }
    if(result != -1) {
        sort(region, region + k, comp);
        for(int i = 0; i < k; i++) {
            float x1 = region[i].x, y1 = region[i].y;
            while (i + 1 < k) {
                float x2 = region[i + 1].x, y2 = region[i + 1].y;
                if(y1 >= x2) {
                    result--;
                    x1 = max(x1, x2);
                    y1 = min(y1, y2);
                    i++;
                } else {
                    break;
                }
            }  
        }
    }
    printf("Case %d: %d\n", Case, result);
    Case++;
}

int main() {
    while (~scanf("%d %d", &N, &D)) {
        if(N == 0 && D == 0) break;
        for(int i = 0; i < N; i++) {
            scanf("%d %d", &(v[i].x), &(v[i].y));
        }
        solve();
    }
    return 0;
}   

标签:Case,typedef,Installation,radar,--,1328,int,sea,include
From: https://www.cnblogs.com/57one/p/17366175.html

相关文章

  • CF1624G 题解
    前言题目传送门!更好的阅读体验?比较好玩的二进制题目。思路答案最小,也就是说较高位要尽可能小。所以很容易想到从最高位开始枚举。第\(i\)位为\(0\),等价于选出的所有边的第\(i\)位都为\(0\)。同时,由于我们是贪心,如果之前枚举过的第\(j\)位可以是\(0\),那么这两个条件......
  • reverse_1
    依旧先查壳(看几位)没壳64位考虑IDA或者OD都行(看个人习惯,OD需要很大的功底)建议先从IDA开始拖入IDA看看发现没有想要的东西-->shift+F12-->(可以ctrl+F)-->也可一个个找关键字flag发现rightflag-->点进去在数据段上(不能操作)没有任何作用-->ctrl+x(查看是谁调用了......
  • 次小生成树(Prim + Kruaskal)
    问题引入:我们先来回想一下生成树是如何定义的,生成树就是用n-1条边将图中的所有n个顶点都连通为一个连通分量,这样的边连成子树称为生成树。最小生成树很明显就是生成树中权值最小的生成树,那么我们即将要学的次小生成树或者K小生成树是怎么定义的呢,很明显就是生成树中权......
  • c#-随机数组
    publicstaticint[]GenerateRandowArray(intmaxSize,intmaxValue){Randomrd=newRandom();int[]arr=newint[(int)((maxSize+1)*rd.NextDouble())];for(inti=0;i<arr.Length;i++){arr[i]=(in......
  • 230501 TI - Engineer It - How to test power supplies - Overview
    Hi,I'mBobHanrahan,ApplicationEngineeringatTexasInstruments.Thisisthefirstonaseriesontestingpowersupplies.We'llbetalkingaboutsomeofthebasictests,notall,butthebasictestsneededtoensureareliablepowersupply.......
  • easyre
    很简单的一个小练手这里习惯性的先查壳(养成习惯)(也可以查是几位的)无壳不管是进OD还是IDA都是直接出答案拖入OD如图拖入IDA很明显flag不需要动什么脑子,会拖入就行......
  • 初识类
    实例封装一个“Point类”来实现平面上的点的操作。1、声明classclass_name{[private:]成员;public:成员;protected:成员;};<1>class_name:类名,一般首字母大写<2>private,public,protected:访问权限<3>成员:数据,函数<4>声明时不会为类分......
  • CF 1709E XOR Tree(树上启发式合并)
    题目链接:https://codeforces.com/contest/1709/problem/E解题思路:定义sum(x,y)为x→y路径上的点的异或和,dx 为x→root路径上的点的异或和。对于一个点权树,sum(x,y)=dx ^dy ^vallca(x,y)。考虑修改一个点,可以将它改为一个很大的2为底数的幂,则经过此点的所有的不合......
  • MySQL基础命令 | ChatGPT问答记录
    问:MySQL基础命令ChatGPT:MySQL是一种流行的开源关系型数据库管理系统(RDBMS),以下是一些常见的MySQL基础命令:连接到MySQL服务器:mysql-uusername-ppassword-hhostname创建数据库:CREATEDATABASEdatabase_name;删除数据库:DROPDATABASEdatabase_name;选......
  • OOP第四到第六次训练总结
    一、前言本文章主要是对作者大学编程学习的记录,本篇文章主要是对OOP的第四到六次训练的总结。现如今,我已经正式的进入了OOP的学习,难度也确实逐渐在提升,这三次作业与前三次比较起来,代码量和难度都有了明显的提升,已经是一个新的阶段,而三次训练一次总结也恰好将学习分为了不同阶段......