Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef long double LD;
- #define all(a) a.begin(),a.end()
- #define rall(a) a.rbegin(),a.rend()
- #define X first
- #define Y second
- #define pb push_back
- #define int long long
- const LL mod = 1e9 + 7;
- template<class T>
- istream& operator >> (istream& in, vector<T>& v){ for (auto &x : v) { in >> x; } return in; }
- template<class T>
- istream& operator >> (istream& in, pair<T, T> & v){ in >> v.X >> v.Y;return in; }
- template<class T>
- ostream& operator << (ostream& out, pair<T, T> & v){ out << v.X << " " << v.Y;return out; }
- void chkmax(int &a, int b) {
- a = max(a, b);
- return;
- }
- void chkmin(int &a, int b) {
- a = min(a, b);
- return;
- }
- LL ppow (LL x, LL s) {
- if (!s) return 1;
- if (!(s - 1)) return x % mod;
- if (s % 2) return (x * ppow (x, s - 1)) % mod;
- LL b = ppow (x, s / 2);
- return (b * b) % mod;
- }
- main(){
- //ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- int n;
- cin >> n;
- vector<int> a(n);
- cin >> a;
- vector<int> pos[25];
- for (int i = 0;i < n;i++) {
- pos[--a[i]].pb(i);
- }
- int cnt[21][21];
- for (int i = 0;i < 20;i++) {
- for (int j = 0;j < 20;j++) {
- cnt[i][j] = 0;
- if (i == j) {
- continue;
- }
- if (pos[i].size() == 0 || pos[j].size() == 0) {
- continue;
- }
- for (auto x : pos[i]) {
- int c = int32_t(upper_bound(all(pos[j]), x) - pos[j].begin());
- cnt[i][j] += c;
- }
- }
- }
- vector<int> dp((1 << 20), 1e18);
- dp[0] = 0;
- for (int mask = 0;mask < (1 << 20);mask++) {
- for (int i = 0;i < 20;i++) {
- int cur = 0;
- if (mask&(1 << i)) {
- continue;
- }
- for (int j = 0;j < 20;j++) {
- if (mask&(1 << j)) {
- cur += cnt[j][i];
- }
- }
- chkmin(dp[mask|(1 << i)], dp[mask] + cur);
- }
- }
- /*for (int i = 0;i < (1 << 3);i++) {
- cout << dp[i] << '\n';
- }*/
- cout << dp[(1 << 20) - 1];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement