Problem p00457 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;

void debug(vector<P> &v) {
  cout << "[debug]" << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "(" << v[i].first << "," << v[i].second << ")" << endl;
  }
  cout << endl;
}

vector<P> remove(vector<P> &a) {
  bool flag = false;
  vector<P> a_;
  for (int i = 0; i < a.size(); i++) {
    if (a[i].second <= 0 || a[i].second >= 4) {
      flag = true;
    } else {
      if (!a_.empty() && a_[a_.size() - 1].first == a[i].first) {
        flag = true;
        a_[a_.size() - 1].second += a[i].second;
      } else {
        a_.push_back(a[i]);
      }
    }
  }
  if (flag)
    return remove(a_);
  return a_;
}

int sum(vector<P> &a) {
  int s = 0;
  for (int i = 0; i < a.size(); i++) {
    s += a[i].second;
  }
  return s;
}

int solve(vector<P> &a) {
  int ans = sum(a);
  for (int i = 0; i < a.size(); i++) {
    if (0 <= i - 1) {
      // 1つ前の色に変える.
      int c = a[i - 1].first;
      vector<P> a_ = a;
      a_[i].second--;
      a_[i - 1].second++;
      a_ = remove(a_);
      /*if( sum(a_) < ans ){
              cout << "change:" << i << " color:" << c << endl;
              debug(a_);
      }*/
      ans = min(ans, sum(a_));
    }
    if (i + 1 < a.size()) {
      // 1つ後ろの色に変える.
      int c = a[i + 1].first;
      vector<P> a_ = a;
      a_[i].second--;
      a_[i + 1].second++;
      a_ = remove(a_);
      /*if( sum(a_) < ans ){
              cout << "change:" << i << " color:" << c << endl;
              debug(a_);
      }*/
      ans = min(ans, sum(a_));
    }
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    vector<P> a;
    for (int i = 0; i < v.size(); i++) {
      if (i == 0) {
        a.push_back(P(v[i], 1));
      } else if (v[i - 1] == v[i]) {
        a[a.size() - 1].second++;
      } else {
        a.push_back(P(v[i], 1));
      }
    }
    cout << solve(a) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;

void debug(vector<P> &v) {
  cout << "[debug]" << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "(" << v[i].first << "," << v[i].second << ")" << endl;
  }
  cout << endl;
}

vector<P> remove(vector<P> a) {
  bool flag = false;
  vector<P> a_;
  for (int i = 0; i < a.size(); i++) {
    if (a[i].second <= 0 || a[i].second >= 4) {
      flag = true;
    } else {
      if (!a_.empty() && a_[a_.size() - 1].first == a[i].first) {
        flag = true;
        a_[a_.size() - 1].second += a[i].second;
      } else {
        a_.push_back(a[i]);
      }
    }
  }
  if (flag)
    return remove(a_);
  return a_;
}

int sum(vector<P> &a) {
  int s = 0;
  for (int i = 0; i < a.size(); i++) {
    s += a[i].second;
  }
  return s;
}

int solve(vector<P> &a) {
  int ans = sum(a);
  for (int i = 0; i < a.size(); i++) {
    if (0 <= i - 1) {
      // 1つ前の色に変える.
      int c = a[i - 1].first;
      vector<P> a_ = a;
      a_[i].second--;
      a_[i - 1].second++;
      a_ = remove(a_);
      /*if( sum(a_) < ans ){
              cout << "change:" << i << " color:" << c << endl;
              debug(a_);
      }*/
      ans = min(ans, sum(a_));
    }
    if (i + 1 < a.size()) {
      // 1つ後ろの色に変える.
      int c = a[i + 1].first;
      vector<P> a_ = a;
      a_[i].second--;
      a_[i + 1].second++;
      a_ = remove(a_);
      /*if( sum(a_) < ans ){
              cout << "change:" << i << " color:" << c << endl;
              debug(a_);
      }*/
      ans = min(ans, sum(a_));
    }
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    vector<P> a;
    for (int i = 0; i < v.size(); i++) {
      if (i == 0) {
        a.push_back(P(v[i], 1));
      } else if (v[i - 1] == v[i]) {
        a[a.size() - 1].second++;
      } else {
        a.push_back(P(v[i], 1));
      }
    }
    cout << solve(a) << endl;
  }
}