QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#164409 | #6116. Changing the Sequences | realIyxiang | WA | 16ms | 16292kb | C++14 | 2.9kb | 2023-09-04 22:50:08 | 2023-09-04 22:50:08 |
Judging History
answer
#include <bits/stdc++.h>
#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;
int read() {
int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}
const int N = 501000;
const int M = 50010;
namespace F { // max_flow_with_cost
const int inf = 0x3f3f3f3f;
int h[N], now[N], cnt = 1, S, T;
bool vis[N];
ll maxflow, cost, dis[N];
struct edge { int v, w, c, nxt; } e[M << 1];
inline void tlink(int x, int y, int w, int c) { e[++cnt] = (edge) { y, w, c, h[x] }; h[x] = cnt; }
inline void link(int x, int y, int w, int c) { tlink(x, y, w, c); tlink(y, x, 0, -c); }
inline void setst(int s, int t) { S = s; T = t; }
bool bfs() {
queue < int > q; q.push(S); rep(i, S, T) dis[i] = -1e9;
memset(vis, 0, sizeof vis); dis[S] = 0; now[S] = h[S]; vis[S] = 1;
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = h[x], y; i; i = e[i].nxt)
if(e[i].w && dis[y = e[i].v] < dis[x] + e[i].c) {
dis[y] = dis[x] + e[i].c; now[y] = h[y];
if(!vis[y]) vis[y] = 1, q.push(y);
}
vis[x] = 0;
} return dis[T] >= 0;
}
int dinic(int x,int flow,ll c) {
if(x == T) return maxflow += flow, cost += c * flow, flow;
int res = flow; vis[x] = 1;
for(int i = now[x], y; i && res; i = e[i].nxt) {
now[x] = i;
if(e[i].w && dis[y = e[i].v] == dis[x] + e[i].c && !vis[y]) {
int k = dinic(y, min(res,e[i].w), c + e[i].c);
e[i].w -= k; e[i ^ 1].w += k; res -= k;
}
} vis[x] = 0;
return flow - res;
}
void runit() { while(bfs()) dinic(S, inf, 0); }
int get(int x) { for(int i = h[x], y = e[i].v; i; i = e[i].nxt, y = e[i].v) if(!e[i].w) return y; }
}
int n, m;
int a[N], b[N], pos[N], pt[N];
int p[100][100];
int ans[N];
int main() {
#ifdef YJR_2333_TEST
freopen("1.in","r",stdin);
#endif
n = in, m = in; rep(i, 1, n) a[i] = in; rep(i, 1, n) b[i] = in;
rep(i, 1, n) if(!pos[a[i]]) pos[a[i]] = i;
rep(i, 1, m) if(!pos[i]) pos[i] = n + 1;
rep(i, 1, m) pt[i] = i;
sort(pt + 1, pt + m + 1, [&](int x, int y) { return pos[x] > pos[y]; });
rep(i, 1, m) pos[pt[i]] = i;
//rep(i, 1, m) cerr << pos[i] << " "; cerr << endl;
rep(i, 1, n) p[a[i]][b[i]]++;
F :: setst(0, m * 2 + 1);
rep(i, 1, m) F :: link(0, i, 1, 0), F :: link(i + m, m * 2 + 1, 1, 0);
//rep(i, 1, m) cerr << pos[i] << " "; cerr << endl;
rep(i, 1, m) rep(j, 1, m) F :: link(i, j + m, 1, p[i][j] * 10000 + pos[i] * (m - j + 1));
F :: runit();
rep(i, 1, m) ans[i] = F :: get(i) - m;
//cerr << F :: maxflow << " " << F :: cost << endl;
rep(i, 1, n) printf("%d ", ans[a[i]]); printf("\n");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 13980kb
input:
4 3 2 2 3 3 2 2 2 2
output:
1 1 2 2
result:
ok 4 number(s): "1 1 2 2"
Test #2:
score: 0
Accepted
time: 3ms
memory: 16036kb
input:
5 3 2 2 3 3 2 2 2 2 2 3
output:
3 3 2 2 3
result:
ok 5 number(s): "3 3 2 2 3"
Test #3:
score: 0
Accepted
time: 1ms
memory: 16028kb
input:
1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 12ms
memory: 16064kb
input:
1 60 60 60
output:
60
result:
ok 1 number(s): "60"
Test #5:
score: 0
Accepted
time: 12ms
memory: 16028kb
input:
1 60 1 60
output:
60
result:
ok 1 number(s): "60"
Test #6:
score: 0
Accepted
time: 12ms
memory: 14048kb
input:
1 60 60 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 16ms
memory: 16292kb
input:
1 60 1 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: -100
Wrong Answer
time: 11ms
memory: 13992kb
input:
100000 60 18 36 47 52 31 3 43 49 2 4 60 23 22 3 4 25 11 50 25 40 51 51 59 5 11 50 47 28 29 21 46 39 46 49 23 50 1 24 15 30 45 12 5 2 4 33 23 29 35 35 47 13 10 24 20 44 23 16 27 4 25 27 47 27 57 47 35 31 24 47 27 17 45 44 29 44 43 4 23 20 22 43 2 53 41 32 56 21 28 56 21 15 44 60 11 52 36 26 33 26 4 5...
output:
41 3 18 55 15 53 27 43 30 35 24 38 12 53 35 1 39 14 1 58 10 10 2 46 39 14 18 45 50 22 48 25 48 43 38 14 20 47 42 32 40 16 46 30 35 57 38 50 44 44 18 56 7 47 23 5 38 8 33 35 1 33 18 33 29 18 44 15 47 18 33 51 40 5 50 5 27 35 38 23 12 27 30 52 28 34 9 22 45 9 22 42 5 24 39 55 3 49 57 49 35 2 36 58 57 ...
result:
wrong answer 7th numbers differ - expected: '21', found: '27'