Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }

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

      b[l]++;
      if (r < n) {
        b[r]--;
      }
    }

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

    a = b;
    ll flag = INF;
    for (ll i = 0; i < n; ++i) {
      flag = min(flag, b[i]);
    }
    if (flag == n) {
      break;
    }
  }
  for (ll i = 0; i < n; ++i) {
    cout << a[i] << ' ';
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }

  vector<ll> b(n, 0);
  for (ll i = 0; i < k; ++i) {
    for (ll j = 0; j < n; ++j) {
      ll x = a[j];
      for (ll t = 0; t < x; ++t) {
        if (j - t - 1 < 0) {
          break;
        }
        b[j - t - 1]++;
      }
      b[j]++;
      for (ll t = 0; t < x; ++t) {
        if (j + t + 1 > n) {
          break;
        }
        b[j + t + 1]++;
      }
    }
    ll flag = 0;
    for (ll j = 0; j < n; ++j) {
      a[j] = b[j];
      if (a[j] == n) {
        flag++;
      }
      b[j] = 0;
    }
    if (flag == n) {
      break;
    }
  }

  for (ll i = 0; i < n; ++i) {
    cout << a[i];
    if (i != n - 1) {
      cout << ' ';
    }
  }
  cout << endl;

  return 0;
}