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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, m;
int p[100000 + 5];
int s[100000 + 5][25];
int cnt[25];
int dp[1 << 21];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, n) cin >> p[i];
  rep(i, n) cnt[p[i] - 1] += 1;
  rep(i, n) s[i + 1][p[i] - 1] += 1;
  rep(i, n) rep(j, m) s[i + 1][j] += s[i][j];
  fill(dp, dp + (1 << m) + 1, 1e9);
  dp[0] = 0;
  rep(i, 1 << m) {
    rep(j, m) {
      if (i >> j & 1) {
        int l = 0;
        int r = 0;
        rep(k, m) {
          if (k == j)
            r = cnt[k];
          else if (i >> k & 1)
            l += cnt[k];
        }
        int change = r - (s[r + l][j] - s[l][j]);
        dp[i] = min(dp[i], dp[i ^ (1 << j)] + change);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, m;
int p[100000 + 5];
int cnt[25];
int dp[1 << 25];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, n) cin >> p[i];
  rep(i, n) cnt[p[i] - 1] += 1;
  fill(dp, dp + (1 << m) + 1, 1e9);
  dp[0] = 0;
  rep(i, 1 << m) {
    rep(j, m) {
      if (i >> j & 1) {
        int l = 0;
        int r = 0;
        rep(k, m) {
          if (k == j)
            r = cnt[k];
          else if (i >> k & 1)
            l += cnt[k];
        }
        int change = 0;
        rep2(k, l, l + r) {
          if (p[k] - 1 != j)
            change += 1;
        }
        dp[i] = min(dp[i], dp[i ^ (1 << j)] + change);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
}