题目链接

http://codeforces.com/contest/1276/problem/C

题解

嗯,比赛结束前3min想到做法然后rush不出来了……比赛结束后又写了15min才过……

以下是我的做法:

设最优解的行数和列数分别是\(R\)和\(C\), 不妨设\(R\le C\). 那么显然对于每个数我们只能选不超过\(R\)个。

考虑将所有数按出现次数从大到小排序,枚举\(R\), 求出可用的数的个数,设为\(cnt\), 那么显然\(C\le \frac{cnt}{R}\).

我们可以通过构造的方式证明,\(C\)可以取到\(\lfloor\frac{cnt}{R}\rfloor\).

下面为了方便默认\(cnt\)已经去掉了\(\mod R\)的余数。

把所有数按出现次数从大到小的顺序依次连成一个序列(一个数出现了多少次就复制多少个)。那么对于任意\(0\le x\lt R,0\le y\lt C\), 我们将序列的第\((xC+y)\)个数放到矩阵的第\(y\)行第\((x+y)\mod C\)列, 其中序列和矩阵的坐标均从\(0\)开始。这样我们可以证明一定满足条件。

举例解释一下: 假设有\(12\)个数排成\(3\times 4\)的矩阵,那么矩阵中每个数在原序列中的下标为:

0 3 6 9
10 1 4 7
8 11 2 5

正确性证明: 对于同一种数,显然它在序列中的位置满足要么所有的\(x\)都相同,要么是\(x\)的一个后缀和\((x+1)\)的一段前缀拼起来,且长度不超过\(R\).

如果是前者,正确性显然。如果是后者,唯一可能影响正确性的情况就是\(x\)后缀部分的某个数和\(y\)前缀部分的某个数放到了同一列上。

显然这种情况只有出现次数等于\(R\)时才会出现(因为每次是整体右移一列),而因为我们是按出现次数从大到小排序构造排列,所以出现次数为\(R\)的都被放到了序列最前面,此时因为前面已经放了的个数都是\(R\)的倍数,所以这一个一定从第\(0\)行开始放,属于第一种情况。因此第二种情况不会出现此问题,证毕。

故枚举每个\(R\), 求出对应的\(C\), 取最大值,再对取到最大值的\(R\)执行上述构造算法即可。

时间复杂度\(O(n\log n)\), 瓶颈在于离散化后统计每个数出现多少次。

代码

由于是很不冷静地rush出来的所以写得比较乱……

#include<bits/stdc++.h>
#define llong long long
#define pii pair<int,int>
#define mkpr make_pair
using namespace std; inline int read()
{
int w=1,s=0;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-')w=-1;ch=getchar();}
while(isdigit(ch)) {s=s*10+ch-'0';ch=getchar();}
return w*s;
} const int N = 1e6;
pii num[N+3];
int cans[N+3];
int fans[N+3];
vector<int> disc;
int a[N+3];
int n,wei; int gcd(int x,int y) {return y==0?x:gcd(y,x%y);} int main()
{
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]),disc.push_back(a[i]);
sort(disc.begin(),disc.end()); disc.erase(unique(disc.begin(),disc.end()),disc.end());
for(int i=1; i<=n; i++) a[i] = lower_bound(disc.begin(),disc.end(),a[i])-disc.begin()+1;
for(int i=1; i<=n; i++) num[i].second = i;
for(int i=1; i<=n; i++) num[a[i]].first++;
sort(num+1,num+n+1);
int l = 1,r = 0,cnt = 0,ans = 0,mxr = 1;
for(int i=1; i<=n; i++)
{
while(l<=n && num[l].first<i) {l++;}
cnt += n-l+1;
if(cnt/i<i) break;
int cur = cnt/i*i;
if(cur>ans) {ans = cur; mxr = i;}
}
int mxc = ans/mxr;
printf("%d\n%d %d\n",ans,mxr,mxc);
l = 1; while(l<=n && num[l].first<1) {l++;}
int x = 0,y = 0; wei = 0;
for(int i=n; i>=l; i--)
{
int cnt = min(num[i].first,mxr);
for(int j=1; j<=cnt&&wei<=ans; j++,wei++)
{
cans[x*mxc+(x+y)%mxc] = disc[num[i].second-1]; x++; if(x==mxr) {y++,x=0;}
}
}
for(int i=0; i<ans; i++)
{
printf("%d ",cans[i]);
if((i+1)%mxc==0) puts("");
}
return 0;
}

Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)的更多相关文章

  1. Codeforces contest 1277 E. Beautiful Rectangle

  2. Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

    链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...

  3. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  4. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  5. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  6. Codeforces 610C:Harmony Analysis(构造)

    [题目链接] http://codeforces.com/problemset/problem/610/C [题目大意] 构造出2^n个由1和-1组成的串使得其两两点积为0 [题解] 我们可以构造这样 ...

  7. Codeforces 804E The same permutation(构造)

    [题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出 ...

  8. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  9. Codeforces 989 P循环节01构造 ABCD连通块构造 思维对云遮月参考系坐标轴转换

    A 直接判存不存在连续的三个包含A,B,C就行 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a ...

随机推荐

  1. HTTP抓包

    1 概述 wireshark:全平台抓包工具,需要图形化界面,十分强大: httpry:http抓包插件,功能一般,操作简单: tcpdump:强大的抓包插件,支持多种网络协议. 2 httpry ( ...

  2. python练习:函数2

    习题: 定义一个方法get_num(num),num参数是列表类型,判断列表里面的元素为数字类型.其他类型则报错,并且返回一个偶数列表:(注:列表里面的元素为偶数). def get_num(num) ...

  3. mysql 2 修改表

    1 修改表名 rename table aaa to bbb; 或者 rename table aaa to bbb; 2 修改字段的数据类型 alter table person modify na ...

  4. 听课笔记--DP--最大子矩阵和

    最大子矩阵问题 给定一个n*n(0<n<=120)的矩阵, 矩阵内元素有正有负, 请找到此矩阵的内部元素和最大的子矩阵 样例输入: 4 0 -2 -7  0  9  2 -6  2  -4 ...

  5. oracle学习笔记:字符串替换 replace、regexp_replace、translate函数

    1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: ​ 将char中的字符串替换. ​ 当re ...

  6. Array+DP leetcode-11.装更多的水

    11. Container With Most Water 题面 Given n non-negative integers a1, a2, ..., an , where each represen ...

  7. 将用户赋予sudo权限:配置sudoers文件

    xxx is not in the sudoers file.This incident will be reported.的解决方法   1.切换到root用户下,怎么切换就不用说了吧,不会的自己百 ...

  8. Django应用之content type(app应用之一django.contrib.contenttypes)

    当一张表作为多个表的FK(主键),并且只能选择其中一个或者几个时,就可以使用content_type表:例如下图的数据关系,因此就可以使用content_type表来将表与表中的对象进行关联,从而做到 ...

  9. json —— pickle 的序列化和反序列化

    前言json的序列化和反序列化 1, json 只能序列化简单的数据类型,如,列表,字典,字符串,等简单的类型,不能序列化复杂的类型. 2, json 是支持所有的语言的,多以我们跨语言的时候都是用j ...

  10. Mha-Atlas-MySQL高可用方案实践

    一,mysql-mha环境准备 1.1 实验环境: 1.1 实验环境: 主机名 IP地址(NAT) 描述 mysql-master eth0:10.1.1.154 系统:CentOS6.5(6.x都可 ...