zoukankan      html  css  js  c++  java
  • poj1328 Radar Installation

    我们一眼就看出了这是一个贪心,只要排序即可。

    具体来说:按照x排序,然后尽量靠右放探测器。

    实际上我们还要用到一个转化:把一个点转化为x轴上的一段区间。

    然后我就成功WA了!

    然后发现少了一个关键语句:else if(a[i].r < now) now = a[i].r;

    之前的贪心策略是错误的。这样搞之后才是正确的贪心。

    (学习了使用pair)

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <algorithm>
     4 using namespace std;
     5 const int N = 1010;
     6 const double eps = 1e-10;
     7 int d;
     8 pair<int, int>island[N];// [x, y]
     9 pair<double, double>a[N];//[l, r]
    10 inline void cal(int x, int y, double& l, double& r) {
    11     double len = sqrt((double)(d * d - y * y));
    12     l = x - len;
    13     r = x + len;
    14     return;
    15 }
    16 int T;
    17 int main() {
    18     //freopen("in.in", "r", stdin);
    19     //freopen("my.out", "w", stdout);
    20     int n;
    21     scanf("%d%d", &n, &d);
    22     while(d || n) {
    23         bool boom = 0;
    24         for(int i = 1; i <= n; i++) {
    25             scanf("%d%d", &island[i].first, &island[i].second);
    26             if(island[i].second > d) {
    27                 boom = 1;
    28             }
    29             cal(island[i].first, island[i].second, a[i].first, a[i].second);
    30         }
    31         if(boom) {
    32             T++;
    33             printf("Case %d: -1
    ", T);
    34             scanf("%d%d", &n, &d);
    35             continue;
    36         }
    37         sort(a + 1, a + n + 1);
    38         double now = -0x3f3f3f3f;
    39         int ans = 0;
    40         for(int i = 1; i <= n; i++) {
    41             if(a[i].first - now > eps) {
    42                 ans++;
    43                 now = a[i].second;
    44             }
    45             else if(now - a[i].second > eps) {
    46                 now = a[i].second;
    47             }
    48         }
    49         T++;
    50         printf("Case %d: %d
    ", T, ans);
    51         scanf("%d%d", &n, &d);
    52     }
    53     return 0;
    54 }
    AC代码
  • 相关阅读:
    c# 读取数据库得到dateset
    c# 读数据库二进制流到图片
    c# 读取数据库得到字符串
    c#打开颜色对话框
    WinForm-GridView
    arcengine 常用方法
    arcgis engine 调用arcgis server服务
    ae
    ae保存图层
    ae 打开地图文档
  • 原文地址:https://www.cnblogs.com/huyufeifei/p/9014783.html
Copyright © 2011-2022 走看看