Class(ACM ICPC 2008–2009, NEERC, Northern Subregional Contest Problem C)

本文介绍了一种算法,用于解决学生如何安排座位以最大化所谓的“班级满度”的问题。班级满度定义为教室中行满度和列满度的较小值,其中行满度是指单行最多的学生数,列满度是指单列最多的学生数。文章提供了详细的解决方案及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input file: class.in

Output file: class.out

Time limit: 3 seconds

Memory limit: 256 megabytes

Description

Dr. Strange is a really strange lecturer. Each lecture he calculates class fullness and if it is small, hedecreases all semester grades by one. So the students want to maximize the class fullness.The class fullness is the minimum of row fullness and column fullness. The column fullness is themaximum number of students in a single column and the row fullness is the maximum number of studentsin a single row.

For example there are 16 students shown on the left picture (occupied desks are darkened). The rowfullness of this arrangement is 5 (the 4-th row) and the column fullness is 3 (the 1-st, the 3-rd, the 5-th orthe 6-th columns). So, the class fullness is 3. But if the students rearrange as shown on the right picturethen the column fullness will become 4 (the 5-th column), and so the class fullness will also become 4.


Input

The first line of the input file contains three integer numbers: n, r and c — number of students, rowsand columns in the class (1 ≤ r, c ≤ 100, 1 ≤ n ≤ r × c).

Output

The first line of the output file must contain a single integer number — the maximum possible classfullness.The following r lines must contain the optimal student arrangement. Each line must contain a descriptionof a single row. Row description is a line of c characters either ‘‘.’’ or ‘‘#’’, where ‘‘.’’ denotes an emptydesk, and ‘‘#’’ denotes an occupied one. If there are multiple optimal arrangements, output any one. 

Sample Input

16 4 6

Sample Output

4

.####.

#..###

#...##

###.##

题解:因为没有规定输出,所以我们可以先排0行0列,剩下一个1~n-1的矩阵,然后多的#在矩阵中随便放。

代码如下:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = 1e6+5;
ll n,m,k,a[maxn],b[maxn];
int main()
{
    freopen("class.in","r",stdin);
    freopen("class.out","w",stdout);
    char a[111][111];
    memset(a,'.',sizeof(a));
    int n,r,c;
    cin>>n>>c>>r;
    if((n+1)/2>max(r,c))//判断0行0列是否能排满
    {
        cout<<min(r,c)<<endl;
        n++;//(0,0)遍历了两遍
        for(int i=0; i<r; i++)
        {
            a[0][i]='#';
            n--;
        }
        for(int i=0; i<c; i++)
        {
            a[i][0]='#';
            n--;
        }
        if(n!=0)
        {
            for(int i=1; i<c; i++)
            {
                for(int j=1; j<r; j++)
                {
                    a[i][j]='#';
                    n--;
                    if(n==0)
                    {
                        i=c;
                        break;
                    }
                }
            }
        }
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                cout<<a[i][j];
            }
            cout<<endl;
        }
    }
    else
    {
        cout<<min(min(r,c),(n+1)/2)<<endl;//输出最小的那个,符合题目要求
        int t=n+1;
        for(int i=0; i<min(min(r,c),(n+1)/2); i++)
        {
            a[i][0]='#';
            t--;
            if(t==0)
                break;
        }
        for(int i=0; i<min(min(r,c),(n+1)/2); i++)
        {
            if(t==0)
                break;
            a[0][i]='#';
            t--;
        }
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                if(t==0)
                {
                    i=c;
                    break;
                }
                if(a[i][j]!='#')
                {
                    a[i][j]='#';
                    t--;
                }
            }
        }
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                cout<<a[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值