Earthworm, problem solution

Find a link to the topic .

Topic:

  The topic is very clear, so I won't say more.

analysis:

  Seeing this question, in fact, the idea came up at once. Let ’s start with the most violent: we find a maximum disassembly each time, and then add everything else, that is, direct simulation. First, this correctness is No problem, but time will not work, so if we want to find other ways, only the biggest, everyone should easily think of priority queues. The complexity of direct priority queue simulation is not as good as violence, but we can optimize:

  Think about this problem, here are some numbers a1, a2, a3, a4, a5. . . You want to increase each number by da (not a segment tree ...), how do we operate it, in fact, we do not need to operate, we find a cardinality, let the cardinality add da, and then add this cardinality when querying each element Well, if there is something that is not updated, then we can directly subtract da (of course, this conversion is because the non-subtracted is less than the subtraction), then we can simulate again, but, tle, tle, tle. . . In fact, nlogn's Changshu is a bit smaller, but it really can't pass here. Although the vjudge time limit is 10000ms, this does not let us pass. Of course, I also tried the handwriting heap, the same tle.

  In fact, it ’s easy to think of it here. In fact, many of our logs are a waste of complexity. Let ’s think about it, n is relatively small. After we sort n numbers, we will no longer sort them. Store n numbers separately, the length of one half and the length of the other half, why not sort it? Although each earthworm will grow longer after the operation, but we add it to the cardinality, so we can guarantee that the three queues have been increasing. So this problem is solved.

  Then, there is: if you use stl, the long long in the queue will be tle (how big is the stl constant ...) So open int, of course, q is only 200, and will not explode int. But the answer may not necessarily be int.

  Finally, the code.

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
queue<int> qu1;
queue<int> qu2;
queue<int> qu3;
int Top(){
    int qu11,qu22,qu33;
    if(qu1.empty())
        qu11=-1e9;
    else
        qu11=qu1.front();
    if(qu2.empty())
        qu22=-1e9;
    else
        qu22=qu2.front();
    if(qu3.empty())
        qu33=-1e9;
    else
        qu33=qu3.front();
    if(qu11>qu22&&qu11>qu33)
        return qu11;
    else if(qu22>qu33)
        return qu22;
    else
        return qu33;
}
void Pop(){
    int qu11,qu22,qu33;
    if(qu1.empty())
        qu11=-1e9;
    else
        qu11=qu1.front();
    if(qu2.empty())
        qu22=-1e9;
    else
        qu22=qu2.front();
    if(qu3.empty())
        qu33=-1e9;
    else
        qu33=qu3.front();
    if(qu11>qu22&&qu11>qu33)
        qu1.pop();
    else if(qu22>qu33)
        qu2.pop();
    else
        qu3.pop();
}
int main(){
    int n,m;
    int q,u,v,t;
//    freopen("P2827_2(1).in","r",stdin);
//    freopen("ouou.out","w",stdout);
    scanf("%d%d%d%d%d%d",&n,&m,&q,&u,&v,&t);
    long long Ha=0;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    for(int i=n;i>=1;i--)
        qu1.push(a[i]);
    for(int i=1;i<=m;i++){
        long long x=Top()+Ha;
        Pop();
        int x1=u*x/v-Ha-q;
        int x2=x-u*x/v-Ha-q;
        qu2.push(x1);
        qu3.push(x2);
        if(i%t==0)
            printf("%lld ",x);
        Ha + = q;
    }
    printf("\n");
    for(int i=1;i<=m+n;i++){
        if(i%t==0)
            printf("%lld ",Top()+Ha);
        Pop();
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wish-all-ac/p/12714974.html