QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#498968 | #6116. Changing the Sequences | cocoa_chan# | TL | 95ms | 14384kb | C++17 | 5.6kb | 2024-07-30 22:22:31 | 2024-07-30 22:22:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 3e5 + 9;
using T = long long;
const T inf = 1LL << 61;
struct MCMF {
struct edge {
int u, v;
T cap, cost;
int id;
edge(int _u, int _v, T _cap, T _cost, int _id) {
u = _u;
v = _v;
cap = _cap;
cost = _cost;
id = _id;
}
};
int n, s, t, mxid;
T flow, cost;
vector<vector<int>> g;
vector<edge> e;
vector<T> d, potential, flow_through;
vector<int> par;
bool neg;
void clear(ll N)
{
n=N+10;
s=0;
t=0;
mxid=0;
flow=0;
cost=0;
g.clear();
e.clear();
d.clear();
potential.clear();
flow_through.clear();
par.clear();
neg=false;
g.assign(n, vector<int> ());
}
MCMF() {}
MCMF(int _n) { // 0-based indexing
n = _n + 10;
g.assign(n, vector<int> ());
neg = false;
mxid = 0;
}
void add_edge(int u, int v, T cap, T cost, int id = -1, bool directed = true) {
if(cost < 0) neg = true;
g[u].push_back(e.size());
e.push_back(edge(u, v, cap, cost, id));
g[v].push_back(e.size());
e.push_back(edge(v, u, 0, -cost, -1));
mxid = max(mxid, id);
if(!directed) add_edge(v, u, cap, cost, -1, true);
}
bool dijkstra() {
par.assign(n, -1);
d.assign(n, inf);
priority_queue<pair<T, T>, vector<pair<T, T>>, greater<pair<T, T>> > q;
d[s] = 0;
q.push(pair<T, T>(0, s));
while (!q.empty()) {
int u = q.top().second;
T nw = q.top().first;
q.pop();
if(nw != d[u]) continue;
for (int i = 0; i < (int)g[u].size(); i++) {
int id = g[u][i];
int v = e[id].v;
T cap = e[id].cap;
T w = e[id].cost + potential[u] - potential[v];
if (d[u] + w < d[v] && cap > 0) {
d[v] = d[u] + w;
par[v] = id;
q.push(pair<T, T>(d[v], v));
}
}
}
for (int i = 0; i < n; i++) {
if (d[i] < inf) d[i] += (potential[i] - potential[s]);
}
for (int i = 0; i < n; i++) {
if (d[i] < inf) potential[i] = d[i];
}
return d[t] != inf; // for max flow min cost
// return d[t] <= 0; // for min cost flow
}
T send_flow(int v, T cur) {
if(par[v] == -1) return cur;
int id = par[v];
int u = e[id].u;
T w = e[id].cost;
T f = send_flow(u, min(cur, e[id].cap));
cost += f * w;
e[id].cap -= f;
e[id ^ 1].cap += f;
return f;
}
//returns {maxflow, mincost}
pair<T, T> solve(int _s, int _t, T goal = inf) {
s = _s;
t = _t;
flow = 0, cost = 0;
potential.assign(n, 0);
if (neg) {
// Run Bellman-Ford to find starting potential on the starting graph
// If the starting graph (before pushing flow in the residual graph) is a DAG,
// then this can be calculated in O(V + E) using DP:
// potential(v) = min({potential[u] + cost[u][v]}) for each u -> v and potential[s] = 0
d.assign(n, inf);
d[s] = 0;
bool relax = true;
for (int i = 0; i < n && relax; i++) {
relax = false;
for (int u = 0; u < n; u++) {
for (int k = 0; k < (int)g[u].size(); k++) {
int id = g[u][k];
int v = e[id].v;
T cap = e[id].cap, w = e[id].cost;
if (d[v] > d[u] + w && cap > 0) {
d[v] = d[u] + w;
relax = true;
}
}
}
}
for(int i = 0; i < n; i++) if(d[i] < inf) potential[i] = d[i];
}
while (flow < goal && dijkstra()) flow += send_flow(t, goal - flow);
flow_through.assign(mxid + 10, 0);
for (int u = 0; u < n; u++) {
for (auto v : g[u]) {
if (e[v].id >= 0) flow_through[e[v].id] = e[v ^ 1].cap;
}
}
return make_pair(flow, cost);
}
};
MCMF dinic(200);
ll n,m,i,j,k,l,r,x,y,z,w,s,t,a[1100000],b[1100000],c[1100000],d[1100000],perm[1100000],adj[1100][1100];
void construct_dinic(ll x,ll y)
{
ll i,j;
for(i=1;i<=m;i++)
{
dinic.add_edge(1,i+1,1,0);
dinic.add_edge(i+m+1,150,1,0);
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if((i==x&&j==y)||perm[i]==j)
dinic.add_edge(i+1,j+m+1,1,-adj[i][j]-10000000);
else
dinic.add_edge(i+1,j+m+1,1,-adj[i][j]);
}
}
}
int main()
{
scanf("%lld %lld",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
}
for(j=1;j<=m;j++)
{
dinic.add_edge(1,j+1,1,0);
dinic.add_edge(j+m+1,150,1,0);
for(i=1;i<=n;i++)
{
if(a[i]==j)
c[i]=1;
else
c[i]=0;
}
for(i=1;i<=m;i++)
{
d[i]=0;
}
for(i=1;i<=n;i++)
{
if(c[i]==1)
d[b[i]]++;
}
for(i=1;i<=m;i++)
adj[j][i]=d[i];
for(i=1;i<=m;i++)
dinic.add_edge(j+1,i+m+1,1,-d[i]);
}
s=-dinic.solve(1,150).second;
z=0;
for(i=1;i<=n;i++)
{
if(perm[a[i]]==0)
{
for(j=1;j<=m;j++)
{
dinic.clear(200);
construct_dinic(a[i],j);
t=(-dinic.solve(1,150).second)-10000000-10000000*z;
if(t==s)
{
z++;
perm[a[i]]=j;
break;
}
}
}
}
for(i=1;i<=n;i++)
printf("%lld ",perm[a[i]]);
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 12080kb
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: 0ms
memory: 12004kb
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: 12240kb
input:
1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 95ms
memory: 14232kb
input:
1 60 60 60
output:
60
result:
ok 1 number(s): "60"
Test #5:
score: 0
Accepted
time: 94ms
memory: 12632kb
input:
1 60 1 60
output:
60
result:
ok 1 number(s): "60"
Test #6:
score: 0
Accepted
time: 5ms
memory: 14384kb
input:
1 60 60 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 4ms
memory: 12372kb
input:
1 60 1 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: -100
Time Limit Exceeded
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...