The problem is to traverse from 1 to N, to find the farthest point in the unheated point to heat up to this point, or to see the code.
#include"bits/stdc++.h"using namespacestd;Const intinf=0x3f3f3f3f; typedefLong LongLL;inta[1005],ans;intMain () {intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); for(intI=1; i<=n;ans++){ //let J be equal to the farthest value that can be heated to I point, the range that can be heated to I point is [j-m+1,j+m-1]//But if j+m-1 is greater than N, then (n,j+m-1] This paragraph to abandon, if j-m+1 is less than 1, then [j-m+1,1] This paragraph also to abandon; intj=i+m-1;if(j>n) j=N; while(j&&j>j-m) { //The maximum point of the heater is found in the range that can be heated to the I point; if(a[j]==1) Break; J--; } //If 1 is not found within this range, then I will not be able to heat the point, I can directly output-1 and end the program if(!j| | j<=j-m) {Puts ("-1"); return 0; } //otherwise let I increase to J heating less than the minimum value is j+m;i=j+m; } printf ("%d", ans); return 0;}
Cf-1066b-heaters