码迷,mamicode.com
首页 > 其他好文 > 详细

CF1066B Heaters

时间:2018-10-18 13:58:12      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:贪心   span   using   turn   ret   class   ios   max   out   

思路:

从左向右贪心选择能覆盖当前位置的最靠右的那个heater即可,和poj radar installation类似。

实现:

 1 #include <iostream>
 2 #include <cassert>
 3 using namespace std;
 4 
 5 const int INF = 0x3f3f3f3f;
 6 
 7 int a[1005], cov[1005];
 8 
 9 int main()
10 {
11     int n, r;
12     while (cin >> n >> r)
13     {
14         fill(cov + 1, cov + n + 1, -1);
15         for (int i = 1; i <= n; i++) cin >> a[i];
16         for (int i = 1; i <= n; i++)
17         {
18             for (int j = max(1, i - r + 1); j <= min(n, i + r - 1); j++)
19             {
20                 if (a[j] == 1) cov[i] = j; 
21             }
22         }
23         int maxn = -INF, cnt = 0;
24         bool flg = true;
25         for (int i = 1; i <= n; i++)
26         {
27             if (maxn >= i - r + 1 && maxn <= i + r - 1) continue;
28             else if (cov[i] == -1) { flg = false; break; }
29             else    
30             {
31                 maxn = cov[i];
32                 cnt++;
33             }
34         }
35         cout << (flg ? cnt : -1) << endl;
36     }
37     return 0;
38 }

 

CF1066B Heaters

标签:贪心   span   using   turn   ret   class   ios   max   out   

原文地址:https://www.cnblogs.com/wangyiming/p/9809880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!