生活随笔
收集整理的這篇文章主要介紹了
喷水装置(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
噴水裝置(二)
時間限制:
3000?ms ?|? 內(nèi)存限制:65535?KB 難度:
4 描述有一塊草坪,橫向長w,縱向長為h,在它的橫向中心線上不同位置處裝有n(n<=10000)個點狀的噴水裝置,每個噴水裝置i噴水的效果是讓以它為中心半徑為Ri的圓都被潤濕。請在給出的噴水裝置中選擇盡量少的噴水裝置,把整個草坪全部潤濕。 輸入第一行輸入一個正整數(shù)N表示共有n次測試數(shù)據(jù)。
每一組測試數(shù)據(jù)的第一行有三個整數(shù)n,w,h,n表示共有n個噴水裝置,w表示草坪的橫向長度,h表示草坪的縱向長度。
隨后的n行,都有兩個整數(shù)xi和ri,xi表示第i個噴水裝置的的橫坐標(最左邊為0),ri表示該噴水裝置能覆蓋的圓的半徑。輸出每組測試數(shù)據(jù)輸出一個正整數(shù),表示共需要多少個噴水裝置,每個輸出單獨占一行。
如果不存在一種能夠把整個草坪濕潤的方案,請輸出0。樣例輸入 2
2 8 6
1 1
4 5
2 10 6
4 5
6 5 樣例輸出 1
2 來源《算法藝術與信息學競賽》上傳者張云聰解題:貪心大法!計算出每個噴頭的圓形覆蓋與坐標y = h/2的兩個交點的橫坐標!然后以左邊的橫坐標進行從小到大進行排序!然后進行貪心!每次選取覆蓋最靠右的那個! 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <vector>
6 #include <climits>
7 #include <algorithm>
8 #include <cmath>
9 #define LL long long
10 using namespace std;
11 struct point{
12 int lt,rt;
13 }p[10010];
14 bool cmp(const point &a,const point &b){
15 return a.lt < b.lt;
16 }
17 int main(){
18 int kase,n,w,h,i,x,r,ans,cur,v;
19 double temp;
20 bool flag;
21 scanf("%d",&kase);
22 while(kase--){
23 scanf("%d %d %d",&n,&w,&h);
24 for(i = 0; i < n; i++){
25 scanf("%d %d",&x,&r);
26 temp = r*r*1.0 - h*h/4.0;
27 if(temp > 0) temp = sqrt(temp);
28 else temp = 0;
29 p[i].lt = x - temp;
30 p[i].rt = x + temp;
31 }
32 sort(p,p+n,cmp);
33 ans = cur = 0;
34 flag = true;
35 while(cur < w){
36 v = 0;
37 for(i = 0; i < n && p[i].lt <= cur; i++){
38 v = max(v,p[i].rt);
39 }
40 if(v <= cur){flag = false;break;}
41 cur = v;
42 ans++;
43 }
44 printf("%d\n",flag?ans:0);
45 }
46 return 0;
47 } View Code ?
轉(zhuǎn)載于:https://www.cnblogs.com/crackpotisback/p/3830471.html
總結(jié)
以上是生活随笔為你收集整理的喷水装置(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。