Luo Gu p2827 earthworm solution to a problem

topic

Algorithms tag algorithms what will not ah

What binary heap? ?

qbxt time out of learning to speak, to do for some time before, but now she wrote on the blog

Maintenance three queues, the queue 1 indicates the beginning of the earthworms, queue 2 represents the cut each time separated by relatively long earthworms that part, each time the queue 3 represents a cut worms that are separated by a shorter part.

We first ordered sequence of the original, because no matter how cut, first cut the earthworm into two parts after a certain cut into more than two parts of a large earthworm

Looking for every cut which is only worms in the queue 1, queue 2, queue 3 of the first team to find an increased maximum length of earthworm count, then put him out of the team, the two parts were cut into the team 2, team 3 .

Finally merge output

Code:

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10000010;
unsigned long long n, m, q, u, v, a[N], ans[N], s, y, tot, t;
queue<int>q1, q2, q3;
int cmp (long x, long y) {
    return x > y;
}
int maxn () {
    long long x1 = -(1 << 30), x2 = x1, x3 = x1;
    if (!q1.empty ()) x1 = q1.front ();
    if (!q2.empty ()) x2 = q2.front ();
    if (!q3.empty ()) x3 = q3.front ();
    if (x1 >= x2 && x1 >= x3) {q1.pop (); return x1;}
    if (x2 >= x1 && x2 >= x3) {q2.pop (); return x2;}
    q3.pop ();return x3;
}
void putin (long long x1, long long x2) {
    if (x1 < x2) swap (x1, x2);
    q2.push(x1);
    q3.push(x2);
    return ;
}
int main () {
    scanf ("%lld%lld%lld%lld%lld%lld", &n, &m, &q, &u, &v, &t);
    for (long long i = 1; i <= n; i++) scanf ("%lld", &a[i]);
    sort (a + 1, a + 1 + n, cmp);
    for (long long i = 1; i <= n; i++) q1.push(a[i]);
    for (long long i = 1; i <= m; i++) {
        ans[i] = maxn() + y;
        long long j = ans[i] * u / v, k = ans[i] - j;
        y += q;
        putin (j - y, k - y);
    }
    while (!q1.empty () || !q2.empty () || !q3.empty ()) a[++tot] = maxn() + y;
    for (long long i = t; i <= m; i += t) printf ("%lld ", ans[i]);
    printf ("\n");
    for (long long i = t; i <= tot; i += t) printf ("%lld ", a[i]);
    return 0;
}

Thank you for watching, I wish good health!

Guess you like

Origin www.cnblogs.com/yanxiujie/p/11668444.html