[洛谷P2827]蚯蚓:堆

分析:

话说这道题标签是二叉堆但堆只能拿85分是什么鬼?
哎呀不扯了其实就是发现:
定义一条蚯蚓被咔嚓,长度为len*p的这段为左段,另一段为右段。
同一侧段中,越早出现的蚯蚓越长(加上生长的长度),所以越早出现的越早被咔嚓。
于是可以用两个队列维护咔嚓得到的蚯蚓,而不是用一个堆。

代码:

堆(85pts):

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
typedef long long LL;

inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x;
}

int n,m,q,u,v,t;
double p;
std::priority_queue<int> que;

int main(){
    n=read(),m=read(),q=read();
    u=read(),v=read(),t=read();
    if(q==0){
        
    }
    p=u*1.0/v;
    for(int i=1;i<=n;i++){
        int len=read();
        que.push(len);
    }
    int counter=0,tag=0;
    for(int stp=1;stp<=m;stp++){
        int len=que.top()+tag;que.pop();
        counter++;
        if(counter==t){
            counter=0;
            printf("%d ",len);
        }
        tag+=q;
        int len1=len*p;
        que.push(len1-tag);
        que.push(len-len1-tag);
    }
    printf("\n");
    counter=0;
    while(!que.empty()){
        counter++;
        if(counter==t){
            counter=0;
            printf("%d ",que.top()+tag);
        }
        que.pop();
    }
    printf("\n");
    return 0;
}

三队列(一开始的蚯蚓降序排序后也算一个队列)(100pts):

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
typedef long long LL;

inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x;
}

int n,m,q,u,v,t,a[100005];
int q1[16000000],q2[16000000],h1,h2,t1,t2;

inline bool cmp(int x,int y){
    return x>y;
}

int main(){
    n=read(),m=read(),q=read();
    u=read(),v=read(),t=read();
    for(int i=1;i<=n;i++) a[i]=read();
    std::sort(a+1,a+n+1,cmp);
    h1=h2=1;
    int counter=0,tag=0,now=1;
    for(int stp=1;stp<=m;stp++){
        counter++;
        int len=-1;
        if(now<=n&&(a[now]>=q1[h1]||h1>t1)&&(a[now]>=q2[h2]||h2>t2)) len=a[now]+tag,now++;
        else if(h1<=t1&&(q1[h1]>a[now]||now>n)&&(q1[h1]>=q2[h2]||h2>t2)) len=q1[h1]+tag,h1++;
        else if(h2<=t2&&(q2[h2]>a[now]||now>n)&&(q2[h2]>q1[h1]||h1>t1)) len=q2[h2]+tag,h2++;
        if(counter==t){
            counter=0;
            printf("%d ",len);
        }
        tag+=q;
        q1[++t1]=1ll*len*u/v-tag;
        q2[++t2]=(len-q1[t1]-tag)-tag;
    }
    printf("\n");
    counter=0;
    while(1){
        counter++;
        int len=-1;
        if(now<=n&&(a[now]>=q1[h1]||h1>t1)&&(a[now]>=q2[h2]||h2>t2)) len=a[now]+tag,now++;
        else if(h1<=t1&&(q1[h1]>a[now]||now>n)&&(q1[h1]>=q2[h2]||h2>t2)) len=q1[h1]+tag,h1++;
        else if(h2<=t2&&(q2[h2]>a[now]||now>n)&&(q2[h2]>q1[h1]||h1>t1)) len=q2[h2]+tag,h2++;
        if(len==-1){
            printf("\n");
            return 0;
        }
        if(counter==t){
            counter=0;
            printf("%d ",len);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/ErkkiErkko/p/9772465.html