文章

454

粉丝

0

获赞

1

访问

10.5k

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

题意

同 5372,但大教室也能容纳 ≤20 人的课程,求最少总教室数与最少大教室数。

思路

  • 总教室数的下界是任意时刻同时进行的课程数峰值,区间图着色可以取到这个下界。

  • 大教室只需覆盖 >20 人的课程,所以大教室数 = 大课最大重叠数。

  • 离散化端点、逐段统计大课数与小课数,取 max(a+b) 与 max(a)。

复杂度

O(n^2)。

参考代码(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++;
            }
        if (big > maxa) maxa = big;
     ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发