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


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return halfpower * halfpower;
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return (halfpower * halfpower) * a;
    }
  }
};

int main() {
  cout.precision(17);
  ll modd = 1000 * 1000 * 1000 + 7;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    readint(k);
    vector<int> events(n + 1, 0);
    forr(i, 0, n) {
      readint(kk);
      events[max(0, i - kk)]++;
      events[min(i + kk + 1, n)]--;
    }
    k--;

    while (k > 0) {
      vector<int> ev_new(n + 1, 0);
      int val = 0;
      bool all_val_n = true;
      forr(i, 0, n) {
        val += events[i];
        all_val_n = all_val_n && (val == n);
        ev_new[max(0, i - val)]++;
        ev_new[min(i + val + 1, n)]--;
      }
      events = move(ev_new);
      if (all_val_n) {
        break;
      }
      k--;
      if (k == 0) {
        break;
      }
    }

    int val = 0;
    forr(i, 0, n) {
      val += events[i];
      cout << val << " ";
    }
    cout << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return halfpower * halfpower;
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return (halfpower * halfpower) * a;
    }
  }
};

int main() {
  cout.precision(17);
  ll modd = 1000 * 1000 * 1000 + 7;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    readint(k);
    multiset<pair<int, int>> events;
    forr(i, 0, n) {
      readint(kk);
      events.insert(make_pair(max(0, i - kk), -1));
      events.insert(make_pair(min(i + kk, n - 1), 1));
    }
    k--;

    while (k > 0) {
      multiset<pair<int, int>> ev_new;
      auto it = events.begin();
      int val = 0;
      bool all_val_n = true;
      forr(i, 0, n) {
        while ((it->first < i) || ((it->first == i) && (it->second == -1))) {
          val += -it->second;
          ++it;
        }
        all_val_n = all_val_n && (val == n);
        ev_new.insert(make_pair(max(0, i - val), -1));
        ev_new.insert(make_pair(min(i + val, n - 1), 1));
      }
      events = move(ev_new);
      if (all_val_n) {
        break;
      }
      k--;
      if (k == 0) {
        break;
      }
    }

    int val = 0;
    auto it = events.begin();
    forr(i, 0, n) {
      while ((it->first < i) || ((it->first == i) && (it->second == -1))) {
        val += -it->second;
        ++it;
      }
      cout << val << " ";
    }
    cout << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}