ny12 喷水装置(二)
生活随笔
收集整理的這篇文章主要介紹了
ny12 喷水装置(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
噴水裝置(二)
時(shí)間限制:3000 ms ?|? 內(nèi)存限制:65535 KB 難度:4 描述每一組測試數(shù)據(jù)的第一行有三個(gè)整數(shù)n,w,h,n表示共有n個(gè)噴水裝置,w表示草坪的橫向長度,h表示草坪的縱向長度。
隨后的n行,都有兩個(gè)整數(shù)xi和ri,xi表示第i個(gè)噴水裝置的的橫坐標(biāo)(最左邊為0),ri表示該噴水裝置能覆蓋的圓的半徑。
如果不存在一種能夠把整個(gè)草坪濕潤的方案,請(qǐng)輸出0。
講解:屬于貪心最優(yōu)解問題
代碼 #include<algorithm> #include<iostream> #include<cstdio> #include<cmath> using namespace std; struct T { int a,b; }c[10010]; int cmp(T n,T m) { if(n.a<m.a) return 1; if(n.a==m.a && n.b>m.b) return 1; return 0; } int main() { int i,m,n,k=0; int w,h,x,r; scanf("%d",&m); while(m--) {int k=0; int max=-1; scanf("%d %d %d",&n,&w,&h); for(i=0;i<n;i++) { scanf("%d %d",&x,&r); if(2*r>h)//去掉不滿足條件的, { c[k].a=x-sqrt(r*r-h*h/4); if(c[k].a<0) c[k].a=0; c[k].b=sqrt(r*r-h*h/4)+x; if(c[k].b>w) c[k].b=w; if(c[k].b>max) max=c[k].b; k++;//重新統(tǒng)計(jì)數(shù)組的組數(shù) } }sort(c,c+k,cmp);//快排,防止超時(shí) if(c[0].a!=0 || max!=w) printf("0\n"); else { int count=0; int f=0; int start=0,last=0; while(start!=w) { for(i=f; i<k; i++) if(c[i].a<=start && c[i].b>last) { last=c[i].b; f=i+1; } if(last==start)//這種情況也無解 { count=0; break; } start=last;//從新定義邊界 count++; } cout<<count<<endl; } } return 0; } View Code
?
總結(jié)
以上是生活随笔為你收集整理的ny12 喷水装置(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python为类定义构造函数
- 下一篇: wordpress插件制作视频教程【资料