跳到正文

题解

题解 P2827 【蚯蚓】

YOUSIKI约 4 分钟
原文
文章信息与版本

文章信息

发布
2020年2月11日
收录
2026年8月15日
更新
2026年8月15日
篇幅
约 4 分钟
获赞
11
题目
P2827

版本

  1. v1当前

蚯蚓可以分为三类——

  1. 未被切割过的蚯蚓
  2. 上次切割中由⌊xp⌋\lfloor xp \rfloor方式产生
  3. 上次切割中由x−⌊xp⌋x - \lfloor xp \rfloor方式产生

对同为第二类或第三类的蚯蚓有如下的性质——

  • 设蚯蚓AA切割后产生了第二类蚯蚓A′A'。
  • 设蚯蚓BB切割后产生了第二类蚯蚓B′B'。
  • AA和BB都在世的时候,如果有AA比BB长,则A′A'和B′B'都在世时一定有A′A'比B′B'长。

证明:

我们不妨用AA表示蚯蚓AA在00时刻长度(它那时可能还未产生,但可以通过在世时tt时刻的长度减去tqtq得到一个无实际意义的长度)。对BB、A′A'、B′B'同理。注意这个00时刻长度可以用来比较蚯蚓之间的相对长短。

不妨设AA在tAt_A时刻被切割,BB在tBt_B时刻被切割,则有tA≤tBt_A\leq t_B。

A′=⌊(A+tAq)p⌋−tAqA'=\lfloor (A+t_Aq)p\rfloor - t_Aq B′=⌊(B+tBq)p⌋−tBqB'=\lfloor (B+t_Bq)p\rfloor - t_Bq

由0≤p≤10\leq p\leq 1易见A′≥B′A'\geq B',得证。

对于同为第三类的蚯蚓,也有类似的性质。

故所有第二类或第三类蚯蚓,其产生时间从早到晚的顺序就是长度从长到短的顺序,可以用队列在O(m)O(m)内维护。而第一类蚯蚓是有限的,只有初始那nn条,O(nlogn)O(nlogn)排序处理即可。

每次要找当前最长的蚯蚓,先分别取出这三类蚯蚓各自的队首(最长),然后比较出全局最长的蚯蚓即可。

总体时间复杂度O(nlog⁡n+m)O(n\log n+m)。

C++
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int n, m, q, u, v, t;
    cin >> n >> m >> q >> u >> v >> t;
    int siz[3] = { n + 1, m + 1, m + 1 };
    int *ptr_b[3], *ptr_e[3];
    for (int i = 0; i < 3; ++i)
        memset(ptr_b[i] = ptr_e[i] = new int[siz[i]], 0xcf, sizeof(int) * siz[i]);
    for (int i = 0; i < n; ++i)
        cin >> *(ptr_e[0]++);
    sort(ptr_b[0], ptr_e[0], greater<int>());
    int *answer = new int[n + m];
    for (int cur = 0; cur < m; ++cur) {
        int k = 0;
        for (int i = 1; i < 3; ++i)
            if (*ptr_b[k] < *ptr_b[i])
                k = i;
        answer[cur] = cur * q + *(ptr_b[k]++);
        int a = (long long)answer[cur] * u / v;
        int b = (long long)answer[cur] - a;
        *(ptr_e[1]++) = a - (cur + 1) * q;
        *(ptr_e[2]++) = b - (cur + 1) * q;
    }
    for (int i = t - 1; i < m; i += t)
        cout << answer[i] << " ";
    cout << endl;
    for (int cur = 0; cur < n + m; ++cur) {
        int k = 0;
        for (int i = 1; i < 3; ++i)
            if (*ptr_b[k] < *ptr_b[i])
                k = i;
        answer[cur] = m * q + *(ptr_b[k]++);
    }
    for (int i = t - 1; i < n + m; i += t)
        cout << answer[i] << " ";
    cout << endl;
    return 0;
}
原文
存档评论7 条抓取于