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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define INF (100000000)

typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> D;
  vector<P> DPAIR(M);
  for (int i = 0; i < M; ++i)
    DPAIR[i] = P(i, 0);
  for (int i = 0; i < N; ++i) {
    int temp;
    cin >> temp;
    D.push_back(temp - 1);
    ++DPAIR[temp - 1].second;
  }

  vector<int> DD[M];

  for (int i = 0; i < M; ++i) {
    DD[i].resize(N);
    DD[i][0] = (D[0] == i ? 1 : 0);
    for (int j = 1; j < N; ++j) {
      DD[i][j] = DD[i][j - 1] + (D[j] == i ? 1 : 0);
    }
  }

  string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string istr = ALPHA.substr(0, M);
  vector<int> indx(M + 1);
  map<string, P> mp;

  for (int c = 1; c <= M; ++c) {
    indx[c] = M;
    for (int i = 0; i < c; ++i)
      indx[i] = i;
    --indx[c - 1];
    while (indx[0] < M - c || c == M) {
      int now = c - 1;
      while (indx[0] != M - c || c == M) {
        if (indx[now + 1] - indx[now] > 1) {
          ++indx[now];
          break;
        } else if (indx[now] - indx[now - 1] > 1) {
          ++indx[now - 1];
          for (int i = now; i < c; ++i)
            indx[i] = indx[i - 1] + 1;
          break;
        } else {
          --now;
        }
      }

      // main
      int cnt = INF;
      int len;
      for (int i = 0; i < c; ++i) { // most back position's index
        string s = "";
        for (int j = 0; j < c; ++j) {
          if (j == i)
            continue;
          s.push_back(istr[indx[j]]);
        }
        if (s == "") {
          cnt = min(cnt, DPAIR[indx[i]].second -
                             DD[indx[i]][DPAIR[indx[i]].second - 1]);
          len = DPAIR[indx[i]].second;
        } else {
          int fst = mp[s].first;
          int snd = mp[s].second;
          cnt = min(cnt, fst + DPAIR[indx[i]].second -
                             DD[indx[i]][snd + DPAIR[indx[i]].second - 1] +
                             DD[indx[i]][snd - 1]);
          len = snd + DPAIR[indx[i]].second;
        }
      }
      string s = "";
      for (int i = 0; i < c; ++i) {
        s.push_back(istr[indx[i]]);
      }
      mp[s] = make_pair(cnt, len);

      if (c == M)
        break;
    }
  }

  cout << mp[istr].first << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

#define INF (100000000)

typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> D;
  vector<P> DPAIR(M);
  for (int i = 0; i < M; ++i)
    DPAIR[i] = P(i, 0);
  for (int i = 0; i < N; ++i) {
    int temp;
    cin >> temp;
    D.push_back(temp - 1);
    ++DPAIR[temp - 1].second;
  }

  vector<int> DD[M];

  for (int i = 0; i < M; ++i) {
    DD[i].resize(N);
    DD[i][0] = (D[0] == i ? 1 : 0);
    for (int j = 1; j < N; ++j) {
      DD[i][j] = DD[i][j - 1] + (D[j] == i ? 1 : 0);
    }
  }

  int ans = INF;
  do {
    int cnt = DPAIR[0].second - DD[DPAIR[0].first][DPAIR[0].second - 1];
    int left = DPAIR[0].second;
    for (int i = 1; i < M; ++i) {
      cnt += DPAIR[i].second - (DD[DPAIR[i].first][left + DPAIR[i].second - 1] -
                                DD[DPAIR[i].first][left - 1]);
      left += DPAIR[i].second;
    }
    ans = min(ans, cnt);
  } while (next_permutation(DPAIR.begin(), DPAIR.end()));

  cout << ans << endl;
  return 0;
}