喷水装置(二) +区间覆盖-基础问题-贪心
2015-07-26 20:04
281 查看
数轴上有n个区间[ai,bi],选择尽量少的区间覆盖一条指定线段[s,t]。
贪心:
把各区间按照a从小到大排序,从前向后遍历,然后每次选择从当前起点S开始的最长区间,并以这个区间的右端点为新的起点,继续选择,直到找不到区间覆盖当前起点S或者S已经到达线段末端。
·
贪心:
把各区间按照a从小到大排序,从前向后遍历,然后每次选择从当前起点S开始的最长区间,并以这个区间的右端点为新的起点,继续选择,直到找不到区间覆盖当前起点S或者S已经到达线段末端。
·
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <cmath> using namespace std; struct qu { int x; int h; double l,r; } tm[10000+5]; double cmp(qu a,qu b) { return a.l<b.l; } int main() { double t; cin>>t; while(t--) { int n,w,h; cin>>n>>w>>h; int ok=0; int i,j; double t1,t2; for (i=1;i<=n;i++) { scanf("%lf %lf",&t1,&t2); if (t2<h/2) continue; tm[++ok].x=t1; tm[ok].l=t1-sqrt(t2*t2-(h*h*1.0/4.0)); tm[ok].r=t1+sqrt(t2*t2-(h*h*1.0/4.0)); tm[ok].h=t2; } sort(tm+1,tm+1+ok,cmp); int cun=0; double st=0; for (i=1;i<=ok&&tm[1].l<=st;i++) { double max=0; for (j=i;tm[j].l<=st&&j<=ok;j++) { if (max<tm[j].r) max=tm[j].r; } cun++; st=max; if (st>=w) break; i=j-1; } if (st>=w) cout<<cun<<endl; else printf("0\n"); } return 0; }
相关文章推荐
- 机房收费-组合查询
- 移植u-boot.2012.04.01
- 正整数
- HDU4287 字典树
- 找出java代码中占用cpu过多问题
- UVA 401
- Automatic Editing
- CSS元素分类
- LightOJ 1141 Program E
- UVA 10474 Where is the Marble?
- 由Handler引发的内存泄漏的思考
- 【Prism】MEF版UIComposition
- HDU 3973 AC's String 字符串哈希
- iostream
- sift是图像匹配的非常经典的算法
- DFS-POJ-1321-棋盘问题
- 【BMI指数计算器V2.0】项目实战
- GestureOverlayView(手势识别2)
- shape layer-list selector使用
- Watch The Movie(严格限制数量的二维费用背包)