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



#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
using ll = long long;

#define eb emplace_back
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define precision(n) fixed << setprecision(n)

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> void done(T ans) {
  cout << ans << "\\n";
  exit(0);
}

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 mt(seed);

clock_t time_p = clock();
void printTime() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\\n";
}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}

const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int main() {
  io();
  ll n;
  cin >> n;
  vector<ll> v{1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (ll i = 0; i < v.size(); i++) {
    int x = v[i] % 10;
    if (v.size() > n)
      break;
    if (x > 0 and x < 9) {
      v.push_back(v[i] * 10 + x - 1);
      v.push_back(v[i] * 10 + x);
      v.push_back(v[i] * 10 + x + 1);
    }
    if (x == 0) {
      v.push_back(v[i] * 10 + x);
      v.push_back(v[i] * 10 + x + 1);
    }
    if (x == 9) {
      v.push_back(v[i] * 10 + x - 1);
      v.push_back(v[i] * 10 + x);
    }
    // cout << v[i] << " ";
  }
  cout << v[n - 1];
}

Error: TLE

Fixed submission:



#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
using ll = long long;

#define eb emplace_back
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define precision(n) fixed << setprecision(n)

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> void done(T ans) {
  cout << ans << "\\n";
  exit(0);
}

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 mt(seed);

clock_t time_p = clock();
void printTime() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\\n";
}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}

const int mod = 1e9 + 7;
const int N = 1e5 + 5;
bool check(ll n) {
  vector<int> v;
  while (n) {
    v.push_back(n % 10);
    n /= 10;
  }
  for (int i = 1; i < v.size(); i++) {
    if (abs(v[i] - v[i - 1]) > 1)
      return false;
  }
  return true;
}
ll dp[100][100];
long long answer(int n) {

  if (n == 1)
    return 10;
  for (int j = 1; j <= 9; j++)
    dp[1][j] = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= 9; j++) {

      if (j == 0)
        dp[i][j] = dp[i - 1][j + 1] + dp[i - 1][j];

      else if (j == 9)
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];

      else
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j + 1] + dp[i - 1][j];
    }
  }

  long long sum = 0;
  for (int j = 1; j <= 9; j++)
    sum += dp[n][j];
  return 0;
}
void start(int i, int cnt, int k) {
  string s = "1";
  for (int j = 0; j < i - 1; j++) {
    s += '0';
  }
  ll ss = stoi(s);
  for (ll j = ss;; j++) {
    if (check(j)) {
      cnt++;
      if (cnt == k) {
        cout << j << "\\n";
        return;
      }
    }
  }
}
int main() {
  io();
  int k;
  cin >> k;
  int cnt = 0;
  for (ll i = 1; i <= 10000000; i++) {
    if (check(i)) {
      // cout << i << "\\n";
      cnt++;
      if (cnt == k) {
        cout << i << "\\n";
        return 0;
      }
    }
  }
  answer(10);
  ll sum = 0;
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= 9; j++) {
      // cout << dp[i][j] << " ";
      sum += dp[i][j];
    }
    if (sum >= k) {
      cout << i;

      start(i, sum, k);
    }
  }
}