NYOJ-12 喷水装置2(贪心 区间覆盖)

来源:互联网 发布:融资租赁软件市场 编辑:程序博客网 时间:2024/05/20 10:56
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#define MAX 10000+1using namespace std;struct node{double left,right;};bool cmp_left(struct node a, struct node b){return a.left<b.left;}int main(){int T;int n,w,h;int count,flag;double circle_x,circle_r,half,now_len,Max;struct node a[MAX]; cin>>T;while(T--){count=0;//所需装置的个数flag=1;now_len=0;//已覆盖的长度cin>>n>>w>>h;half=h/2.000000;for(int i=0;i<n;i++){double temp;cin>>circle_x>>circle_r;temp=sqrt(circle_r*circle_r-half*half);if(temp>0){a[i].left=circle_x-temp;//左端点a[i].right=circle_x+temp;//右端点}}sort(a,a+n,cmp_left);//左端点从小到大排序while(now_len<w){Max=0;for(int i=0;i<n&&a[i].left<=now_len;i++){if((a[i].right-now_len)>Max) Max=a[i].right-now_len;//用来选出左端点满足条件的线段中,右端点最大的线段}if(Max==0){flag=0;//没有左端点符合的break;}else{count++;now_len+=Max;//更新覆盖到的区间}}if(flag==1) cout<<count<<endl;else cout<<"0"<<endl;}return 0;}

0 0
原创粉丝点击