Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include<math.h>
  3. #include<vector>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <algorithm>
  8. #include <climits>
  9. #include<regex>
  10. using namespace std;
  11.  
  12. int n, k, a[100002];
  13. int s[100002];
  14. int lox = 0;
  15. void fun()
  16. {
  17. int counter = 0;
  18. for (int i = 1; i <= n; i++)
  19. {
  20. if (a[i] == 0)
  21. {
  22. if (i - k <= 1 && !s[1])
  23. s[1] = 1;
  24. else if (i - k > 1 && !s[i - k])
  25. s[i - k] = 1;
  26.  
  27. if (k + i >= n)
  28. s[n] = 2;
  29. else
  30. s[k + i] = 2;
  31. }
  32. else
  33. {
  34. int z = i;
  35. int q = a[i];
  36. while (true)
  37. {
  38. if (z - k <= 1)
  39. s[1] = 1;
  40. else
  41. s[z - k] = 1;
  42.  
  43. if (z + k >= n)
  44. s[n] = 2;
  45. else if (z+k< n)
  46. s[z + k] = 2;
  47.  
  48. if (q != 0)
  49. z = q;
  50. q = a[z];
  51. if (q == 0)
  52. {
  53. if (z - k <= 1)
  54. s[1] = 1;
  55. else
  56. s[z - k] = 1;
  57.  
  58. if (z + k >= n)
  59. s[n] = 2;
  60. else
  61. s[z + k] = 2;
  62. break;
  63. }
  64. }
  65. }
  66. int nach = 0, cur = 0;
  67. for (int j = 1; j <= n; j++)
  68. {
  69. if (s[j] == 1)
  70. {
  71. if (cur == 0)
  72. nach = j;
  73. cur++;
  74. }
  75. else if (nach == 0 && s[j] == 2)
  76. counter++;
  77. else if (s[j]==2)
  78. {
  79. cur--;
  80. if (cur == 0)
  81. {
  82. // if (j - nach == 1)
  83. counter += (j - nach) + 1;
  84. // else
  85. // counter += (j - nach);
  86. nach = 0;
  87. }
  88. }
  89. s[j] = 0;
  90. }
  91. cout << counter+lox << ' ';
  92. counter = 0;
  93. lox = 0;
  94. }
  95. }
  96.  
  97. int main()
  98. {
  99. freopen("input.txt", "r", stdin);
  100. freopen("output.txt", "w", stdout);
  101.  
  102.  
  103. cin >> n >> k;
  104. for (int i = 1; i <= n; i++)
  105. cin >> a[i];
  106.  
  107. fun();
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement