Highway UVA - 1615

本文通过模拟算法解决了一个关于区间覆盖的问题。对于给定的点集,在平面上找到这些点在特定线段上的作用区间,并统计最少需要多少个这样的区间来覆盖整个线段。文章详细展示了如何使用C++实现该算法,包括如何处理运行时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟,把每个点在S上的作用区间找到。
这道题疯狂runtime error,结果还是数组开小了,下次长记性了(;
这里写图片描述

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

struct node {
    double l;
    double r;
} p[10000];

int s;
int d;
int n;
int ans;
node t;

bool cmp(node & a, node & b){
   if (a.l == b.l) {
       return a.r < b.r;
   }
   return a.l < b.l;
}

int main() {
  //  freopen("input.txt", "r", stdin);
    int x, y;
    while(~scanf("%d", &s)) {
        scanf("%d", &d);
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            double r = sqrt((double)abs((double)(d * d - y * y)));
            p[i].l = 0 >= (x - r) ? 0 : x - r;
            p[i].r = s <= (x + r) ? s : x + r;
        }
        sort(p, p+n, cmp);
        ans = 1;
        t.l = p[0].l;
        t.r = p[0].r;
        for(int i = 1; i < n; i++) {
            if (t.r < p[i].l) {
                ans++;
                t.l = p[i].l;
                t.r = p[i].r;
            }
            else {
                t.l = p[i].l;
                t.r = min(t.r, p[i].r);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值