Poj-1328 二维化一维 典型贪婪算法

来源:互联网 发布:一个矩阵怎么计算 编辑:程序博客网 时间:2024/06/06 19:15

题意大概是x轴上方是海洋,海洋上分布着一些岛屿(用点坐标表示),你呢想在岸上安装雷达,使得雷达能扫到岛屿(即在雷达的范围内),求安装雷达的最小数量^-^

Poj-1328#include<cstdio>#include<algorithm>#include<cmath>const int INF = 1000000000;using namespace std;struct  Interval {      //interval是区间的意思    double left, right;};const int max_island = 1000 + 10;Interval the_interval[max_island];int the_num;double the_r;bool cmp(const Interval& a, const Interval& b) { //排序函数    if (a.right == b.right)        return a.left >= b.left;    return a.right < b.right;}int main() {    int the_case = 1;    while (scanf("%d%lf", &the_num, &the_r) == 2) {        if (the_num == 0 && the_r == 0)            break;        double point_x, point_y;        bool if_out_r = false;        for (int i = 0; i < the_num; i++) {            scanf("%lf%lf", &point_x, &point_y);            if (point_y > the_r) {//如果离海岸太远,超出雷达半径,我劝你还是放弃吧                if_out_r = true;            }            else {                the_interval[i].left = point_x - sqrt(pow(the_r, 2) - pow(point_y, 2));                the_interval[i].right = point_x + sqrt(pow(the_r, 2) - pow(point_y, 2));            }        }        if (if_out_r) {            printf("Case %d: -1\n", the_case++);        }        else {            sort(the_interval, the_interval + the_num, cmp);            double most_left = -INF; //double 我写成int找了很久,最大允许的左边区间点            int the_need_radar = 0;            for (int i = 0; i < the_num; i++) {                if (the_interval[i].left > most_left) {                        most_left = the_interval[i].right;                        the_need_radar++;                }            }            printf("Case %d: %d\n", the_case++, the_need_radar);        }    }    return 0;}

总结:
1.防止思路错误——我先前以为是像平移法一样把圆往右移,依次计算,导致我花了很长一段时间来写
2.审题要仔细,特别是那些对变量的描述的词汇,特别注意如数据类型,范围,输出格式······
3.编程时先写主函数,把框架先写好,遇到阻碍,在写细节。