[NOIP2016]蚯蚓 題解

題目描述

本題中,咱們將用符號[c]表示對c向下取整,例如:[3.0」= [3.1」= [3.9」=3。
蛐蛐國最近蚯蚓成災了!隔壁跳蚤國的跳蚤也拿蚯蚓們沒辦法,蛐蛐國王只好去請神刀手來幫他們消滅蚯蚓。
蛐蛐國裏如今共有n只蚯蚓(n爲正整數)。每隻蚯蚓擁有長度,咱們設第i只蚯蚓的長度爲a_i(i=1,2,…,n),並保證全部的長度都是非負整數(即:可能存在長度爲0的蚯蚓)。
每一秒,神刀手會在全部的蚯蚓中,準確地找到最長的那一隻(若有多個則任選一個)將其切成兩半。神刀手切開蚯蚓的位置由常數p(是知足0< p<1的有理數)決定,設這隻蚯蚓長度爲x,神刀手會將其切成兩隻長度分別爲[px]和x-[px]的蚯蚓。特殊地,若是這兩個數的其中一個等於0,則這個長度爲0的蚯蚓也會被保留。此外,除了剛剛產生的兩隻新蚯蚓,其他蚯蚓的長度都會增長q(是一個非負整常數)。
蛐蛐國王知道這樣不是長久之計,由於蚯蚓不只會愈來愈多,還會愈來愈長。蛐蛐國王決定求助於一位有着洪荒之力的神祕人物,可是救兵還須要m秒才能到來……
(m爲非負整數)
蛐蛐國王但願知道這m秒內的戰況。具體來講,他但願知道:
•m秒內,每一秒被切斷的蚯蚓被切斷前的長度(有m個數)
•m秒後,全部蚯蚓的長度(有n+m個數)。
蛐蛐國王固然知道怎麼作啦!可是他想考考你……node

輸入格式

第一行包含六個整數n,m,q,u,v,t,其中:n,m,q的意義見【題目描述】;u,v,t均爲正整數;你須要本身計算p=u/v(保證0< u< v)t是輸出參數,其含義將會在【輸出格式】中解釋。
第二行包含n個非負整數,爲ai,a2,…,an,即初始時n只蚯蚓的長度。
同一行中相鄰的兩個數之間,剛好用一個空格隔開。
保證1<=n<=10^5,0< m<7*10^6,0< u< v<10^9,0<=q<=200,1< t<71,0< ai<10^8。ios

輸出格式

第一行輸出[m/t]個整數,按時間順序,依次輸出第t秒,第2t秒,第3t秒……被切斷蚯蚓(在被切斷前)的長度。
第二行輸出[(n+m)/t]個整數,輸出m秒後蚯蚓的長度;須要按從大到小的順序,依次輸出排名第t,第2t,第3t……的長度。
同一行中相鄰的兩個數之間,剛好用一個空格隔開。即便某一行沒有任何數須要 輸出,你也應輸出一個空行。
請閱讀樣例來更好地理解這個格式。git

樣例輸入

3 7 1 1 3 1
3 3 2less

樣例輸出

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

spa

$Solution:$

暴力很好打吧,直接開一個堆維護大小關係,再用一個變量當加法標記就行了。blog

注意爲了防止炸精要直接用分數乘,記得開long long。隊列

得分:玄學get

$loj$評測結果:string

STL優先隊列:85ptsit

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e5+5;
typedef long long ll;
int n,m,g,u,v,t;
int inc,a[N];
priority_queue<int> q;
int main()
{
    n=read();m=read();g=read();u=read();v=read();t=read();
    for(int i=1;i<=n;i++)
        a[i]=read(),q.push(a[i]);
    for(int now=1;now<=m;now++)
    {
        int x=q.top()+inc;q.pop();
        if(now%t==0)printf("%d ",x);
        //cout<<now<<' '<<x<<endl;
        int len1=(1LL*x*u)/v,len2=x-len1;
        len1-=g+inc;len2-=g+inc;
        q.push(len1);q.push(len2);
        inc+=g;
    }
    putchar('\n');
    int sz=q.size();
    for(int i=1;i<=sz;i++)
    {
        int x=q.top()+inc;q.pop();
        if(i%t==0)printf("%d ",x);
    }
    putchar('\n');
    return 0;
}

 

平板電視配對堆:80pts

#include<cstdio>
#include<iostream>
#include<cstring>
#include<ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e5+5,M=8e6+5;
typedef long long ll;
int n,m,g,u,v,t;
int inc,a[N];
__gnu_pbds::priority_queue<int, less<int>, pairing_heap_tag> q;
int main()
{
    n=read();m=read();g=read();u=read();v=read();t=read();
    for(int i=1;i<=n;i++)
        a[i]=read(),q.push(a[i]);
    for(int now=1;now<=m;now++)
    {
        int x=q.top()+inc;q.pop();
        if(now%t==0)printf("%d ",x);
        int len1=(1LL*x*u)/v,len2=x-len1;
        len1-=g+inc;len2-=g+inc;
        q.push(len1);q.push(len2);
        inc+=g;
    }
    putchar('\n');
    int sz=q.size();
    for(int i=1;i<=sz;i++)
    {
        int x=q.top()+inc;q.pop();
        if(i%t==0)printf("%d ",x);
    }
    putchar('\n');
    return 0;
}

 

垃圾博主手寫的多是假的配對堆:65pts

#include<cstdio>
#include<iostream>
#include<cstring>
#define re register
//#include<queue>
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e5+5,M=8e6+5;
typedef long long ll;
int n,m,g,u,v,t;
int inc,a[N];
//priority_queue<int> q;
struct stack
{
    int s[M];
    int tp,cnt;
    inline int top()
    {
        if(!tp)return ++cnt;
        return s[tp--];
    }
    inline void push(int x){s[++tp]=x;}
}node,edge;
struct Pairheap
{
    int root,fa[M],val[M];
    int sz;
    int head[M],to[M],nxt[M],que[M];
    inline void add(int x,int y)
    {
        int tot=edge.top();
        to[tot]=y;
        nxt[tot]=head[x];
        head[x]=tot;
    }
    inline int merge(int x,int y)
    {
        if(val[x]<val[y])swap(x,y);
        add(x,y);fa[y]=x;
        return x;
    }
    inline void push(int Val)
    {
        ++sz;
        int x=node.top();
        val[x]=Val;
        root=root?merge(root,x):x;
    }
    inline int top()
    {
        return val[root];
    }
    inline void pop()
    {
        --sz;
        re int l=0,r=0;
        for(re int i=head[root];i;i=nxt[i])
        {
            int y=to[i];
            edge.push(i);
            if(fa[y]==root)fa[y]=0,que[++r]=y;
        }
        node.push(root);
        head[root]=fa[root]=0;
        val[root]=0;root=0;
        while(l<r)
        {
            ++l;if(l==r){root=que[l];return ;}
            int x=que[l],y=que[++l];
            que[++r]=merge(x,y);
        }
    }
    inline int size(){return sz;}
}q;

int main()
{
    n=read();m=read();g=read();u=read();v=read();t=read();
    for(int i=1;i<=n;i++)
        a[i]=read(),q.push(a[i]);
    for(int now=1;now<=m;now++)
    {
        int x=q.top()+inc;q.pop();
        if(now%t==0)printf("%d ",x);
        int len1=(1LL*x*u)/v,len2=x-len1;
        len1-=g+inc;len2-=g+inc;
        q.push(len1);q.push(len2);
        inc+=g;
    }
    putchar('\n');
    int sz=q.size();
    for(int i=1;i<=sz;i++)
    {
        int x=q.top()+inc;q.pop();
        if(i%t==0)printf("%d ",x);
    }
    putchar('\n');
    return 0;
}

 

 

基本和預期結果徹底相反QAQ

而後仔細觀察一下這個暴力,你會發現一個比較顯然的性質:堆內元素有單調性。

由於你用了加法標記,而每次取出後都會先拆成兩段再分別-q以後才放回去,因此元素應該是單調減的。

這種題的套路通常是開多個隊列而後比較隊首,那麼對於本題只須要開三個隊列,每次取出最大的隊首就行了。

第一個隊列存放初始值,每次獲得最大隊首後拆開,$\lfloor px \rfloor$放回第二個隊列,$x-  \lfloor  px  \rfloor$放回第三個便可。

爲了保證開始的單調性,別忘了sort一下。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e5+5,inf=0x3f3f3f3f;
typedef long long ll;
int n,m,g,u,v,t;
int inc,a[N];
queue<int> q1,q2,q3;
int main()
{
    n=read();m=read();g=read();u=read();v=read();t=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    sort(a+1,a+n+1,greater<int>());
    for(int i=1;i<=n;i++)
        q1.push(a[i]);
    for(int now=1;now<=m;now++)
    {
        int x=-inf;
        if(!q1.empty())x=max(x,q1.front());
        if(!q2.empty())x=max(q2.front(),x);
        if(!q3.empty())x=max(x,q3.front());
        if(!q1.empty()&&x==q1.front())q1.pop();
        else if(!q2.empty()&&x==q2.front())q2.pop();
        else if(!q3.empty()&&x==q3.front())q3.pop();
        x+=inc;
        if(now%t==0)printf("%d ",x);
        //cout<<now<<' '<<x<<endl;
        int len1=(1LL*x*u)/v,len2=x-len1;
        len1-=g+inc;len2-=g+inc;
        q2.push(len1);q3.push(len2);
        inc+=g;
    }
    putchar('\n');
    for(int i=1;i<=n+m;i++)
    {
        int x=-inf;
        if(!q1.empty())x=max(x,q1.front());
        if(!q2.empty())x=max(q2.front(),x);
        if(!q3.empty())x=max(x,q3.front());
        if(i%t==0)printf("%d ",x+inc);
        if(!q1.empty()&&x==q1.front())q1.pop();
        else if(!q2.empty()&&x==q2.front())q2.pop();
        else if(!q3.empty()&&x==q3.front())q3.pop();

    }
    putchar('\n');
    return 0;
}
相關文章
相關標籤/搜索