fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int solve(vector<int> &A, int B) {
  5. int first=-1;
  6. int second=A.size();
  7. int flag=0;
  8. int cnt=0;
  9. for(int i=0;i<A.size();i++)
  10. {
  11. if(A[i]==1)
  12. {
  13. int f=i-B+1;
  14. int g=i+B-1;
  15. cout<<i<<" "<<f<<" "<<g<<endl;
  16.  
  17. if(flag==0)
  18. {
  19. first=f;
  20. second=g;
  21. cnt++;
  22. flag=1;
  23. }
  24. else
  25. {
  26. if(second>=f)
  27. {
  28. continue;
  29. }
  30. else
  31. {
  32. cnt++;
  33. second=g;
  34. }
  35.  
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42. }
  43.  
  44. }
  45. cout<<first<<" "<<second<<endl;
  46. if(first<=0 and second>=A.size()-1)
  47. {
  48. return cnt;
  49. }
  50. else
  51. {
  52. return -1;
  53. }
  54. }
  55.  
  56.  
  57. int main() {
  58. vector<int> A = { 1,1,1, 1};
  59. int B = 3;
  60.  
  61. int minLights =solve(A, B);
  62. cout << "Minimum lights to light the corridor: " << minLights << endl;
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.01s 5456KB
stdin
Standard input is empty
stdout
0 -2 2
1 -1 3
2 0 4
3 1 5
-2 2
Minimum lights to light the corridor: -1