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


// author-Shivam gupta

#include <bits/stdc++.h>

using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert(B <= A && A <= C)

#define MP make_pair
#define FOR(i, a) for (int i = 0; i < a; i++)

#define FOR1(i, j, a) for (int i = j; i < a; i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795

#define MOD 1000000007
const int N = 3e5 + 5;

#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout << x << '\\n'
#define out1(x) cout << x << " "
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<int> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
using vl = vector<ll>;

bool isPowerOfTwo(int x) {
  /* First x in the below expression is
    for the case when x is 0 */
  return x && (!(x & (x - 1)));
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

long long phi[max1], result[max1], F[max1];
// Precomputation of phi[] numbers. Refer below link
// for details : https://goo.gl/LUqdtY
void computeTotient() {
  // Refer https://goo.gl/LUqdtY
  phi[1] = 1;
  for (int i = 2; i < max1; i++) {
    if (!phi[i]) {
      phi[i] = i - 1;
      for (int j = (i << 1); j < max1; j += i) {
        if (!phi[j])
          phi[j] = j;

        phi[j] = (phi[j] / i) * (i - 1);
      }
    }
  }
  for (int i = 1; i <= 100000; i++) {
    for (int j = i; j <= 100000; j += i) {
      int p = j / i;
      F[j] += (i * phi[p]) % mod;
      F[j] %= mod;
    }
  }
}
int gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int x1, y1;
  int d = gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
  g = gcd(abs(a), abs(b), x0, y0);
  if (c % g) {
    return false;
  }

  x0 *= c / g;
  y0 *= c / g;
  if (a < 0)
    x0 = -x0;
  if (b < 0)
    y0 = -y0;
  return true;
}

int main() {
  fast();
  ll n, k;
  cin >> n >> k;
  ll a[n];
  FOR(i, n) { cin >> a[i]; }

  FOR(i, k) {
    vl t(n + 1, 0);
    FOR(j, n) {
      t[max(j - a[j], (ll)0)]++;
      t[min(n, j + a[j] + 1)]--;
    }
    int sum = 0;
    FOR(i, n) {
      sum += t[i];
      t[i] = sum;
    }
    bool win = true;
    FOR(j, n) {
      a[j] = t[j];

      if (a[j] != n) {
        win = false;
      }
    }
    if (win) {
      break;
    }
  }
  FOR(i, n) { cout << a[i] << " "; }

  return 0;
}


Error: TLE

Fixed submission:


// author-Shivam gupta

#include <bits/stdc++.h>

using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert(B <= A && A <= C)

#define MP make_pair
#define FOR(i, a) for (int i = 0; i < a; i++)

#define FOR1(i, j, a) for (int i = j; i < a; i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795

#define MOD 1000000007
const int N = 3e5 + 5;

#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout << x << '\\n'
#define out1(x) cout << x << " "
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<int> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
using vl = vector<ll>;

bool isPowerOfTwo(int x) {
  /* First x in the below expression is
    for the case when x is 0 */
  return x && (!(x & (x - 1)));
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

long long phi[max1], result[max1], F[max1];
// Precomputation of phi[] numbers. Refer below link
// for details : https://goo.gl/LUqdtY
void computeTotient() {
  // Refer https://goo.gl/LUqdtY
  phi[1] = 1;
  for (int i = 2; i < max1; i++) {
    if (!phi[i]) {
      phi[i] = i - 1;
      for (int j = (i << 1); j < max1; j += i) {
        if (!phi[j])
          phi[j] = j;

        phi[j] = (phi[j] / i) * (i - 1);
      }
    }
  }
  for (int i = 1; i <= 100000; i++) {
    for (int j = i; j <= 100000; j += i) {
      int p = j / i;
      F[j] += (i * phi[p]) % mod;
      F[j] %= mod;
    }
  }
}
int gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int x1, y1;
  int d = gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
  g = gcd(abs(a), abs(b), x0, y0);
  if (c % g) {
    return false;
  }

  x0 *= c / g;
  y0 *= c / g;
  if (a < 0)
    x0 = -x0;
  if (b < 0)
    y0 = -y0;
  return true;
}

int main() {
  fast();
  ll n, k;
  cin >> n >> k;
  ll a[n];
  FOR(i, n) { cin >> a[i]; }

  FOR(i, k) {
    vl t(n, 0);
    FOR(j, n) {
      FOR(l, n) {
        if (abs(j - l) <= a[l])
          t[j]++;
      }
    }
    bool win = true;
    FOR(j, n) {
      a[j] = t[j];
      if (a[j] != n) {
        win = false;
      }
    }
    if (win) {
      break;
    }
  }
  FOR(i, n) { cout << a[i] << " "; }

  return 0;
}