Advertisement
Dmaxiya

社区服务 参考代码

Mar 10th, 2025
215
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 5000 + 100;
  6. int n, last;
  7. char str[maxn];
  8. int l[maxn], r[maxn];
  9.  
  10. int main() {
  11. #ifdef ExRoc
  12.     freopen("test.txt", "r", stdin);
  13. #endif
  14.     ios::sync_with_stdio(false);
  15.  
  16.     scanf("%d%s", &n, str + 1);
  17.     last = -1e7;
  18.     for (int i = 1; i <= n; ++i) {
  19.         if (str[i] == '1') {
  20.             last = i;
  21.         }
  22.         l[i] = last;
  23.     }
  24.     last = 1e7;
  25.     for (int i = n; i >= 1; --i) {
  26.         if (str[i] == '1') {
  27.             last = i;
  28.         }
  29.         r[i] = last;
  30.     }
  31.     for (int i = 1; i <= n; ++i) {
  32.         if (str[i] == '1') {
  33.             continue;
  34.         }
  35.         int ans = min(i - l[i], r[i] - i);
  36.         if (ans >= n) {
  37.             cout << -1 << " ";
  38.         } else {
  39.             cout << ans << " ";
  40.         }
  41.     }
  42.     cout << endl;
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement