Submission #4536104


Source Code Expand

#include <bits/stdc++.h>
      
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); i++)
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())

#define pb emplace_back
#define fi first
#define se second

using namespace std;

using ld = long double;
using ll = long long int;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> void UNIQUE(vector<T> &a){ a.erase(unique(a.begin(), a.end()), a.end()); }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

ll in(){long long int x; assert(scanf("%lld", &x) == 1); return x;}
ld fin(){double x; assert(scanf("%lf", &x) == 1); return x;}
char yuyushiki[1000010]; string stin(){assert(scanf("%s", yuyushiki) == 1); return yuyushiki;}

// head

int main(){

  ll n = in();
  ll m = in();
  vector<ll> a(m);
  REP(i, m){
    a[i] = in();
  }
  vector<vector<ll>> to(n, vector<ll>(n, 0));
  vector<ll> cur(n, 0);
  for(ll i = m-1; i >= 0; i--){
    REP(j, n){
      if(a[i] != j){
        to[j][a[i]] += cur[j];
      }
    }
    cur[a[i]]++;
  }

  vector<ll> dp(PW(n), INFLL);
  dp[0] = 0;
  
  REP(i, PW(n)){
    REP(j, n){
      if(!(i & PW(j))){
        ll nd = 0;
        ll ni = i | PW(j);
        REP(k, n){
          if(!(i & PW(k))){
            nd += to[j][k];
          }
        }
        chmin(dp[ni], dp[i] + nd);
      }
    }
  }

  cout << dp[PW(n)-1] << endl;
  
  return 0;
}

Submission Info

Submission Time
Task G - Teishoku
User joisino
Language C++14 (GCC 5.4.1)
Score 200
Code Size 2102 Byte
Status AC
Exec Time 34 ms
Memory 2048 KiB

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 38
Set Name Test Cases
All 0-sample, 0-sample0, 0-sample1, 1-random-small-0, 1-random-small-1, 1-random-small-2, 1-random-small-3, 1-random-small-4, 1-random-small-5, 1-random-small-6, 1-random-small-7, 1-random-small-8, 1-random-small-9, 2-random-large-0, 2-random-large-1, 2-random-large-2, 2-random-large-3, 2-random-large-4, 2-random-large-5, 2-random-large-6, 2-random-large-7, 2-random-large-8, 2-random-large-9, 3-random-large2-0, 3-random-large2-1, 3-random-large2-2, 3-random-large2-3, 3-random-large2-4, 3-random-large2-5, 3-random-large2-6, 3-random-large2-7, 3-random-large2-8, 3-random-large2-9, 3-special-1, 3-special-2, 3-special-3, 3-special-4, 9-hand-small0
Case Name Status Exec Time Memory
0-sample AC 1 ms 256 KiB
0-sample0 AC 1 ms 256 KiB
0-sample1 AC 1 ms 256 KiB
1-random-small-0 AC 1 ms 256 KiB
1-random-small-1 AC 1 ms 256 KiB
1-random-small-2 AC 1 ms 256 KiB
1-random-small-3 AC 1 ms 256 KiB
1-random-small-4 AC 1 ms 256 KiB
1-random-small-5 AC 1 ms 256 KiB
1-random-small-6 AC 1 ms 256 KiB
1-random-small-7 AC 1 ms 256 KiB
1-random-small-8 AC 1 ms 256 KiB
1-random-small-9 AC 1 ms 256 KiB
2-random-large-0 AC 8 ms 640 KiB
2-random-large-1 AC 21 ms 1664 KiB
2-random-large-2 AC 15 ms 1408 KiB
2-random-large-3 AC 2 ms 384 KiB
2-random-large-4 AC 7 ms 768 KiB
2-random-large-5 AC 15 ms 1280 KiB
2-random-large-6 AC 11 ms 1024 KiB
2-random-large-7 AC 13 ms 1280 KiB
2-random-large-8 AC 15 ms 1408 KiB
2-random-large-9 AC 6 ms 640 KiB
3-random-large2-0 AC 33 ms 2048 KiB
3-random-large2-1 AC 33 ms 2048 KiB
3-random-large2-2 AC 33 ms 2048 KiB
3-random-large2-3 AC 33 ms 2048 KiB
3-random-large2-4 AC 33 ms 2048 KiB
3-random-large2-5 AC 33 ms 2048 KiB
3-random-large2-6 AC 33 ms 2048 KiB
3-random-large2-7 AC 33 ms 2048 KiB
3-random-large2-8 AC 34 ms 2048 KiB
3-random-large2-9 AC 33 ms 2048 KiB
3-special-1 AC 19 ms 1792 KiB
3-special-2 AC 17 ms 1792 KiB
3-special-3 AC 33 ms 2048 KiB
3-special-4 AC 31 ms 2048 KiB
9-hand-small0 AC 11 ms 512 KiB