[CF1276C]Beautiful Rectangle

89 篇文章 0 订阅

题目

传送门 to luogu

思路

部分枚举是不错的思路,考虑枚举其行数。不妨设行数 r ≤ r\le r 列数 c c c

若数字 x x x 出现了 v x v_x vx 次,则 x x x 可以在矩阵中填入 min ⁡ ( v x , r ) \min(v_x,r) min(vx,r) 次,原因是直接按照斜线填。形如:

∣ x y x x ∣ \begin{vmatrix} x& & &y\\ &x& &\\ & &x& \end{vmatrix} xxxy

下一步就接着往下填 y y y 。不难发现,任意连续的 t ( t ≤ r ) t(t\le r) t(tr) 个格子满足不在同一行、同一列上。只有一个例外,就是 n × n n\times n n×n 的矩阵,可能有

∣ y x x x ∣ \begin{vmatrix} y&x& \\ &x& \\ & &x \end{vmatrix} yxxx

连续的 3 3 3 x x x 却有两个在同一列。所以这种情况先填出现次数多的,以避免单独的 y y y 卡在上面的情况。

形式化地,我们用 f x ( t ) , f y ( t ) f_x(t),f_y(t) fx(t),fy(t) 表示第 t t t 个数所在的行列坐标。因为有取模,我们规定坐标范围是 [ 0 , n ) [0,n) [0,n) [ 0 , m ) [0,m) [0,m) ,则

f x ( t ) = t   m o d   n f_x(t)=t\bmod n fx(t)=tmodn

f y ( t ) = [ ⌊ t ⋅ gcd ⁡ ( n , m ) / n m ⌋ + t ]   m o d   m f_y(t)=\Big[\left\lfloor {t\cdot\gcd(n,m)}/{nm}\right\rfloor+t\Big]\bmod m fy(t)=[tgcd(n,m)/nm+t]modm

其中, t l c m ( n , m ) \frac{t}{{\tt lcm}(n,m)} lcm(n,m)t 是进行的修正。因为移动 l c m \tt lcm lcm 次之后就会重复。

既然如此,我们可以直接用 ∑ i = 1 n min ⁡ ( v i , r ) \sum_{i=1}^{n}\min(v_i,r) i=1nmin(vi,r) 计算列数,然后更新答案即可。

复杂度 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn)因为要排序离散化。

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
template < typename T >
void getMax(T&a,const T&b){if(a<b)a=b;}
template < typename T >
void getMin(T&a,const T&b){if(b<a)a=b;}

const int MaxN = 400005;
int a[MaxN], n;

int tmp[MaxN];
void input(){
	n = readint();
	for(int i=1; i<=n; ++i)
		a[i] = readint();
	for(int i=1; i<=n; ++i)
		tmp[i] = a[i];
	sort(tmp+1,tmp+n+1);
	int *r = unique(tmp+1,tmp+n+1);
	# define lb lower_bound
	for(int i=1; i<=n; ++i)
		a[i] = lb(tmp+1,r,a[i])-tmp;
	# undef lb
}

int cnt[MaxN]; // cnt[num]为num出现次数
int bucket[MaxN]; // [cnt]几个数出现cnt次
struct Node{
	int x, y, val;
	bool operator < (const Node &that) const {
		if(x == that.x)
			return y < that.y;
		return x < that.x;
	}
} node[MaxN];
Node want[MaxN];
void solve(){
	for(int i=1; i<=n; ++i)
		++ cnt[a[i]];
	int now = 0, delta = 0;
	for(int i=1; i<=n; ++i){
		++ bucket[cnt[i]];
		if(cnt[i]) ++ delta;
	}
	int ans = 0, row, col;
	for(int i=1; i*i<=n; ++i){
		now += delta, delta -= bucket[i];
		if(now/i < i) continue;
		if(ans < now/i*i)
			ans = now/i*i, row = i;
	}
	col = ans/row;
	printf("%d\n%d %d\n",ans,row,col);
	for(int i=1; i<=n; ++i){
		getMin(cnt[i],row);
		want[i].x = cnt[i];
		want[i].y = 0;
		want[i].val = i;
	}
	sort(want+1,want+n+1);
	for(int i=1; i<n+1-i; ++i)
		swap(want[i],want[n+1-i]);
	int T = ans/__gcd(row,col);
	for(int i=0,x=0,y=0,j=1; i<ans; ++i){
		if(i != 0 && i%T == 0) ++ y;
		x = x%row+1, y = y%col+1;
		while(cnt[want[j].val] == 0) ++ j;
		node[i].x = x, node[i].y = y;
		node[i].val = tmp[want[j].val];
		-- cnt[want[j].val];
	}
	sort(node,node+ans);
	for(int i=0; i<row; ++i){
		printf("%d",node[i*col].val);
		for(int j=1; j<col; ++j)
			printf(" %d",node[i*col+j].val);
		putchar('\n');
	}
}

int main(){
	input(), solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值