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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class p {
public:
  int num;
  int co;
};
int N, M;
vector<int> ans;
vector<int> co;
vector<int> line;
bool compare(p a, p b) { return a.co > b.co; }
pair<int, int> mkp(int a, int b) {
  if (a > b) {
    swap(a, b);
  }
  return pair<int, int>(a, b);
}
void add(map<pair<int, int>, int> &ma, pair<int, int> num) {
  map<pair<int, int>, int>::iterator itr;
  itr = ma.find(num);
  if (itr != ma.end()) {
    ++ma[num];
  } else {
    ma[num] = 1;
  }
}

bool find(map<pair<int, int>, int> &ma, pair<int, int> num) {
  map<pair<int, int>, int>::iterator itr;
  itr = ma.find(num);
  return itr != ma.end();
}
void func(int place) {
  map<pair<int, int>, int> ma;
  for (int i = place; i + 1 < line.size(); i += 2) {
    pair<int, int> pa = mkp(line[i], line[i + 1]);
    add(ma, pa);
  }
  for (int i = place; i + 1 < line.size(); i += 2) {
    if (line[i] == line[i + 1]) {
      ans[line[i]] = min(ans[line[i]], N - co[line[i]]);
    } else {
      pair<int, int> pa = mkp(line[i], line[i + 1]);
      int num = N - (co[line[i]] + co[line[i + 1]] - ma[pa]);
      ans[line[i]] = min(ans[line[i]], num);
      ans[line[i + 1]] = min(ans[line[i + 1]], num);
    }
  }
  vector<p> li(M);
  for (int i = 0; i < M; ++i) {
    li[i].num = i + 1;
    li[i].co = co[i + 1];
  }
  sort(li.begin(), li.end(), compare);
  for (int i = 0; i < li.size(); ++i) {
    int j;
    for (j = 0; j < li.size(); ++j) {
      if (i == j) {
        continue;
      }
      pair<int, int> pa;
      pa = mkp(li[i].num, li[j].num);
      if (!find(ma, pa)) {
        break;
      }
    }
    if (j == li.size()) {
      continue;
    }
    int num = N - (li[i].co + li[j].co);
    ans[li[i].num] = min(ans[li[i].num], num);
    ans[li[j].num] = min(ans[li[j].num], num);
  }
}
int main() {
  cin >> N >> M;
  line.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> line[i];
  }
  ans.resize(M + 1, INT32_MAX);
  co.resize(M + 1, 0);
  for (int i = 0; i < line.size(); ++i) {
    ++co[line[i]];
  }
  if (M == 1) {
    for (int i = 1; i < ans.size(); ++i) {
      ans[i] = 0;
    }
  } else {
    func(0);
    func(1);
  }
  for (int i = 1; i < ans.size(); ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int N, M;
int func(vector<int> &line, int use) {
  if (M == 1) {
    return 0;
  }
  int res = INT32_MAX;
  vector<int> count(M + 1, 0);
  ++count[line[0]];
  int place = 1;
  while (place < line.size()) {
    if (place + 1 == line.size()) {
      ++count[line[place]];
    } else {
      int x = line[place];
      int y = line[place + 1];
      int c = 0;
      c += x == use ? 1 : 0;
      c += y == use ? 1 : 0;
      if (c > 0) {
        count[use] += c;
      } else {
        ++count[x];
        ++count[y];
      }
    }
    place += 2;
  }
  for (int i = 1; i <= M; ++i) {
    if (i == use) {
      continue;
    }
    res = min(res, N - count[use] - count[i]);
  }
  count = vector<int>(M + 1, 0);
  place = 0;
  while (place < line.size()) {
    if (place + 1 == line.size()) {
      ++count[line[place]];
    } else {
      int x = line[place];
      int y = line[place + 1];
      int c = 0;
      c += x == use ? 1 : 0;
      c += y == use ? 1 : 0;
      if (c > 0) {
        count[use] += c;
      } else {
        ++count[x];
        ++count[y];
      }
    }
    place += 2;
  }
  for (int i = 1; i <= M; ++i) {
    if (i == use) {
      continue;
    }
    res = min(res, N - count[use] - count[i]);
  }
  return res;
}
int main() {
  cin >> N >> M;
  vector<int> line(N);
  for (int i = 0; i < N; ++i) {
    cin >> line[i];
  }
  for (int i = 1; i <= M; ++i) {
    cout << func(line, i) << endl;
  }
  return 0;
}