Problem p03318 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define fi first
#define se second
#define show(x)                                                                \
  cerr << #x << "="                                                            \
       << "\n"
typedef long long ll;
typedef unsigned long long ull;

using namespace std;
const ll llinf = LLONG_MAX;
const ull ullinf = ULLONG_MAX;

int n;
vector<pair<double, ll>> v;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  // FILE *stream2;
  stream1 = freopen("in.txt", "r", stdin);
  // stream2 = freopen("out.txt", "w", stdout);
  if (stream1 == NULL)
    return 0;
    // if (stream2 == NULL) return 0;
#endif
  /*vector<pair<long double, int>> v;
  for (int i = 1; i < 10000001; i++) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(long double)i / res, i});
  }
  sort(v.begin(), v.end());
  int now = 0;
  for(int i = 0; i < (int)v.size(); i++) {
    if (now < v[i].se) {
      now = v[i].se;
      cout << v[i].se << ": " << v[i].fi << "\n";
    }
  }*/
  cin >> n;
  for (ll i = 1; i <= 9; i++) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 19; i <= 99; i += 10) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 199; i <= 999; i += 100) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1099; i <= 10000; i += 100) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10999; i <= 100000; i += 1000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 109999; i <= 1000000; i += 10000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1009999; i <= 10000000; i += 10000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10099999; i <= 100000000; i += 100000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100099999; i <= 1000000000; i += 100000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1000999999; i <= 10000000000; i += 1000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10000999999; i <= 100000000000; i += 1000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100009999999; i <= 1000000000000; i += 10000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1000009999999; i <= 10000000000000; i += 10000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10000099999999; i <= 100000000000000; i += 100000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100000099999999; i <= 1000000000000000; i += 100000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  sort(v.begin(), v.end());
  ll now = 0;
  int cont = 0;
  for (int i = 0; i < (int)v.size(); i++) {
    if (now < v[i].se) {
      now = v[i].se;
      printf("%lld\n", v[i].se);
      cont++;
      if (cont == n)
        return 0;
    }
  }
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  // fclose(stream2);
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define fi first
#define se second
#define show(x)                                                                \
  cerr << #x << "="                                                            \
       << "\n"
typedef long long ll;
typedef unsigned long long ull;

using namespace std;
const ll llinf = LLONG_MAX;
const ull ullinf = ULLONG_MAX;

int n;
vector<pair<double, ll>> v;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  // FILE *stream2;
  stream1 = freopen("in.txt", "r", stdin);
  // stream2 = freopen("out.txt", "w", stdout);
  if (stream1 == NULL)
    return 0;
    // if (stream2 == NULL) return 0;
#endif
  /*vector<pair<long double, int>> v;
  for (int i = 1; i < 10000001; i++) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(long double)i / res, i});
  }
  sort(v.begin(), v.end());
  int now = 0;
  for(int i = 0; i < (int)v.size(); i++) {
    if (now < v[i].se) {
      now = v[i].se;
      cout << v[i].se << ": " << v[i].fi << "\n";
    }
  }*/
  cin >> n;
  for (ll i = 1; i <= 9; i++) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 19; i <= 99; i += 10) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 199; i <= 999; i += 100) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1099; i <= 10000; i += 100) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10999; i <= 100000; i += 1000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 109999; i <= 1000000; i += 10000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1009999; i <= 10000000; i += 10000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10099999; i <= 100000000; i += 100000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100099999; i <= 1000000000; i += 100000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1000999999; i <= 10000000000; i += 1000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10000999999; i <= 100000000000; i += 1000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100009999999; i <= 1000000000000; i += 10000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 1000009999999; i <= 10000000000000; i += 10000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 10000099999999; i <= 100000000000000; i += 100000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  for (ll i = 100000999999999; i <= 1000000000000000; i += 1000000000) {
    string s = to_string(i);
    int res = 0;
    for (int j = 0; j < (int)s.size(); j++) {
      res += s[j] - '0';
    }
    v.push_back({(double)i / res, i});
  }
  sort(v.begin(), v.end());
  ll now = 0;
  int cont = 0;
  for (int i = 0; i < (int)v.size(); i++) {
    if (now < v[i].se) {
      now = v[i].se;
      printf("%lld\n", v[i].se);
      cont++;
      if (cont == n)
        return 0;
    }
  }
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  // fclose(stream2);
#endif
  return 0;
}