BZOJ4721(NOIP2016)[蚯蚓]--队列

来源:互联网 发布:甘肃政法学院网络教学 编辑:程序博客网 时间:2024/05/16 06:49

【链接】
bzoj4721

【解题报告】

初看此题好像是堆但好像要被卡常。

所以经过思考以后可以发现:

(1)、其他蚯蚓加一段长度等价于给被切断的蚯蚓减一段长度。

(2)、此次被切断的两段短肯定分别比上次切断的两段短

所以只需要维护三个单调的队列就好了。

#include<cstdio>#include<cstring>#include<algorithm>#define LL long longusing namespace std;const int maxn=7000005,INF=((1<<30)-1)*2+1;int n,m,q,u,v,T,S,now,hed[3],til[3],que[3][maxn];double p;inline char nc(){    static char buf[100000],*l,*r;    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);    if (l==r) return EOF; return *l++;}inline int Read(){    int res=0; char ch=nc();    while (ch<'0'||ch>'9') ch=nc();    while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=nc();    return res;}inline bool Cmp(int a,int b) {return a>b;}void Write(int x) {if (!x) return; Write(x/10); putchar((x%10)+48);}int MAX(){    int x=0;    if (que[1][hed[1]]>que[x][hed[x]]) x=1;    if (que[2][hed[2]]>que[x][hed[x]]) x=2;    return x;}int main(){    freopen("4721.in","r",stdin);    freopen("4721.out","w",stdout);    n=Read(); m=Read(); q=Read(); u=Read(); v=Read(); T=Read();    for (int i=1; i<=n; i++) que[0][i]=Read();    sort(que[0]+1,que[0]+1+n,Cmp);    S=0; now=0; hed[0]=hed[1]=hed[2]=1; que[0][n+1]=-INF; til[1]=til[2]=0;    for (int i=1; i<=m; i++)    {        int x=MAX(),k=que[x][hed[x]++]+now,p1=(LL)k*u/v,p2=k-p1;        if (!(i%T)) {if (k==0) putchar(48); else Write(k); if ((i/T+1)*T<=m) putchar(32);}        que[1][++til[1]]=p1-now-q; que[2][++til[2]]=p2-now-q; now+=q;    }    putchar(10); que[1][til[1]+1]=que[2][til[2]+1]=-INF;    for (int i=1,x; i<=n+m; i++)    {        x=MAX(),p=que[x][hed[x]++]+now;        if (!(i%T)) {if (p==0) putchar(48); else Write(p); if ((i/T+1)*T<=n+m) putchar(32);}    }    return 0;}
原创粉丝点击