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

#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int main() {
  int64_t n, k;
  cin >> n >> k;

  vector<int64_t> a(n), b(n, 0);
  for (auto i = 0; i < n; i++)
    cin >> a[i];

  for (auto i = 0; i < k; i++) {
    bool flg = true;
    for (auto j = 0; j < n; j++) {
      auto left = max(j - a[j], 0L);
      auto right = min(j + a[j], n - 1);
      b[left]++;
      if (right + 1 < n)
        b[right + 1]--;
    }
    for (auto j = 1; j < n; j++) {
      b[j] += b[j - 1];
      if (b[j] != n)
        flg = false;
    }
    a = b;
    b.assign(n, 0);
    if (flg)
      break;
  }

  for (auto j = 0; j < n; j++)
    cout << a[j] << " ";
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int main() {
  int64_t n, k;
  cin >> n >> k;

  vector<int64_t> a(n);
  for (auto i = 0; i < n; i++)
    cin >> a[i];

  vector<int64_t> dp(n, 0);
  vector<int64_t> tmp(n, 0);
  for (auto i = 0; i < n; i++)
    dp[i] = a[i];
  for (auto i = 1; i <= k; i++) {
    tmp = dp;
    dp.assign(n, 0);
    for (auto j = 0; j < n; j++) {
      for (auto l = max(j - tmp[j], 0L); l <= min(j + tmp[j], n - 1); l++) {
        dp[l]++;
      }
    }
  }

  for (auto i = 0; i < n; i++)
    cout << dp[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

Int cnt;
Int N;

vector<Int> f(vector<Int> ans) {
  vector<Int> v(N, 0);
  rep(i, N) {
    int start, end;
    if (i >= ans[i])
      start = i - ans[i];
    else
      start = 0;
    if (i + ans[i] <= N - 1)
      end = i + ans[i];
    else
      end = N - 1;
    v[start]++;
    if (end < N - 1)
      v[end + 1]--;
  }
  vector<Int> w(N);
  w[0] = v[0];
  if (ans[0] != N && w[0] == N)
    cnt++;
  rep1(i, N - 1) {
    w[i] = w[i - 1] + v[i];
    if (ans[i] != N && w[i] == N)
      cnt++;
  }
  return w;
}

int main() {
  Int K;
  cin >> N >> K;
  if (K > 50)
    K = 50;
  vector<Int> A(N), ans(N);
  cnt = 0;
  rep(i, N) {
    cin >> A[i];
    ans[i] = A[i];
    if (A[i] == N)
      cnt++;
  }

  rep(i, K) {
    ans = f(ans);
    if (cnt == N)
      break;
  }

  if (N > 1)
    rep(i, N - 1) cout << ans[i] << " ";
  cout << ans[N - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

Int cnt;
Int N;

vector<Int> f(vector<Int> ans) {
  vector<Int> v(N, 0);
  rep(i, N) {
    int start, end;
    if (i >= ans[i])
      start = i - ans[i];
    else
      start = 0;
    if (i + ans[i] <= N - 1)
      end = i + ans[i];
    else
      end = N - 1;
    v[start]++;
    if (end < N - 1)
      v[end + 1]--;
  }
  vector<Int> w(N);
  w[0] = v[0];
  if (ans[0] != N && w[0] == N)
    cnt++;
  rep1(i, N - 1) {
    w[i] = w[i - 1] + v[i];
    if (ans[i] != N && w[i] == N)
      cnt++;
  }
  return w;
}

int main() {
  Int K;
  cin >> N >> K;
  vector<Int> A(N), ans(N);
  cnt = 0;
  rep(i, N) {
    cin >> A[i];
    ans[i] = A[i];
    if (A[i] == N)
      cnt++;
  }

  while (K-- > 0) {
    ans = f(ans);
    if (cnt == N)
      break;
  }

  if (N > 1)
    rep(i, N - 1) cout << ans[i] << " ";
  cout << ans[N - 1] << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  auto upd = [](const vector<int> &v) {
    vector<int> imos((int)v.size() + 2);
    for (int i = 1; i <= (int)v.size(); i++) {
      int l = max(0, i - v[i - 1]);
      int r = min((int)v.size() + 1, i + v[i - 1] + 1);
      imos[l]++;
      imos[r]--;
    }
    for (int i = 1; i < (int)imos.size(); i++) {
      imos[i] += imos[i - 1];
    }
    vector<int> res((int)v.size());
    for (int i = 1; i <= (int)v.size(); i++) {
      res[i - 1] = imos[i];
    }
    return res;
  };
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < min(50, k); i++) {
    a = upd(a);
  }
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  auto upd = [](const vector<int> &v) {
    vector<int> imos((int)v.size() + 2);
    for (int i = 1; i <= (int)v.size(); i++) {
      int l = max(0, i - v[i - 1]);
      int r = min((int)v.size() + 2, i + v[i - 1] + 1);
      imos[l]++;
      imos[r]--;
    }
    for (int i = 1; i < (int)imos.size(); i++) {
      imos[i] += imos[i - 1];
    }
    vector<int> res((int)v.size());
    for (int i = 1; i <= (int)v.size(); i++) {
      res[i - 1] = imos[i];
    }
    return res;
  };
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < min(50, k); i++) {
    a = upd(a);
  }
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <string>
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define len(s) s.length()
#define all(v) v.begin(), v.end()
const ll INF = LONG_MAX;
const int N = 2e5 + 5;
const ll mod = 1000000007;
using namespace std;

int a[N], b[N];
int n;
void solve() {
  int i;
  for (i = 0; i <= n; i++)
    b[i] = 0;

  for (i = 0; i < n; i++) {
    int l = max(i - a[i], 0);
    int r = min(i + a[i] + 1, n);

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

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

  for (i = 0; i < n; i++) {
    a[i] = b[i];
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);

  ll tc = 1;
  // cin >> tc;

  while (tc--) {

    int m, k, i, j;
    int x, y;

    cin >> n >> k;

    for (i = 0; i < n; i++)
      cin >> a[i];

    int ok = 0;

    // for (i = 0; i < n; i++) {
    // 	// printf("%d ", a[i]);
    // 	cout << a[i] << " ";
    // }
    // cout << endl;

    while (k--) {
      ok = 1;
      for (i = 0; i < n; i++) {
        ok &= (a[i] == n);
      }

      if (ok)
        break;

      solve();
    }

    for (i = 0; i < n; i++) {
      printf("%d ", a[i]);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <string>
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define len(s) s.length()
#define all(v) v.begin(), v.end()
const ll INF = LONG_MAX;
const int N = 2e5 + 5;
const ll mod = 1000000007;
using namespace std;

int a[N], b[N];
int n;
void solve() {
  int i;
  for (i = 0; i <= n; i++)
    b[i] = 0;

  for (i = 0; i < n; i++) {
    int l = max(i - a[i], 0);
    int r = min(i + a[i] + 1, n);

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

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

  for (i = 0; i < n; i++) {
    a[i] = b[i];
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);

  ll tc = 1;
  // cin >> tc;

  while (tc--) {

    int m, k, i, j;
    int x, y;

    cin >> n >> k;

    for (i = 0; i < n; i++)
      cin >> a[i];

    int ok = 0;

    // for (i = 0; i < n; i++) {
    // 	// printf("%d ", a[i]);
    // 	cout << a[i] << " ";
    // }
    // cout << endl;

    while (k--) {
      for (i = 0; i < n; i++) {
        ok &= (a[i] == n);
      }

      if (ok)
        break;

      solve();
    }

    for (i = 0; i < n; i++) {
      printf("%d ", a[i]);
    }
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int mylog(int N) {
  int s = 1;
  int i = 0;
  while (N > s) {
    s *= 2;
    i++;
  }
  return i;
}

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

  vector<long long> B(N, 0);
  K = min(K, 100 * mylog(N));
  for (int c = 0; c < K; c++) {
    for (int i = 0; i < N; i++)
      B[i] = 0;
    for (int i = 0; i < N; i++) {
      if (i - A[i] >= 0) {
        B[i - A[i]]++;
      } else {
        B[0]++;
      }

      if (i + A[i] + 1 < N) {
        B[i + A[i] + 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];
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
}

Error: TLE

Fixed submission:

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

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

  vector<long long> B(N, 0);
  for (int c = 0; c < K; c++) {
    for (int i = 0; i < N; i++)
      B[i] = 0;
    for (int i = 0; i < N; i++) {
      if (i - A[i] >= 0) {
        B[i - A[i]]++;
      } else {
        B[0]++;
      }

      if (i + A[i] + 1 < N) {
        B[i + A[i] + 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];
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

/// Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define ll long long
#define int ll
#define fi first
#define si second
#define mp make_pair
#define pb push_back
#define pi pair<ll, ll>
#define clr(x) memset(x, 0, sizeof(x));
#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)
#define done(i) cout << "done = " << i << endl;
#define show(x, y)                                                             \\
  cout << x << " : ";                                                          \\
  for (auto z : y)                                                             \\
    cout << z << " ";                                                          \\
  cout << endl;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

const ll inf = 1e18;
const int mod = 1e9 + 7;
const int M = 200005;
int a[M], n, k, cnt[M];
void work() {
  clr(cnt);
  f(i, 1, n) {
    int d = a[i];
    int lft = i - d;
    int rgt = i + d;
    lft = max(lft, 1LL);
    rgt = min(rgt, n);
    if (lft > rgt)
      continue;
    cnt[lft]++;
    cnt[rgt + 1]--;
  }
  f(i, 1, n) { cnt[i] += cnt[i - 1]; }
  f(i, 1, n) { a[i] = cnt[i]; }
}
main()

{
  fast cin >> n >> k;
  f(i, 1, n) { cin >> a[i]; }
  int tmp = 1000;
  tmp = min(tmp, k);
  while (tmp--) {
    work();
  }
  f(i, 1, n) { cout << a[i] << " "; }
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

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

const int maxN = 400010;

int A[2][maxN];

int main() {
  int n, m, k, i, j, p;
  scanf("%d%d", &n, &k);
  m = n + n;
  for (i = n; i < m; ++i)
    scanf("%d", A[0] + i);
  for (p = 0; p < k; ++p) {
    int cur = p & 1, nxt = 1 - cur;
    for (i = n; i < m; ++i)
      if (A[cur][i] != n)
        break;
    if (i == m)
      break;
    for (i = n; i < m; ++i)
      A[nxt][i] = 0;
    for (i = n; i < m; ++i) {
      ++A[nxt][i - A[cur][i]];
      --A[nxt][i + A[cur][i] + 1];
    }
    for (i = n; i < m; ++i)
      A[nxt][i] += A[nxt][i - 1];
  }
  for (i = n; i < m; ++i)
    printf("%d ", p < k ? n : A[k & 1][i]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i < (n); ++i)
const int INF = 1001001001;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<string>;
Graph field;
queue<P> q;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  rep(ki, k + 1) {
    vector<int> b(n + 2, 0);
    rep(i, n + 1) {
      int l = max(1, (int)(i - a[i]));
      int r = min(n + 1, (int)(i + a[i] + 1.5));
      b[l]++;
      b[r]--;
    }
    rep(i, n + 1) b[i + 1] += b[i];
    b.pop_back();
    if (a == b)
      break;
    a = b;
  }
  rep(i, n + 1) cout << a[i] << ' ';
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i < (n); ++i)
const int INF = 1001001001;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<string>;
Graph field;
queue<P> q;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  rep(i, n + 1) cin >> a[i];
  if (n <= log(k)) {
    rep(i, n + 1) cout << n << " ";
    cout << endl;
  }
  rep(i, k + 1) {
    ll b[n + 1];
    rep(l, n + 1) b[l] = 0;
    rep(j, n + 1) {
      for (int i = j - a[j] - 0.5 + 1; i <= j + a[j] + 0.5; i++) {
        b[i]++;
      }
    }
    rep(j, n + 1) a[j] = b[j];
  }
  rep(i, n + 1) cout << a[i] << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5 + 5;

int a[sz], b[sz];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    for (int i = 0; i < n; i++) {
      int x = max(0, i - a[i]);
      int y = min(n, i + a[i] + 1);
      b[x]++, b[y]--;
    }
    for (int i = 0, j = 0; i < n; i++) {
      j += b[i];
      a[i] = j, b[i] = 0;
    }
    bool no = 1;
    for (int i = 0; i < n; i++)
      if (a[i] < n)
        no = 0;
    if (no)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  puts("");
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 1e5 + 5;

int a[sz], b[sz];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    for (int i = 0; i < n; i++) {
      int x = max(0, i - a[i]);
      int y = min(n, i + a[i] + 1);
      b[x]++, b[y]--;
    }
    for (int i = 0, j = 0; i < n; i++) {
      j += b[i];
      a[i] = j, b[i] = 0;
    }
    bool no = 1;
    for (int i = 0; i < n; i++)
      if (a[i] < n)
        no = 0;
    if (no)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  puts("");
}
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;

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

  // 電球データ
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }

  // 電球更新処理
  while (K > 0) {

    // 終了判定
    bool flg = true;

    // 累積和
    vector<int> B(N, 0);
    for (int i = 0; i < N; i++) {
      int x = A[i];
      if (x != N)
        flg = false;

      if (i - x >= 0) {
        B[i - x]++;
      } else {
        B[0]++;
      }
      if (i + x + 1 < N) {
        B[i + x + 1]--;
      }
    }

    for (int i = 0; i < N - 1; i++) {
      B[i + 1] += B[i];
    }

    // 配列の更新処理
    A = B;
    K--;
    if (flg)
      break;
  }

  for (int x : A) {
    cout << x << " ";
  }
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
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]; }

  // 電球更新処理
  while (K > 0) {

    // 終了判定
    bool flg = true;

    vector<int> B(N, 1);
    for (int i = 0; i < N; i++) {
      int x = A[i];

      if (x != N)
        flg = false;

      // ライトが１以上
      int count = 1;
      while (x > 0) {
        if (i - count >= 0)
          B[i - count]++;
        if (i + count < N)
          B[i + count]++;
        count++;
        x--;
      }
    }
    // 配列の更新処理
    A = B;
    K--;
    if (flg)
      break;
  }

  for (int x : A) {
    cout << x << " ";
  }
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 0 false, 1 true
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
  std::cout << std::fixed << std::setprecision(15);
  ll n, k;
  cin >> n >> k;
  vector<long long> vec(n + 1);
  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }

  for (ll i = 0; i < k; i++) {
    vector<ll> imos(n + 1);

    for (ll j = 0; j < n; j++) {
      imos[max(j - vec[j], (ll)0)]++;
      imos[min(j + vec[j] + 1, n)]--;
    }

    rep(j, n + 1) {
      if (j > 0) {
        imos[j] += imos[j - 1];
      }
    }

    if (vec == imos) {
      break;
    }

    vec = imos;
  }

  rep(i, n) { cout << vec[i] << " "; }
  br;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 0 false, 1 true
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
  std::cout << std::fixed << std::setprecision(15);
  ll n, k;
  cin >> n >> k;
  vector<long long> vec(n + 1);
  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }

  for (ll i = 0; i < k; i++) {
    vector<ll> imos(n + 1);

    for (ll j = 0; j < n; j++) {
      imos[max(j - vec[j], (ll)0)]++;
      imos[min(j + vec[j] + 1, n)]--;
    }

    bool flag = true;
    rep(j, n + 1) {
      if (j > 0) {
        imos[j] += imos[j - 1];
      }
      if (imos[j] != n) {
        flag = false;
      }

      /*
      vec[j] = imos[j];
      if(vec[j] != n){
          flag = false;
      }
      */
    }

    vec = imos;

    if (flag) {
      break;
    }
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  for (int i = 0; i < min(K, 41); i++) {
    vector<int> tmp(N, 0);
    for (int j = 0; j < N; j++) {
      int l = max(0, j - A[j]);
      int r = min(N - 1, j + A[j]);
      tmp[l]++;
      if (r + 1 < N)
        tmp[r + 1]--;
    }
    for (int j = 0; j < N - 1; j++) {
      tmp[j + 1] += tmp[j];
    }

    A.swap(tmp);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  for (int i = 0; i < K; i++) {
    vector<int> tmp(N, 0);
    bool end = true;
    for (int j = 0; j < N; j++) {
      if (A[j] != N)
        end = false;

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

    if (end)
      break;

    A.swap(tmp);
  }

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

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


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int A[200001];
int B[200001];

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

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

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

    for (int k = 1; k < N; k++) {
      B[k] = B[k - 1] + B[k];
    }

    bool all_max = true;
    bool all_equal = true;
    for (int j = 0; j < N; j++) {
      if (A[j] != B[j])
        all_equal = false;
      A[j] = B[j];
      B[j] = 0;
      if (A[j] != N)
        all_max = false;
    }

    if (all_max)
      break;
  }

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

  return 0;
}

Error: TLE

Fixed submission:


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int A[200000];
int B[200000];

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

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

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

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

    if (all_max)
      break;
  }

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

  return 0;
}