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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

// FILE *in = freopen("C", "r", stdin);

int data[] = {
    1,      4,      10,     20,     35,     56,     84,     120,    165,
    220,    286,    364,    455,    560,    680,    816,    969,    1140,
    1330,   1540,   1771,   2024,   2300,   2600,   2925,   3276,   3654,
    4060,   4495,   4960,   5456,   5984,   6545,   7140,   7770,   8436,
    9139,   9880,   10660,  11480,  12341,  13244,  14190,  15180,  16215,
    17296,  18424,  19600,  20825,  22100,  23426,  24804,  26235,  27720,
    29260,  30856,  32509,  34220,  35990,  37820,  39711,  41664,  43680,
    45760,  47905,  50116,  52394,  54740,  57155,  59640,  62196,  64824,
    67525,  70300,  73150,  76076,  79079,  82160,  85320,  88560,  91881,
    95284,  98770,  102340, 105995, 109736, 113564, 117480, 121485, 125580,
    129766, 134044, 138415, 142880, 147440, 152096, 156849, 161700, 166650,
    171700, 176851, 182104, 187460, 192920, 198485, 204156, 209934, 215820,
    221815, 227920, 234136, 240464, 246905, 253460, 260130, 266916, 273819,
    280840, 287980, 295240, 302621, 310124, 317750, 325500, 333375, 341376,
    349504, 357760, 366145, 374660, 383306, 392084, 400995, 410040, 419220,
    428536, 437989, 447580, 457310, 467180, 477191, 487344, 497640, 508080,
    518665, 529396, 540274, 551300, 562475, 573800, 585276, 596904, 608685,
    620620, 632710, 644956, 657359, 669920, 682640, 695520, 708561, 721764,
    735130, 748660, 762355, 776216, 790244, 804440, 818805, 833340, 848046,
    862924, 877975, 893200, 908600, 924176, 939929, 955860, 971970, 988260,
    1004731};
int data2[] = {1,      35,     165,    455,    969,    1771,   2925,   4495,
               6545,   9139,   12341,  16215,  20825,  26235,  32509,  39711,
               47905,  57155,  67525,  79079,  91881,  105995, 121485, 138415,
               156849, 176851, 198485, 221815, 246905, 273819, 302621, 333375,
               366145, 400995, 437989, 477191, 518665, 562475, 608685, 657359,
               708561, 762355, 818805, 877975, 939929, 1004731};

int n;
int d1[1000010];
int d2[1000010];

void process() {
  fill(d1, d1 + n + 1, 0x7fffffff);
  fill(d2, d2 + n + 1, 0x7fffffff);
  d1[0] = 0;
  d2[0] = 0;
  for (int i = 0; i < 181; i++) {
    for (int j = data[i]; j <= n; j++) {
      // if(d1[j - data[i]] != -1) {d1[j] == -1 ||
      if (d1[j] > d1[j - data[i]] + 1) {
        d1[j] = d1[j - data[i]] + 1;
      }
      //}
    }
  }
  for (int i = 0; i < 46; i++) {
    for (int j = data2[i]; j <= n; j++) {
      // if(d2[j - data2[i]] != -1) {d2[j] == -1 ||
      if (d2[j] > d2[j - data2[i]] + 1) {
        d2[j] = d2[j - data2[i]] + 1;
      }
      //}
    }
  }

  // printf("%d %d\\n", d1[n], d2[n]);
}

int main() {
  n = 1000000;
  process();
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d %d\\n", d1[n], d2[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

typedef long long int lli;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RREP(i, x) for (int i = (x); i >= 0; i--)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)

// FILE *in = freopen("C", "r", stdin);

int data[] = {
    1,      4,      10,     20,     35,     56,     84,     120,    165,
    220,    286,    364,    455,    560,    680,    816,    969,    1140,
    1330,   1540,   1771,   2024,   2300,   2600,   2925,   3276,   3654,
    4060,   4495,   4960,   5456,   5984,   6545,   7140,   7770,   8436,
    9139,   9880,   10660,  11480,  12341,  13244,  14190,  15180,  16215,
    17296,  18424,  19600,  20825,  22100,  23426,  24804,  26235,  27720,
    29260,  30856,  32509,  34220,  35990,  37820,  39711,  41664,  43680,
    45760,  47905,  50116,  52394,  54740,  57155,  59640,  62196,  64824,
    67525,  70300,  73150,  76076,  79079,  82160,  85320,  88560,  91881,
    95284,  98770,  102340, 105995, 109736, 113564, 117480, 121485, 125580,
    129766, 134044, 138415, 142880, 147440, 152096, 156849, 161700, 166650,
    171700, 176851, 182104, 187460, 192920, 198485, 204156, 209934, 215820,
    221815, 227920, 234136, 240464, 246905, 253460, 260130, 266916, 273819,
    280840, 287980, 295240, 302621, 310124, 317750, 325500, 333375, 341376,
    349504, 357760, 366145, 374660, 383306, 392084, 400995, 410040, 419220,
    428536, 437989, 447580, 457310, 467180, 477191, 487344, 497640, 508080,
    518665, 529396, 540274, 551300, 562475, 573800, 585276, 596904, 608685,
    620620, 632710, 644956, 657359, 669920, 682640, 695520, 708561, 721764,
    735130, 748660, 762355, 776216, 790244, 804440, 818805, 833340, 848046,
    862924, 877975, 893200, 908600, 924176, 939929, 955860, 971970, 988260,
    1004731};
int data2[] = {1,      35,     165,    455,    969,    1771,   2925,   4495,
               6545,   9139,   12341,  16215,  20825,  26235,  32509,  39711,
               47905,  57155,  67525,  79079,  91881,  105995, 121485, 138415,
               156849, 176851, 198485, 221815, 246905, 273819, 302621, 333375,
               366145, 400995, 437989, 477191, 518665, 562475, 608685, 657359,
               708561, 762355, 818805, 877975, 939929, 1004731};

int n;
int d1[1000010];
int d2[1000010];

void process() {
  fill(d1, d1 + n + 1, -1);
  fill(d2, d2 + n + 1, -1);
  d1[0] = 0;
  d2[0] = 0;
  for (int i = 0; i < 181; i++) {
    for (int j = data[i]; j <= n; j++) {
      if (d1[j - data[i]] != -1) {
        if (d1[j] == -1 || d1[j] > d1[j - data[i]] + 1) {
          d1[j] = d1[j - data[i]] + 1;
        }
      }
    }
  }
  for (int i = 0; i < 46; i++) {
    for (int j = data2[i]; j <= n; j++) {
      if (d2[j - data2[i]] != -1) {
        if (d2[j] == -1 || d2[j] > d2[j - data2[i]] + 1) {
          d2[j] = d2[j - data2[i]] + 1;
        }
      }
    }
  }

  printf("%d %d\\n", d1[n], d2[n]);
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    process();
  }
  return 0;
}