Watering Grass (贪心,最小覆盖)

参考:  

https://blog.csdn.net/shuangde800/article/details/7828675

https://www.cnblogs.com/haoabcd2010/p/6171794.html?utm_source=itdadao&utm_medium=referral

 

 

 

复制代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 
 7 
 8 using namespace std;
 9 
10 struct node
11 {
12     double L;
13     double R; 
14 }a[10010];
15 
16 bool cmp(node a, node b)
17 {
18     return a.R > b.R;
19 }
20 
21 void sort_node(int n)
22 {
23     for (int i=0;i<n;i++)
24     {
25         int k=i;
26         for (int j=i+1;j<n;j++)
27         {
28             if (a[j].R>a[k].R)
29                 k=j;
30         }
31         swap(a[k],a[i]);
32     }
33 }
34 
35 int main() 
36 {
37     int n;
38     double length, wide;    // 都定义为double类型,因为使用勾股定理的时候肯定会算出小数
39     double center, radius;     // center为圆心,radius为半径 
40     double t;
41     while(scanf("%d%lf%lf", &n, &length, &wide) != EOF)
42     {
43         for(int i = 0; i < n; ++i)
44         {
45             cin >> center >> radius;
46             t = sqrt(radius*radius - (wide/2.0)*(wide/2.0));
47             a[i].L = center - t;
48             a[i].R = center + t;    
49         }
50         
51         //sort(a, a+n, cmp);    快排不知道为什么不能AC 
52         sort_node(n);
53         
54         double border = 0;
55         int cnt = 0;
56         while(border < length)  // 此类型题目的固定套路
57         {
58             int i;
59             for(i = 0; i < n; ++i)
60             {
61                 if(a[i].L <= border && a[i].R > border)
62                 {
63                     border = a[i].R;
64                     cnt++;
65                     break;
66                 }
67             }
68             
69             if(i == n)    // 如果i==n,说明遍历完了这n个区间,仍然没有找到符合条件的,即不能完全覆盖 
70                 break;
71         }
72         
73         if(border < length)
74             cout << -1 << endl;
75         else
76             cout << cnt << endl;
77     } 
78             
79     return 0;
80 }
复制代码

 

posted @   拾月凄辰  阅读(148)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示
主题色彩