首页 > 代码库 > UVA - 1615 Highway(高速公路)(贪心+区间选点)
UVA - 1615 Highway(高速公路)(贪心+区间选点)
题意:给定平面上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(高速公路)(贪心+区间选点)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。