文章

454

粉丝

0

获赞

1

访问

10.5k

头像
教室分配问题 题解(C++ AC 代码)
P5372 复旦大学2025年机试题
发布于 2026年11月
阅读数 11

题意

n 门课各有 [s,e] 与人数 c,小教室只能放 ≤20 人的课,大教室只能放 >20 人的课,求最少总教室数与该前提下的最少大教室数。

思路

  • 大小教室互不通用,两类课各自看作区间图着色问题,最少教室数 = 最大重叠数。

  • 把所有端点离散化,逐段统计该时刻同时进行的大课数 a 与小课数 b。

  • 答案:总教室 = maxA + maxB,大教室 = maxA。

复杂度

O(n^2)(n ≤ 1000,逐段枚举课程即可)。

参考代码(C++)

C++:

#include <cstdio>
#include <algorithm>
using namespace std;

struct Course { int s, e, c; };
Course a[1005];
int xs[2100];

int main() {
    int n;
    if (scanf("%d", &n) != 1) return 0;
    int tot = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &a[i].s, &a[i].e, &a[i].c);
        xs[tot++] = a[i].s;
        xs[tot++] = a[i].e;
    }
    sort(xs, xs + tot);
    tot = unique(xs, xs + tot) - xs;
    int maxa = 0, maxb = 0, peak = 0;
    for (int i = 0; i + 1 < tot; i++) {
        int s = xs[i], e = xs[i + 1];
        int big = 0, small = 0;
        for (int j = 0; j < n; j++)
            if (a[j].s <= s && a[j].e >= e) {
                if (a[j].c > 20) big++; else small++;
            }
 ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发