Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<math.h>
- #include<vector>
- #include <queue>
- #include <stack>
- #include <string>
- #include <algorithm>
- #include <climits>
- #include<regex>
- using namespace std;
- int n, k, a[100002];
- int s[100002];
- int lox = 0;
- void fun()
- {
- int counter = 0;
- for (int i = 1; i <= n; i++)
- {
- if (a[i] == 0)
- {
- if (i - k <= 1 && !s[1])
- s[1] = 1;
- else if (i - k > 1 && !s[i - k])
- s[i - k] = 1;
- if (k + i >= n)
- s[n] = 2;
- else
- s[k + i] = 2;
- }
- else
- {
- int z = i;
- int q = a[i];
- while (true)
- {
- if (z - k <= 1)
- s[1] = 1;
- else
- s[z - k] = 1;
- if (z + k >= n)
- s[n] = 2;
- else if (z+k< n)
- s[z + k] = 2;
- if (q != 0)
- z = q;
- q = a[z];
- if (q == 0)
- {
- if (z - k <= 1)
- s[1] = 1;
- else
- s[z - k] = 1;
- if (z + k >= n)
- s[n] = 2;
- else
- s[z + k] = 2;
- break;
- }
- }
- }
- int nach = 0, cur = 0;
- for (int j = 1; j <= n; j++)
- {
- if (s[j] == 1)
- {
- if (cur == 0)
- nach = j;
- cur++;
- }
- else if (nach == 0 && s[j] == 2)
- counter++;
- else if (s[j]==2)
- {
- cur--;
- if (cur == 0)
- {
- // if (j - nach == 1)
- counter += (j - nach) + 1;
- // else
- // counter += (j - nach);
- nach = 0;
- }
- }
- s[j] = 0;
- }
- cout << counter+lox << ' ';
- counter = 0;
- lox = 0;
- }
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> n >> k;
- for (int i = 1; i <= n; i++)
- cin >> a[i];
- fun();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement