堆的基本应用

233 讲个笑话 堆也是高级数奆结垢…
最近在备考 C S P CSP CSP 要复习图论 d p dp dp 突然发现自己好久没打的堆优化 d i j k s t r a dijkstra dijkstra 既不会 d i j k s t r a dijkstra dijkstra又忘了堆… 所以拿来训练场里的五道水题练练手…
各位大哥别骂我水博客 都是大哥…
P S : PS: PS:第五道题种树那题是带反悔的贪心 我感觉挺好玩也挺有用的,嫌弃前面太水看看最后一个玩玩吧(虽然最后一个也很水…

P1801 黑匣子_NOI导刊2010提高(06)

#include<bits/stdc++.h>
using namespace std;
#define N int(2e6+1)
inline void read(int &x){
    
    
    int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
    x=s*w;
}
priority_queue<int,vector<int>,greater<int> > qs;
priority_queue<int> qb;
int n,m,now,lst,a[N],req;
int main(){
    
    
    read(n),read(m);
    for(int i=1;i<=n;i++)read(a[i]);
    for(int i=1;i<=m;i++){
    
    
        read(req);
        for(int j=lst+1;j<=req;j++){
    
    
            qb.push(a[j]);
            if(qb.size()==i)qs.push(qb.top()),qb.pop();
        }
        lst=req;
        printf("%d\n",qs.top());
        qb.push(qs.top()),qs.pop();
    }
}


P2278 [HNOI2003]操作系统

#include<bits/stdc++.h>
using namespace std;
struct crs{
    
    
    int id,st,ti,pr;
    bool operator < (const crs &a) const{
    
    
        if(pr==a.pr)return a.st<st;
        else return a.pr>pr;
    }
}cpu;
priority_queue<crs> q;
int nowtim;
int main(){
    
    
    while(scanf("%d%d%d%d",&cpu.id,&cpu.st,&cpu.ti,&cpu.pr)!=EOF){
    
    
        while(!q.empty()&&nowtim+q.top().ti<=cpu.st){
    
    
            printf("%d %d\n",q.top().id,nowtim+q.top().ti);
            nowtim+=q.top().ti;q.pop();
        }
        if(!q.empty()){
    
    
            crs c=q.top();q.pop();
            c.ti-=(cpu.st-nowtim);
            q.push(c);
            
        }
        q.push(cpu);
        nowtim=cpu.st;
    }
    while(!q.empty()){
    
    
        printf("%d %d\n",q.top().id,q.top().ti+nowtim);
        nowtim+=q.top().ti;
        q.pop();
    }
}

P1631 序列合并

#include<bits/stdc++.h>
using namespace std;
#define N int(1e5+1)
#define reg register
inline void read(int &x){
    
    
    int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
    x=s*w;
}
priority_queue<int> q;
int n,a[N],b[N],ans[N];
int main(){
    
    
    read(n);
    for(reg int i=1;i<=n;i++)read(a[i]);
    for(reg int i=1;i<=n;i++)read(b[i]);
    for(reg int i=1;i<=n;i++){
    
    
        for(reg int j=1;j<=n;j++){
    
    
            if(q.size()<n)q.push(a[i]+b[j]);
            else{
    
    
                if(q.top()>a[i]+b[j])q.pop(),q.push(a[i]+b[j]);
                else break;
            }
        }
    }
    for(reg int i=n;i;i--)ans[i]=q.top(),q.pop();
    for(reg int i=1;i<=n;i++)printf("%d ",ans[i]);
    puts("");
}

P2085 最小函数值

#include <bits/stdc++.h>
int a[10001],b[10001],c[10001],n,m,s[10000001];
int main(){
    
    
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d %d %d",&a[i],&b[i],&c[i]);
    for(int i=1;i<=n;i++) for(int j=1;j<=100;j++) s[j+(i-1)*100]=a[i]*j*j+b[i]*j+c[i];
    std::sort(s+1,s+1+n*100);
    for(int i=1;i<=m;i++) printf("%d ",s[i]);
}

P1484 种树

#include<bits/stdc++.h>
using namespace std;
#define N int(5e5+100)
typedef long long ll;
inline void read(ll &x){
    
    
    ll s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
    x=s*w;
}
struct node{
    
    
    int id;
    ll w;
    bool operator <(const node &a)const {
    
    
        return w<a.w;
    }
}t;
bool vis[N];
ll ans,a[N],n,k,l[N],r[N];
priority_queue<node> q;
int main(){
    
    
    read(n);read(k);
    for(int i=1;i<=n;i++){
    
    
        read(t.w);
        t.id=i,a[i]=t.w;
        l[i]=i-1,r[i]=i+1;
        q.push(t);
    }
    r[0]=1,l[n+1]=n;
    for(int i=1;i<=k;i++){
    
    
        while(!q.empty()&&vis[q.top().id])q.pop();  
        t=q.top();q.pop();
        if(t.w<0)break;
        ans+=t.w;int x=t.id;
        a[x]=a[l[x]]+a[r[x]]-a[x];
        t.w=a[x];
        vis[l[x]]=vis[r[x]]=true;
        l[x]=l[l[x]],r[l[x]]=x;
        r[x]=r[r[x]];l[r[x]]=x;
        q.push(t);
    }
    printf("%lld\n",ans);
}



猜你喜欢

转载自blog.csdn.net/dhdhdhx/article/details/102573042
今日推荐