跳到正文

题解

题解:P11787 「FAOI-R4」蒲公英的约定

MPLN约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年2月23日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 7 分钟
获赞
6
题目
P11787

版本

  1. v1当前

细节多搞心态,赛时没调出来。

理解题目

题目里面的模拟过程有些复杂。

翻译:按分数从高到低依次选报高中,所有人都尽可能选最好的。若某个高中最开始有空,那么选报的时候,分数一样的都可以报该高中。

可以看看暴力代码。

解法:模拟

怎么优化暴力?无非就是每次询问能够 O(log⁡n)O(\operatorname{log}n) 修改。可是这很难搞啊,那么来探索一下性质。发现高中的名额只会减少( v≥0v\ge 0 ),那么所有人实际选报高中在自己志愿里的排名只会降低。一直降到上不了高中就去搬砖就结束。

所以无论 qq 到多少,总共的换学校次数最多就是 ∑li≤106\sum l_i\le 10^6 ,允许我们套个 log⁡\operatorname{log} 。

若 AA 学校名额减少了,怎么样的学生会被踢出呢?并不是所有 AA 高中后 bA−tAb_A-t_A 名的学生,录取的分数等于第 tAt_A 名的超出学生也会留下。

为了实现这一点,方便后续的操作,我们可以改变学校里面存储所有录取学生的方式,而新定义小组为报同一所高中且分数一样的同学们。而学校里面用一个 set 记录所有小组,方便取出分数最低的小组。

而给 xx 同学更换学校很简单,只要看他的下一志愿,直接往里面放(此处需要新建 / 更新小组)。然后再检查这个高中是否需要踢出低分小组即可。这样做的好处是即便 xx 可能本不能加入这个高中,也会再次踢出并检查下一个志愿,以此类推,注意边界。

时间复杂度 O(nlog⁡n)O(n\operatorname{log}n) 。

在此祝愿大家升入理想高中!

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, q, T;
unordered_set<int> ans[300010];
struct group {
    int o, sz;
    vector<int> p;
    bool operator<(const group &x) const { return o < x.o; }
};
struct sch {
    int t, b, lst;
    set<group> pq;
} c[300010];
struct stu {
    int id, l, o, lq;
    vector<int> a;
    bool operator<(const stu &x) const { return o < x.o; }
} s[300010];
void ins(int x, int to, int i) {
    s[x].lq = i, c[to].b++;
    auto it = c[to].pq.lower_bound({s[x].o, 0, {}});
    if (c[to].pq.empty() || (it->o) != s[x].o) {
        c[to].pq.insert({s[x].o, 1, {x}});
    } else {
        auto newt = *it;
        newt.sz++, newt.p.push_back(x);
        c[to].pq.erase(it);
        c[to].pq.insert(newt);
    }
}
void calFirst() {
    priority_queue<stu> pq;
    for (int i = 1; i <= n; i++) pq.push(s[i]);
    while (!pq.empty()) {
        int hscore = pq.top().o;
        while (!pq.empty() && pq.top().o == hscore) {
            int x = pq.top().id; pq.pop();
            for (int i = 0; i < s[x].l; i++) {
                int to = s[x].a[i];
                if (c[to].b < c[to].t || c[to].lst == hscore) {
                    ins(x, to, i);
                    if (c[to].b >= c[to].t) c[to].lst = hscore;
                    break;
                }
            }
        }
    }
    return;
}
void change(int x) {
    ans[T].insert(x);
    if(s[x].lq + 1 >= s[x].l) return;
    int nxt = s[x].lq + 1, to = s[x].a[nxt];
    ins(x, to, nxt);
    if (!c[to].pq.empty() && c[to].t <= c[to].b - (*c[to].pq.begin()).sz) {
        c[to].b -= (*c[to].pq.begin()).sz;
        vector<int> tmp = (*c[to].pq.begin()).p;
        c[to].pq.erase(c[to].pq.begin());
        for (int p : tmp) change(p);
    }
}
int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1, x; i <= m; i++) {
        scanf("%d", &x);
        c[i] = {x, 0, -1, {}};
    }
    for (int i = 1, x, z; i <= n; i++) {
        scanf("%d", &x);
        for (int j = 1, y; j <= x; j++) {
            scanf("%d", &y);
            s[i].a.push_back(y);
        }
        scanf("%d", &z);
        s[i] = {i, x, z, 0, s[i].a};
    }
    calFirst(), T = q;
    for (int x, v; T; T--) {
        scanf("%d%d", &x, &v);
        c[x].t -= v;
        while (!c[x].pq.empty() && c[x].t <= c[x].b - (*c[x].pq.begin()).sz) {
            c[x].b -= (*c[x].pq.begin()).sz;
            vector<int> tmp = (*c[x].pq.begin()).p;
            c[x].pq.erase(c[x].pq.begin());
            for (int p : tmp) change(p);
        }
        printf("%d\n", (int)ans[T].size());
    }
    return 0;
}
原文
存档评论2 条抓取于