码迷,mamicode.com
首页 > 其他好文 > 详细

切题记录(5月)

时间:2019-05-03 22:46:29      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:水题   namespace   ++   i++   using   lse   names   name   pre   

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);
    }
}

 

切题记录(5月)

标签:水题   namespace   ++   i++   using   lse   names   name   pre   

原文地址:https://www.cnblogs.com/rilisoft/p/10806704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!