【LOJ#10002】噴水裝置
阿新 • • 發佈:2018-11-23
題目大意:給定一段區間 [l,r] ,N 條線段,求至少用多少條線段能夠覆蓋整個區間,不能覆蓋輸出-1。
題解:每次在起點小於當前位置的線段集合中選擇有端點最大的位置作為下一個位置,並更新答案,如果當前位置無法被更新,輸出 -1。
比較坑的一點是,輸入資料會有 r < W 的情況,需要對線段下標加以處理。
程式碼如下
#include <bits/stdc++.h> using namespace std; const int maxn=15010; int n,cnt; double L,W; struct node{ double l,r; }e[maxn]; bool cmp(const node& x,const node& y){ return x.l<y.l; } void read_and_parse(){ scanf("%d%lf%lf",&n,&L,&W),W/=2.0; double pos,r; for(int i=1;i<=n;i++){ scanf("%lf%lf",&pos,&r); if(r<W)continue; e[++cnt].l=pos-sqrt(r*r-W*W); e[cnt].r=pos+sqrt(r*r-W*W); } sort(e+1,e+cnt+1,cmp); } void solve(){ double now=0;int ans=0,i=1; while(now<L&&i<=cnt){ double r=now;bool flag=0; for(;i<=cnt&&now>=e[i].l;i++)if(r<e[i].r)r=e[i].r,flag=1; if(!flag){puts("-1");return;} ++ans,now=r; } printf("%d\n",ans); } int main(){ int T;scanf("%d",&T); while(T--){ cnt=0; read_and_parse(); solve(); } return 0; }