码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 1615 Highway(高速公路)(贪心+区间选点)

时间:2017-02-09 21:24:47      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:min   贪心   1.0   include   左右   while   端点   ack   printf   

题意:给定平面上n(n<=105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过D。

分析:

1、根据D可以算出每个点在x轴上的可选区域,计算出区域的左右端点。

2、贪心选点,每次都选这个区域的最右端点,这样此端点可存在于尽可能多的区域。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Node{
    double l, r;
    void set(double ll, double rr){
        l = ll;
        r = rr;
    }
    bool operator<(const Node& rhs)const{
        return r < rhs.r || (r == rhs.r && l < rhs.l);
    }
}num[MAXN];
int main(){
    int L, D;
    while(scanf("%d%d", &L, &D) == 2){
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            double x, y;
            scanf("%lf%lf", &x, &y);
            double len = sqrt(D * D - y * y);
            num[i].set(Max(x - len, 0.0), Min(x + len, L));
        }
        sort(num, num + n);
        int cnt = 1;
        int pos = num[0].r;
        for(int i = 1; i < n; ++i){
            if(pos >= num[i].l && pos <= num[i].r) continue;
            pos = num[i].r;
            ++cnt;
        }
        printf("%d\n", cnt);
    }
    return 0;
}

 

UVA - 1615 Highway(高速公路)(贪心+区间选点)

标签:min   贪心   1.0   include   左右   while   端点   ack   printf   

原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6383797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!