算法竞赛进阶指南---0x12(队列) 蚯蚓

题面在这里插入图片描述

题解

  1. m次操作,每次都要将一个最大的切成两段,然后再加上一个偏移量,然后将两段全部放入队列中,但是这样是O(mlogm),看题中数据范围肯定会超时,那么我就要继续优化
  1. 我们可以发现,先将原序列从大到小排列(q1,q2,q3,q4…),第一次肯定是切割q1,假设将q1切成了q1l,q1r,那么对于第二次切割只需要在q2,q1l,q1r中找到最大的即可,这个用三个队列来维护是O(1)
  1. 对于每次切割后,都要将没有切割的蚯蚓加上一个偏移量q,那么我们就可以将偏移量设为一个整体,在每次操作后,偏移量+=q,但是对于新分割的是不加q的,那么我们可以将这两段减去一个q,再放入队列,那么最后计算偏移量的时候就可以和其他一样

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<limits.h>

using namespace std;
const int N = 1e5 + 10, M = 7e6 + 10;

int n, m, q, u, v, t;
int q1[N], q2[M], q3[M];
int offset;
int hh1, hh2, hh3, tt1, tt2 = -1, tt3 = -1;

int get_max() {
    
    
    int maxn = INT_MIN;
    if (hh1 <= tt1) maxn = max(q1[hh1], maxn);
    if (hh2 <= tt2) maxn = max(q2[hh2], maxn);
    if (hh3 <= tt3) maxn = max(q3[hh3], maxn);
    if (hh1 <= tt1 && maxn == q1[hh1]) hh1++;
    else if (hh2 <= tt2 && maxn == q2[hh2]) hh2++;
    else hh3++;
    return maxn;
}

int main() {
    
    

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    cin >> n >> m >> q >> u >> v >> t;
    for (int i = 0; i < n; i++) cin >> q1[i];
    tt1 = n - 1;
    sort(q1, q1 + n);
    reverse(q1, q1 + n);

    for (int i = 1; i <= m; i++) {
    
    
        int x = get_max();
        x += offset;
        if (i % t == 0) cout << x << " ";
        int left = x * 1ll * u / v;
        int right = x - left;
        offset += q;
        left -= offset, right -= offset;
        q2[++tt2] = left, q3[++tt3] = right;
    }
    cout << endl;
    for (int i = 1; i <= m + n; i++) {
    
    
        int x = get_max();
        if (i % t == 0) cout << x + offset << " ";
    }
    cout << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/114301158