alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int A[200010];
  rep(i, N) cin >> A[i];
  K = min(K, 460LL);
  while (K--) {
    int B[200010] = {};
    rep(i, N) {
      B[max(0LL, i - A[i])]++;
      B[min(N - 1, i + A[i]) + 1]--;
    }
    rep(i, N) B[i + 1] += B[i];
    rep(i, N) A[i] = B[i];
  }
  rep(i, N) {
    if (i != 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int A[200010];
  rep(i, N) cin >> A[i];
  K = min(K, 460LL);
  while (K--) {
    int B[200010] = {};
    rep(i, N) {
      REP(j, max(0LL, i - A[i]), min(N - 1, i + A[i])) { B[j]++; }
    }
    rep(i, N) A[i] = B[i];
  }
  rep(i, N) {
    if (i != 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define rep(i, n) for (int i = 0; i != (n); ++i)

using namespace std;

typedef long long ll;

int main() {
  int N, K;
  scanf("%d %d", &N, &K);

  ll A[N], B[N];
  rep(i, N) scanf("%lld", A + i);
  rep(i, N) B[i] = 0;

  int a, b, c;

  rep(ki, K) {
    rep(i, N) {
      a = i - A[i] >= 0 ? i - A[i] : 0;
      b = i + A[i] + 1 <= N ? i + A[i] + 1 : N;
      B[a] += 1;
      if (b < N) {
        B[b] -= 1;
      }
    }
    /*rep(i,N)cout<<B[i]<<" ";
    cout<<endl;*/
    for (int i = 0; i != N - 1; ++i) {
      B[i + 1] = B[i] + B[i + 1];
    }
    /* rep(i,N)cout<<B[i]<<" ";
     cout<<endl;*/
    bool flag = true;
    for (int i = 0; i != N; ++i) {
      if (A[i] != B[i]) {
        flag = false;
      }
      A[i] = B[i];
      B[i] = 0;
    }
    if (flag) {
      break;
    }
  }

  rep(i, N) { cout << A[i] << " "; }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

#define rep(i, n) for (int i = 0; i != (n); ++i)

using namespace std;

typedef long long ll;

int main() {
  int N, K;
  scanf("%d %d", &N, &K);

  ll A[N], B[N];
  rep(i, N) scanf("%lld", A + i);
  rep(i, N) B[i] = 0;

  int a, b, c;

  rep(ki, K) {
    rep(i, N) {
      a = i - A[i] >= 0 ? i - A[i] : 0;
      b = i + A[i] + 1 <= N ? i + A[i] + 1 : N;
      B[a] += 1;
      if (b < N) {
        B[b] -= 1;
      }
    }
    /*rep(i,N)cout<<B[i]<<" ";
    cout<<endl;*/
    for (int i = 0; i != N - 1; ++i) {
      B[i + 1] = B[i] + B[i + 1];
    }
    /* rep(i,N)cout<<B[i]<<" ";
     cout<<endl;*/
    bool flag = true;
    for (int i = 0; i != N; ++i) {
      A[i] = B[i];
      B[i] = 0;
      if (A[i] != B[i]) {
        flag = false;
      }
    }
    if (flag) {
      break;
    }
  }

  rep(i, N) { cout << A[i] << " "; }
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  rep(ki, k) {
    vector<int> b(n + 1);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(n - 1, i + a[i]);

      b[l]++;
      b[r + 1]--;
    }

    rep(i, n) b[i + 1] += b[i];
    b.pop_back();
    if (a == b)
      break;
    a = b;
  }

  rep(i, n) cout << a[i] << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  rep(h, k) {
    vector<int> ans(n, 0);
    for (int i = 0; i < n; ++i) {
      int l = max(0, i - a[i]);
      int u = min(n - 1, i + a[i]);
      for (int j = l; j <= u; ++j) {
        ans[j]++;
      }
    }

    a = ans;
  }

  rep(i, n) cout << a[i] << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> b(n, 0);
  for (int j = 0; j < min(k, 100); j++) {
    if (j != 0)
      b.assign(n, 0);
    for (int i = 0; i < n; i++) {
      int l = max(0, i - a[i]);
      int r = min(n - 1, i + a[i]);
      b[l]++;
      if (r + 1 < n)
        b[r + 1]--;
    }
    for (int i = 1; i < n; i++)
      b[i] += b[i - 1];
    for (int i = 0; i < n; i++)
      a[i] = b[i];
  }
  for (int i = 0; i < n; i++)
    cout << (b[i] >= n ? n : b[i]) << (i == n - 1 ? "\\n" : " ");
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  bool all_n = false;
  vector<P> start_goal(n);
  for (int x = 0; x < k; x++) {
    map<int, int> start;
    map<int, int> goal;
    for (int i = 0; i < n; i++) {
      start_goal[i].first = max(1, i + 1 - a[i]);
      start_goal[i].second = min(n, i + 1 + a[i]);
      start[start_goal[i].first]++;
      goal[start_goal[i].second]++;
    }
    int cnt = start[1];
    if (start_goal[0].second == n && start_goal[n - 1].first == 1) {
      all_n = true;
      break;
    }

    for (int i = 0; i < n; i++) {
      if (i != 0)
        cnt += start[i + 1];
      a[i] = (i < n - 1 ? cnt : goal[i + 1]);

      cnt -= goal[i + 1];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << (all_n ? n : a[i]) << (i == n - 1 ? "\\n" : " ");
  }
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2e5 + 10;
int n, k, A[maxn], tmp[maxn];

void solve() {
  cin >> n >> k;
  rep(i, 1, n) A[i] = read();
  rep(T, 1, k) {
    memset(tmp, 0, (n + 1) << 2);
    rep(i, 1, n) {
      int l = i - A[i], r = i + A[i];
      chkmax(l, 1), chkmin(r, n);
      tmp[l]++, tmp[r + 1]--;
    }
    bool flg = 1;
    rep(i, 1, n) {
      tmp[i] += tmp[i - 1];
      A[i] = tmp[i];
      flg &= A[i] == n;
    }
    if (flg)
      break;
  }
  rep(i, 1, n) printf("%d ", A[i]);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2e5 + 10;
int n, k, A[maxn], tmp[maxn];

void solve() {
  cin >> n >> k;
  rep(i, 1, n) A[i] = read();
  rep(T, 1, k) {
    memset(tmp, 0, (n + 1) << 2);
    rep(i, 1, n) {
      int l = i - A[i], r = i + A[i];
      tmp[l]++, tmp[r + 1]--;
    }
    bool flg = 1;
    rep(i, 1, n) {
      tmp[i] += tmp[i - 1];
      A[i] = tmp[i];
      flg &= A[i] == n;
    }
    if (flg)
      break;
  }
  rep(i, 1, n) printf("%d ", A[i]);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<pair<int, int>> x(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int left = i - a[i];
    int right = i + a[i];
    if (left < 0)
      left = 0;
    if (right > n - 1)
      right = n - 1;
    x[i] = make_pair(left, right);
  }
  // sort(x.begin(),x.end());
  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    ans[x[i].first]++;
    ans[x[i].second + 1]--;
  }
  for (int i = 0; i < n; ++i) {
    ans[i + 1] += ans[i];
  }
  k--;
  bool ok = false;
  vector<int> tmp(n + 1, 0);
  for (int i = 0; i < k; ++i) {
    if (ok)
      break;
    tmp = vector<int>(n + 1, 0);
    for (int j = 0; j < n; ++j) {
      if (ok)
        break;
      int left = j - ans[j];
      int right = j + ans[j];
      if (left < 0)
        left = 0;
      if (right > n - 1)
        right = n - 1;
      tmp[left]++;
      tmp[right + 1]--;
    }
    for (int m = 0; m < n; ++m) {
      tmp[m + 1] += tmp[m];
      ans[m + 1] = tmp[m + 1];
    }
    ans[0] = tmp[0];
    for (int l = 0; l < n; ++l) {
      if (ans[l] >= n) {
        ans[l] = n;
        ok = true;
      } else {
        ok = false;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ans[i] > n)
      ans[i] = n;
    if (i != n - 1) {
      cout << ans[i] << " ";
    } else {
      cout << ans[i] << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<pair<int, int>> x(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int left = i - a[i];
    int right = i + a[i];
    if (left < 0)
      left = 0;
    if (right > n - 1)
      right = n - 1;
    x[i] = make_pair(left, right);
  }
  // sort(x.begin(),x.end());
  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    ans[x[i].first]++;
    ans[x[i].second + 1]--;
  }
  for (int i = 0; i < n; ++i) {
    ans[i + 1] += ans[i];
  }
  k--;
  bool ok = false;
  vector<int> tmp(n + 1, 0);
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j) {
      if (ok)
        break;
      int left = j - ans[j];
      int right = j + ans[j];
      if (left < 0)
        left = 0;
      if (right > n - 1)
        right = n - 1;
      tmp[left]++;
      tmp[right + 1]--;
    }
    for (int m = 0; m < n; ++m) {
      tmp[m + 1] += tmp[m];
      ans[m + 1] = tmp[m + 1];
    }
    ans[0] = tmp[0];
    for (int l = 0; l < n; ++l) {
      if (ans[l] >= n) {
        ans[l] = n;
        ok = true;
      } else {
        ok = false;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ans[i] > n)
      ans[i] = n;
    if (i != n - 1) {
      cout << ans[i] << " ";
    } else {
      cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  int flag = 1, t = 0;
  while (flag) {
    flag = 0;
    vector<int> B(N + 1);
    rep(i, N) {
      int mmin = max(0, i - A[i]);
      int mmax = min(N, i + A[i] + 1);
      B[mmin] += 1;
      B[mmax] -= 1;
    }
    A[0] = B[0];
    for (int i = 1; i < N; ++i) {
      A[i] = A[i - 1] + B[i];
    }
    if (++t == K)
      break;
    rep(i, N) {
      if (A[i] != N) {
        flag = 1;
        break;
      }
    }
  }

  rep(i, N) { cout << A[i] << " "; }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  int flag = 1, t = 0;
  while (flag) {
    flag = 0;
    vector<int> B(N);
    rep(i, N) {
      int mmin = max(0, i - A[i]);
      int mmax = min(N - 1, i + A[i]);
      for (int j = mmin; j <= mmax; ++j) {
        ++B[j];
      }
    }
    A.swap(B);
    if (++t == K)
      break;
    rep(i, N) {
      if (A[i] != N) {
        flag = 1;
        break;
      }
    }
  }

  rep(i, N) { cout << A[i] << " "; }
  cout << endl;

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn], p[maxn];
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (k--) {
    bool flag = true;
    memset(p, 0, sizeof(p));
    for (int i = 1; i <= n; i++) {
      p[max(i - a[i], 1)]++;
      if (i + a[i] + 1 <= n)
        p[i + a[i] + 1]--;
    }
    a[1] = p[1];
    for (int i = 2; i <= n; i++) {
      a[i] = p[i] + a[i - 1];
      if (a[i] != n)
        flag = false;
    }
    if (flag)
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn], ans[maxn];
int n, k;
inline int read() {
  int s = 0, w = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar()) {
    if (c == '-')
      w = -1;
  }
  for (; isdigit(c); c = getchar()) {
    s = (s << 1) + (s << 3) + (c ^ 48);
  }
  return s * w;
}
bool inboard(int x) { return 1 <= x && x <= n; }
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (int i = 1; i <= min(n, k); i++) {
    for (int i = 1; i <= n; i++)
      ans[i] = 1;
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= a[j]; k++) {
        if (inboard(j - k))
          ans[j - k]++;
        if (inboard(j + k))
          ans[j + k]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      a[i] = ans[i];
    }
    sort(ans + 1, ans + n + 1);
    if (ans[1] == ans[n] && ans[1] == n)
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[200005], c[200005], n, k;
int main() {
  scanf("%d%d", &n, &k);
  register int i, j, t, s, f;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (j = 1; j <= k; j++) {
    memset(c, 0, sizeof(c));
    for (i = 1; i <= n; ++i) {
      ++c[max(1, i - a[i])];
      --c[min(n + 1, i + a[i] + 1)];
    }
    s = 0, f = 1;
    for (i = 1; i <= n; i++) {
      s += c[i];
      a[i] = s;
      if (a[i] < n)
        f = 0;
    }
    if (f)
      break;
  }
  for (i = 1; i <= n; i++) {
    printf("%d ", a[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int a[200005], dp[200005], rp[200005];
void light(int n, int k) {
  for (int i = 1; i <= k; i++) {
    int f = 1;
    for (int j = 1; j <= n; j++) {
      dp[j] = rp[j];
      rp[j] = 0;
      if (a[j] < n)
        f = 0;
    }
    if (f)
      return;
    for (int j = 1; j <= n; j++) {
      a[j] = dp[j];
      int l = 1 >= j - a[j] ? 1 : j - a[j], r = n <= j + a[j] ? n : j + a[j];
      for (; l <= r; l++)
        rp[l]++;
    }
  }
  return;
}
int main() {
  int n, k, max = -1;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int l = 1 >= i - a[i] ? 1 : i - a[i], r = n <= i + a[i] ? n : i + a[i];
    for (; l <= r; l++)
      rp[l]++;
  }
  light(n, k);
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

void Main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<int> B(N + 1, 0);
  for (int k = 0; k < K; ++k) {
    for (int i = 0; i < N; ++i) {
      B[max(0, i - A[i])] += 1;
      B[1 + min(N - 1, i + A[i])] -= 1;
    }
    for (int i = 1; i < N + 1; ++i) {
      B[i] += B[i - 1];
    }

    // for (int i = 0; i < N; ++i) {
    //     for (int j = max(0, i - A[i]); j <= min(N - 1, i + A[i]); ++j) {
    //         B[j] += 1;
    //     }
    // }

    bool horizon = true;
    for (int i = 0; i < N; ++i) {
      A[i] = B[i];
      B[i] = 0;
      if (A[i] != N) {
        horizon = false;
      }
    }
    if (horizon) {
      break;
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i > 0) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

void Main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<int> next(N, 0);
  vector<bool> needCheck(N, true);
  for (int k = 0; k < K; ++k) {
    for (int i = 0; i < N; ++i) {
      if (needCheck[i]) {
        for (int j = 0; j < N; ++j) {
          if (abs(i - j) <= A[j]) {
            next[i] += 1;
          }
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      A[i] = next[i];
      next[i] = 0;
      if (A[i] == N) {
        needCheck[i] = false;
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i > 0) {
      cout << " ";
      ;
    }
    cout << A[i];
  }
  cout << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// 答えみた
// imos法?
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  for (int t = 0; t < min(50, k); ++t) {
    vector<int> b(n, 0);
    // cout << "## " << t << endl;  // debug
    for (int i = 0; i < n; ++i) {
      ++b[max(0, i - a[i])];
      if (i + a[i] + 1 < n)
        --b[i + a[i] + 1];
    }
    // cout << "before: ";  // debug
    // for (int i = 0; i < n; ++i) cout << b[i] << " ";

    for (int i = 1; i < n; ++i)
      b[i] += b[i - 1];

    // cout << endl << "after: ";  // debug
    // for (int i = 0; i < n; ++i) cout << b[i] << " ";

    a = b;
    // cout << endl;  // debug
  }
  for (int i : a)
    cout << i << " ";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// 一旦そのまま書いてみる 時間足りなくなるパターンで
// #define MAX 200000
// int A[MAX];

struct Lamp {
  map<int, int> strong;
};

int main() {
  int N, K;
  cin >> N >> K;

  Lamp A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i].strong[0];

  int strong;
  int point = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = 0; j < N; j++) {
      strong = A[j].strong[i - 1];
      // cout << strong << endl;
      for (int k = -strong; k <= strong; k++) {
        if (j + k < 0)
          continue; // 0以下はデータなし
        if (j + k >= N)
          continue; // N以上はデータなし
        A[j + k].strong[i]++;
      }
      // A[j].strong[i]++; // 自らの電球をプラス うえのloopに入ってそう
    }
    if (A[0].strong[i] == N && A[N - 1].strong[i] == N) {
      point = i;
      break;
    }
  }
  if (point == 0)
    point = K;

  // result print
  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << A[i].strong[point];
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  while (K--) {
    if (count(A.begin(), A.end(), N) == N)
      break;
    vector<int> tmp(N + 1);
    for (int i = 0; i < N; i++) {
      tmp[max(0, i - A[i])]++;
      tmp[min(N, i + A[i] + 1)]--;
    }
    tmp.pop_back();
    partial_sum(tmp.begin(), tmp.end(), A.begin());
  }

  for (auto e : A) {
    cout << e << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  while (K--) {
    if (count(A.begin(), A.end() - 1, N) == N)
      break;
    vector<int> tmp(N + 1);
    for (int i = 0; i < N; i++) {
      tmp[max(0, i - A[i])]++;
      tmp[min(N, i + A[i] + 1)]--;
    }
    tmp.pop_back();
    partial_sum(tmp.begin(), tmp.end(), A.begin());
  }

  for (auto e : A) {
    cout << e << endl;
  }
  return 0;
}