NYOJ12,喷水装置(二)
来源:互联网 发布:淘宝装修素材 编辑:程序博客网 时间:2024/04/30 01:37
原题链接:点击打开链接
这是一个贪心算法中的区间完全覆盖问题,详细解题思路点击打开链接
不同的是在化为区间完全覆盖问题之前需要先对其有效喷水面积进行筛选,再化为区间,然后问题就明朗化了,下面直接贴代码:
#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;struct sb{double from,to;}a[10005];bool cmp(sb t1,sb t2){if(t1.from<t2.from)return true;if(t1.from==t2.from&&t1.to>t2.to)return true;return false;}int main(){int t,n,i;double d,h,r,w,x;cin>>t;while(t--){int count=0,flag=0,sum=0;double begin=0,end=0;cin>>n>>w>>h;for(i=0;i<n;i++){cin>>x>>r;d=sqrt(r*r-((h/2)*(h/2)));if(r>=(h/2)){if(x-d>w||x+d<0)continue;a[count].from=x-d;a[count].to=x+d;count++;}}if(count==0){cout<<0<<endl;continue;}std::sort(a,a+count,cmp);if(a[0].from>0){cout<<0<<endl;continue;}double max=0;int num;for(i=0;i<count;i++){if(a[i].from<=end){for(int num=i;num<count;num++){if(a[num].to>max&&a[num].to>end&&a[num].from<=end){max=a[num].to;i=num;}}end=max;sum++;}if(end>w)break;}if(end<w||begin==end){cout<<0<<endl;continue;}cout<<sum<<endl;}return 0;}
- NYOJ12,喷水装置(二)
- NYOJ12喷水装置(二)
- nyoj12 喷水装置(二)
- nyoj12喷水装置(二)
- nyoj12 喷水装置(二)
- NYOJ12 喷水装置(二)
- 喷水装置二 nyoj12
- NYOJ12喷水装置(二)
- nyoj12 喷水装置(二)
- nyoj12-喷水装置(二)(贪心)
- 贪心nyoj12 喷水装置(二)
- NYOJ12 喷水装置(二)【贪心】
- NYOJ12 喷水装置(二)【贪心】
- nyoj12 喷水装置(二)贪心
- NYOJ12 喷水装置(二)(贪心)
- 喷水装置2(nyoj12)
- NYOJ12——喷水装置(二)(贪心算法)
- 贪心算法之——喷水装置二(nyoj12)
- java jxl 导出excel 附带导出图片和单元格样式
- 类是私有成员访问
- 龙芯下汇编语言编程(Hello world)
- Linux environ 环境变量指针
- AlarmManager
- NYOJ12,喷水装置(二)
- matlab梯度计算
- 从XSD生成JAVA类 IMS QTI规范实现之一
- 内存数据库FastDB和SQLite性能测评
- 锚点
- ovs 内置模块说明
- 改进向量空间模型
- Mongo 内存 锁机制
- [Javascript]获取窗口关闭事件