跳到正文

题解

题解:CF2062G Permutation Factory

Ado_qwq约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年6月26日
收录
2026年6月27日
更新
2026年11月14日
篇幅
约 12 分钟
获赞
1
题目
CF2062G

版本

  1. v1当前

Link\text{Link}

发现 min⁡(∣i−j∣,∣pi−pj∣)\min(|i-j|,|p_i-p_j|) 这个东西跟 ii 和 pip_i 都有关系,非常不好。考虑拆成两个代价分别为 ∣i−j∣|i-j| 和 ∣pi−pj∣|p_i-p_j| 的操作。

把 nn 个点刻画到二维平面上,第 ii 个点放到 (i,pi)(i,p_i) 这里。将前面的两个操作变为可以花 ∣i−j∣|i-j| 的代价交换两个横坐标,∣pi−pj∣|p_i-p_j| 的代价交换两个纵坐标,发现操作代价就是到交换后的点的曼哈顿距离。我们要求的是使得操作后的点集和所有 (i,qi)(i,q_i) 构成的点集相同的最小代价。

考虑先确定一个排列 posipos_i,代表原来的第 ii 个点要匹配 (posi,qposi)(pos_i,q_{pos_i}),这种方案的最小代价为 12∑i=1n∣i−posi∣+∣pi−pposi∣\frac1 2 \sum_{i=1}^n |i-pos_i|+|p_i-p_{pos_i}|。

构造性的证明对于任意的 pospos 我们都能取到最小代价。

先考虑将所有横坐标匹配,找到一个 posipos_i 最小的 xi≠posix_i \ne pos_i,此时一定有 posi<xipos_i < x_i。再找到一个 jj,使得 posi≤xj<xi≤posjpos_i \le x_j < x_i \le pos_j。(注意到 xx 和 pospos 都是排列,posipos_i 是最小的不合法的位置,此时只有 xj≤posjx_j \le pos_j 的点对全部在 posipos_i 和 xix_i 之间或者全部在 xix_i 之后我们才找不到合法的 jj。因为是排列,要让 posipos_i 最小肯定要有一个跨 xix_i 的 jj,所以一定能找到。)

纵坐标可以使用类似的方法,这里不再赘述。

最多交换 O(n2)O(n^2) 次,所以这里是 O(n3)O(n^3) 的。

现在思考如何找出代价最小的 pospos,连边 i→j′i \to j',权值为 ∣i−j∣+∣pi−qj∣|i-j|+|p_i-q_j|,做二分图带权最大匹配即可。KM 做到 O(n3)O(n^3) 或者直接费用流 O(n4)O(n^4) 都可以过。

Code
C++
#include<bits/stdc++.h>
#define pb emplace_back
namespace fast_IO{
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *P1 = ibuf, *P2 = ibuf, *P3 = obuf;
	#define getchar() ((P1==P2)and(P2=(P1=ibuf)+fread(ibuf,1,IOSIZE,stdin),P1==P2)?(EOF):(*P1++))
	#define putchar(x) ((P3==obuf+IOSIZE)&&(fwrite(obuf,P3-obuf,1,stdout),P3=obuf),*P3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, P3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
}using namespace fast_IO;
using namespace std;
const int N=255,inf=2e9;
int n,s,t,tot,p[N],q[N],id1[N],id2[N],dis[N],cur[N],pos[N];
vector<int>e[N],f[N],to[N],c[N];
inline void add(int u,int v,int flow,int cost){
	to[u].pb(e[v].size());
	to[v].pb(e[u].size());
	f[u].pb(flow);
	f[v].pb(0);
	c[u].pb(cost);
	c[v].pb(-cost);
	e[u].pb(v);
	e[v].pb(u);
}
inline bool spfa(){
	for(int i=1;i<=tot;++i)dis[i]=inf,cur[i]=0;
	dis[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty()){
	  int u=q.front();
	  q.pop();
	  for(int i=0;i<(int)e[u].size();++i){
		if(!f[u][i])continue;
		int v=e[u][i];
		if(dis[u]+c[u][i]<dis[v]){
		  dis[v]=dis[u]+c[u][i];
		  q.push(v);
		}
	  }
	}
	return dis[t]!=inf;
}
inline int dinic(int u,int flow){
	if(u==t)return flow;
	int used=0;
	for(int i=cur[u];i<(int)e[u].size();++i){
	  cur[u]=i+1;
	  int v=e[u][i];
	  if(dis[u]+c[u][i]==dis[v]&&f[u][i]){
		int delta=dinic(v,min(flow-used,f[u][i]));
		if(!delta){
		  dis[v]=-1e9;
		  continue;
		}
		f[u][i]-=delta;
		f[v][to[u][i]]+=delta;
		used+=delta;
		if(used==flow)break;
	  }
	}
	return used;
}
inline void test_main(){
	cin>>n;
	for(int i=1;i<=n;++i)cin>>p[i];
	for(int i=1;i<=n;++i)cin>>q[i];
	for(int i=1;i<=tot;++i)e[i].clear(),f[i].clear(),to[i].clear(),c[i].clear();
	s=1,t=tot=2;
	for(int i=1;i<=n;++i)id1[i]=++tot,id2[i]=++tot;
	for(int i=1;i<=n;++i){
	  add(s,id1[i],1,0);
	  add(id2[i],t,1,0);
	}
	for(int i=1;i<=n;++i)
	for(int j=1;j<=n;++j)
	  add(id1[i],id2[j],1,abs(i-j)+abs(p[i]-q[j]));
	while(spfa())dinic(s,inf);
	for(int i=1;i<=n;++i){
	  int u=id1[i];
	  for(int j=0;j<(int)e[u].size();++j){
		int v=e[u][j];
		if(f[u][j]||v==s)continue;
		for(int k=1;k<=n;++k){
		  if(id2[k]==v){
			pos[i]=k;
			break;
		  }
		}
	  }
	}
	vector<int>res1,res2;
	int mi;
	check:;
	mi=0;
	for(int i=1;i<=n;++i){
	  if(i!=pos[i]&&(pos[i]<pos[mi]||!mi))
		mi=i;
	}
	if(mi){
	  for(int j=1;j<=n;++j){
		if(mi!=j&&pos[mi]<=j&&j<mi&&mi<=pos[j]){
		  swap(pos[mi],pos[j]);
		  swap(p[mi],p[j]);
		  res1.pb(mi),res2.pb(j);
		  goto check;
		}
	  }
	}
	for(int i=1;i<=n;++i){
	  if(p[i]!=q[pos[i]]&&(q[pos[i]]<q[pos[mi]]||!mi))
		mi=i;
	}
	if(mi){
	  for(int j=1;j<=n;++j){
		if(mi!=j&&q[pos[mi]]<=p[j]&&p[j]<p[mi]&&p[mi]<=q[pos[j]]){
		  swap(p[mi],p[j]);
		  res1.pb(mi),res2.pb(j);
		  goto check;
		}
	  }
	}
	cout<<res1.size()<<"\n";
	for(int i=0;i<(int)res1.size();++i)cout<<res1[i]<<" "<<res2[i]<<"\n";
}
signed main(){
	int T=1;
	cin>>T;
	while(T--)test_main();
	return 0;
}
原文