1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 #include <set> 7 #include <map> 8 #include <stack> 9 10 using namespace std; 11 12 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 13 #define _rep(i,a,b) for(int i = (a);i <= (b);i ++) 14 typedef long long int ll; 15 16 struct cmp 17 { 18 inline bool operator() (const int a,const int b) 19 { 20 return a >= b; 21 } 22 }; 23 24 int readint() 25 { 26 int x; //vector<int> v;v.push_back(readint()) 27 scanf("%d",&x); 28 return x; 29 } 30 31 struct edge 32 { 33 int Left; 34 int Right; 35 }; 36 37 int main() 38 { 39 int n,r; 40 while(~scanf("%d %d",&n,&r)) 41 { 42 vector<pair<int,int>> v; 43 int heaterNum = 0; 44 for(int i = 0;i < n;i ++) 45 { 46 int tmp; 47 scanf("%d",&tmp); 48 if(tmp) 49 { 50 heaterNum ++; 51 int left,right; 52 left = i-r+1; 53 right = i+r-1; 54 if(i-r+1<0) 55 { 56 left = 0; 57 } 58 if(i+r-1>=n) 59 { 60 right = n-1; 61 } 62 v.push_back(make_pair(left,right)); 63 } 64 } 65 66 sort(v.begin(),v.end()); 67 // for(auto p:v) 68 // cout << p.first << " " << p.second << endl; 69 int test[n] {0}; 70 for(int i = 0;i < v.size();i ++) 71 { 72 for(int j = v[i].first;j <= v[i].second;j ++) 73 test[j] = 1; 74 } 75 int flag = 0; 76 for(auto d:test) 77 { 78 if(!d) 79 { 80 flag = 1; 81 } 82 } 83 if(flag) 84 { 85 cout << "-1" << endl; 86 continue; 87 } 88 89 int m[n] {0}; 90 int result = 0; 91 for(int i = 0;i < n;i ++) 92 { 93 if(m[i]==0) 94 { 95 for(int j = v.size()-1;j >= 0;j --) 96 { 97 if(v[j].first<=i) 98 { 99 result ++;100 for(int k = v[j].first;k <= v[j].second;k ++)101 m[k] = 1;102 break;103 }104 }105 }106 }107 cout << result << endl;108 }109 return 0;110 }
Codeforces-1066B heaters.