AcWing earthworm

AcWing earthworm

Description

  • National cricket recently the disaster earthworm!

    Flea flea the country next door also had no way to get earthworms, crickets king had no choice but to invite Shendao hand to help them destroy earthworms.

    Now there are n cricket kingdom only earthworms, the length of the earthworm is only i ai, length of all earthworms are non-negative integers, that there may be a length of earthworms zero.

    Every second, Shendao hand will find exactly that one of the longest in all of the earthworms, cut it into two pieces. If more than just the longest, the optional one. Shendao hand cut earthworms position is determined by the rational p.

    A worm x length will be cut into two lengths of x-⌊px⌋ ⌊px⌋ and earthworms.

    Particularly, if one of these two numbers is equal to 0, then the length of the earthworm 0 will be retained.

    Further, in addition to two new earthworms just produced, will increase the length of the remaining earthworms a non-negative integer q.

    The king cricket know this is not a permanent solution, not only because more and more earthworms, but also getting longer.

    King of cricket has decided to resort to a primitive force of a mysterious figure, but also need reinforcement m seconds to come.

    The king wanted to know cricket fighting that m seconds within.

    Specifically, he wants to know:

    1. Within m seconds, the earthworm is cut every second length before being cut off, a total number of m.
    2. After m seconds, the length of all the earthworms, a total number of n + m.

Input

  • The first row contains six integers n, m, q, u, v, t, wherein: n, m, q meanings described with reference to the subject; u, v, t are positive integers; you need to calculate their p = u / V (to ensure that 0 <u <v); t is an output parameter, the meaning of which will be explained in the output format.

    The second line comprises a non-negative integer n, is A . 1, A 2, ..., AN, i.e., only when the initial n earthworms length.

    Between two numbers in the same row of adjacent, just separated by a space.

Output

  • The first line of output ⌊m / t⌋ integers, chronologically sequentially outputs of t seconds, the second 2t, 3t of seconds, is cut earthworms ...... length (before being cut) is.

    The second line of the output ⌊ (n + m) / t⌋ integers, the m outputs of the second length of earthworms; required in descending order, sequentially outputs ranked t, the first 2t, of 3T, ...... length.

    Between two numbers in the same row of adjacent, just separated by a space.

    Even if a line does not need any number of output, you should also output a blank line.

    Please see the example to better understand this format.

Sample Input

3 7 1 1 3 1
3 3 2

Sample Output

3 4 4 4 5 5 6
6 6 6 5 5 4 4 3 2 2

Data Size

  • 1≤n≤105,
    0≤ai≤108,
    0<p<1,
    0≤q≤200,
    0≤m≤7∗106,
    0<u<v≤109,
    1≤t≤71

answer:

  • queue.

  • 80pts very easy to think of the idea. Subject of the request is to find the maximum value, insert a new value. This is clearly available heap to achieve. Then there is another subject increment q, for this requirement, set up and maintain an offset amount k enough. Then find the maximum value is: actual value = median team + k; the new value is inserted: Push (actual value - k - p).

  • So each operational complexity is O (logn) to see the data, m data needed to do linear algorithm. Then use the heap to die. Continue observed that there are hidden problems in these properties:

    1. Earthworms must be better than the first cut after cut the long earthworm. (Having a monotonic! )
    2. First cut of the earthworms out of the two divisions, respectively, than earthworms earthworm split after cutting out two long earthworm. (Having a monotonic! )
    • Above two conclusions prove yy can understand.
  • This question is not only all the global monotonic, but also the local monotonicity. This question itself has a monotonic.

  • Then it can open three queues. que1 (beginning read earthworms), que2 (cut out earthworm 1), que3 (cut out earthworm 2). Each first queue is removed from the team three longest earthworms, after cutting into que2, que3 (que1 only take hold).

  • Conclusion: To be good at discovering the title implied monotonic.

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 100005
#define M 7000005
using namespace std;

int n, m, q, k, e1, e2, t;
double p;
int a[N];
queue<int> que1, que2, que3;

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

int main()
{
    cin >> n >> m >> q;
    double t1 = read(), t2 = read();
    p = t1 / t2;
    t = read();
    e1 = m / t, e2 = (n + m) / t;
    if(!e1) printf("\n");
    
    for(int i = 1; i <= n; i++) a[i] = read();
    sort(a + 1, a + 1 + n, greater<int>());
    for(int i = 1; i <= n; i++) que1.push(a[i]);
    for(int i = 1; i <= m; i++)
    {
        int pos = -1, val = -1;
        if(que1.size() && que1.front() + k > val) val = que1.front() + k, pos = 1;
        if(que2.size() && que2.front() + k > val) val = que2.front() + k, pos = 2;
        if(que3.size() && que3.front() + k > val) val = que3.front() + k, pos = 3;
        int v1 = (int)(floor)(val * p), v2 = val - v1;
        que2.push(v1 - k - q), que3.push(v2 - k - q);
        if(pos == 1) que1.pop();
        else if(pos == 2) que2.pop();
        else if(pos == 3) que3.pop();
        if(i % t == 0) printf("%d ", val);
        k += q;
    }
    if(e1) cout << endl;
    
    if(!e2) {cout << endl; return 0;}
    int len = que1.size() + que2.size() + que3.size(), cnt = 0;
    for(int i = 1; i <= len; i++)
    {
        int pos = -1, val = -1;
        if(que1.size() && que1.front() + k > val) val = que1.front() + k, pos = 1;
        if(que2.size() && que2.front() + k > val) val = que2.front() + k, pos = 2;
        if(que3.size() && que3.front() + k > val) val = que3.front() + k, pos = 3;
        if(pos == 1) que1.pop();
        else if(pos == 2) que2.pop();
        else if(pos == 3) que3.pop();
        if(i % t == 0)
        {
            if(cnt < e2) printf("%d ", val), cnt++;
            else break;
        }
    }
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11318770.html