LOJ2362. 「NOIP2016」蚯蚓【单调队列】

时间:2021-03-25 15:35:57

LINK


思路

良心来说这题还挺思维的

我没看题解也不知道要这样维护

把每次斩断的点分别放进两个队列里面

因为要维护增长,所以可以让新进队的节点来一个负增长?

是不是就好了?

然后很容易发现因为在原始队列中小的数比大的数如果多增加了\(k \times q\)在剩下两个队列中大的分别比小的多增加大于等于\(k \times q\)

然后随便维护一下


//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1000010;
int n, m;
ll q, u, v, t, a[N];
queue<int> Q[4];
ll find_pos() {
int res = 0;
if (Q[1].size() && (!res || Q[1].front() > Q[res].front())) res = 1;
if (Q[2].size() && (!res || Q[2].front() > Q[res].front())) res = 2;
if (Q[3].size() && (!res || Q[3].front() > Q[res].front())) res = 3;
return res;
}
int main() {
Read(n), Read(m), Read(q), Read(u), Read(v), Read(t);
fu(i, 1, n) Read(a[i]);
sort(a + 1, a + n + 1);
fd(i, n, 1) Q[1].push(a[i]);
fu(i, 1, m) {
ll pos = find_pos(), len = Q[pos].front();
Q[pos].pop();
len += (i - 1) * q;
if (i % t == 0) {
Write(len);
putchar(' ');
}
ll l = len * u / v, r = len - l;
l -= i * q; r -= i * q;
Q[2].push(l);
Q[3].push(r);
}
putchar('\n');
fu(i, 1, n + m) {
ll pos = find_pos(), len = Q[pos].front();
Q[pos].pop();
len += m * q;
if (i % t == 0) {
Write(len);
putchar(' ');
}
}
return 0;
}