#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define int long long
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
typedef long long ll;
const int E = 1e6+777;
const long long inf = 1e18+777;
const int N = 24;
const int MOD = 1e9+7;
int n;
bitset<N> used1, used2;
pair<int,int> edge[N];
int dist(int x1, int y1, int x2, int y2){
return (abs(x1 - x2) + abs(y1 - y2));
}
int get(vector<int> &a, int y){
vector<tuple<int,int,int>> v;
for(int k : a){
for(int x = 1; x <= n; ++x){
v.pb({dist(edge[k].F,edge[k].S,x,y),k,x});
}
}sort(all(v));
int res = 0;
for(auto [d, pos, pos2] : v){
if(!used1[pos] && !used2[pos2]){
res += d;
used1[pos] = true;
used2[pos2] = true;
}
}
used1.reset();
used2.reset();
return res;
}
signed main(){
cin >> n;
int n1 = n + n;
for(int i = 0; i < n1; ++i){
cin >> edge[i].F >> edge[i].S;
}
int res = INT_MAX;
for(int mask = 0; mask < (1 << n1); ++mask){
vector<int> f, s;
for(int a = 0; a < n1; ++a){
if((mask >> a) & 1){
f.pb(a);
}else{
s.pb(a);
}
}
if(sz(f) == sz(s)){
// cerr << "F: "; for(auto it : f) cerr << it << ' '; cerr << "get: " << get(f, 1) << '\n';
// cerr << "S: "; for(auto it : s) cerr << it << ' '; cerr << "get: " << get(s, 1) << '\n';
res = min(res, get(f, 1) + get(s, 2));
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |