切题记录(5月)

jzoj2307:

水题,只要每次从这n个珠子中选出个数最大的m个做成项链即可

有二分做法,待填

ps:最近奥考后降智严重,连二叉堆都打错

#include<bits/stdc++.h>
using namespace std;
priority_queue<int>q;
int ar[100010],ct,nm,hp[2000010],cw;
int top(){
    return hp[1];
}
int pop(){
    int ans=hp[1],o=1;
    hp[1]=0;
    while(1){
        if(o*2>nm)break;
        else if(o*2+1>nm){
            swap(hp[o],hp[o*2]);
            o=o*2;
        }
        else{
            if(hp[o*2]<hp[o*2+1]){
                swap(hp[o],hp[o*2+1]);
                o=o*2+1;
            }
            else{
                swap(hp[o],hp[o*2]);
                o=o*2;
            }
        }
    }
    cw--;
}
void push(int x){
    nm++;cw++;
    hp[nm]=x;
    int g=nm;
    while(g>1){
        int fa=g/2;
        if(hp[fa]<hp[g]){
            swap(hp[fa],hp[g]);
            g=fa;
        }
        else return;
    }
}
int main(){
    int n,m;
    while(scanf("%d",&n)!=EOF&&n){
        nm=0;
        int ans=0,r=1e9;
        for(int i=1;i<=n;i++){
            int a;
            scanf("%d",&a);
            push(a);
        }
        scanf("%d",&m);
        while(cw>=m){
            r=1;
            ct=0;
            for(int i=1;i<=m;i++){
                int a=top();
                pop();
                r=min(r,a);
                ar[++ct]=a;
            }
            ans+=r;
            for(int i=1;i<=ct;i++)
                if(ar[i]-r>0)push(ar[i]-r);
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自www.cnblogs.com/rilisoft/p/10806704.html