跳到正文

题解

题解:AT_arc176_a [ARC176A] 01 Matrix Again

鲤鱼江约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月29日
收录
2026年8月8日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v1当前

仔细思考可以得到一个假做法:每次取出离目标还差得最多的行和列,如果可以匹配就匹配,否则暴力找到下一个。

是小常数 O(nm2log⁡n)O(nm^2\log n) 但是这是假算,这个时候要用到只给了 mm 个点的限制了,这启发我们将图分组,使得从中选出任意 mm 组都能使行列和恰为 mm,那么给 mm 个点就相当于强行钦定了一些组必选。

至此思路清晰,按 (i+j)(i+j)%n 分成 mm 组,显然每一组都能使每行每列的值恰好加一,用 6×66\times 6 举例:

代码
0 1 2 3 4 5
1 2 3 4 5 0
2 3 4 5 0 1
3 4 5 0 1 2
4 5 0 1 2 3
5 0 1 2 3 4

数字相同代表一组。

C++
#include<bits/stdc++.h>
#define mk make_pair
#define sec second
#define endl '\n'
#define fi first

using namespace std;
typedef pair<int,int> pii;

const int N=2e5+10;
int n,m,flag[N],p[N],len;

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1,x,y;i<=m;++i){
		cin>>x>>y;--x;--y;int c=(x+y)%n;
		if(!flag[c]){flag[c]=1;p[++len]=c;}
	}
	for(int i=0;i<n&&len<m;++i) if(!flag[i]) p[++len]=i;
	cout<<n*m<<endl;
	for(int i=1;i<=m;++i){
		for(int j=0;j<n;++j){
			cout<<j+1<<' '<<(p[i]+n-j)%n+1<<endl;
		}
	}
	return 0;
}
原文