Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <algorithm>
- #include <functional>
- #include <vector>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- #include <numeric>
- #include <queue>
- #include <cassert>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #define pb push_back
- #define mp make_pair
- #define sz(x) (int)(x).size()
- #define li long long
- #define ld long double
- #define x first
- #define y second
- #define pt pair<int, int>
- #define pll pair<li, li>
- #define forn(i, t) for(int i = 0; i < (t); i++)
- #define fore(i, f, t) for(int i = (f); i < (t); i++)
- #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- using namespace std;
- mt19937 myrand(time(NULL));
- const int INF = 1e9;
- const int MOD = 1000000007;
- const li INF64 = 2e18;
- const ld EPS = 1e-7;
- const int N = 200 + 7;
- int n;
- pt pos[N];
- bool read(){
- if(scanf("%d", &n) != 1)
- return 0;
- memset(pos, 255, sizeof(pos));
- int t;
- forn(i, n * 2){
- scanf("%d", &t);
- --t;
- if (pos[t].x == -1)
- pos[t].x = i;
- else
- pos[t].y = i;
- }
- return 1;
- }
- struct ed{
- int x, y, w, d, f;
- ed(int x, int y, int w, int d, int f) : x(x), y(y), w(w), d(d), f(f) {};
- ed(){};
- };
- bool operator <(const ed &a, const ed &b){
- if (a.x != b.x)
- return a.x < b.x;
- return a.y < b.y;
- }
- bool operator ==(const ed &a, const ed &b){
- return (a.x == b.x) && (a.y == b.y);
- }
- vector<ed> edges;
- int p[N];
- int blm(int v){
- vector<int> d(2 * n + 2, INF);
- d[v] = 0;
- while (1){
- bool fl = 0;
- for (auto it = edges.begin(); it != edges.end(); it++){
- auto e = *it;
- if (e.f < e.d && d[e.x] < INF && d[e.y] > d[e.x] + e.w){
- d[e.y] = d[e.x] + e.w;
- p[e.y] = e.x;
- fl = 1;
- }
- }
- if (!fl)
- break;
- }
- if (d[2 * n + 1] == INF)
- return 0;
- int cur = 2 * n + 1, dt = INF;
- while (cur != v){
- auto it = lower_bound(all(edges), ed(p[cur], cur, 0, 0, 0));
- dt = min(dt, it->d - it->f);
- cur = p[cur];
- }
- cur = 2 * n + 1;
- while (cur != v){
- int tmp = lower_bound(all(edges), ed(p[cur], cur, 0, 0, 0)) - edges.begin();
- int rtmp = lower_bound(all(edges), ed(cur, p[cur], 0, 0, 0)) - edges.begin();
- edges[tmp].f += dt;
- edges[rtmp].f -= dt;
- cur = p[cur];
- }
- return dt;
- }
- void solve(){
- edges = vector<ed>();
- forn(i, n){
- forn(j, n){
- edges.pb(ed(j + n, i, abs(pos[i].x - j) + abs(pos[i].y - j - n), 0, 0));
- edges.pb(ed(i, j + n, abs(pos[i].x - j) + abs(pos[i].y - j - n), 1, 0));
- }
- edges.pb(ed(2 * n + 1, i + n, 10000, 0, 0));
- edges.pb(ed(i + n, 2 * n + 1, 10000, 1, 0));
- edges.pb(ed(i, 2 * n, 10000, 0, 0));
- edges.pb(ed(2 * n, i, 10000, 1, 0));
- }
- sort(all(edges));
- int res = blm(2 * n);
- while (res)
- res = blm(2 * n);
- int ans[N];
- forn(i, n)
- forn(j, n)
- if (lower_bound(all(edges), ed(i, j + n, 0, 0, 0))->f != 0)
- ans[j] = i + 1;
- forn(j, 2)
- forn(i, n)
- printf("%d ", ans[i]);
- printf("\n");
- }
- int main(){
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #else
- freopen("teams.in", "r", stdin);
- freopen("teams.out", "w", stdout);
- #endif
- while(read())
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement