POJ1456 Supermarket(二叉堆,小根堆)

#include

#include
#include
using namespace std;
struct node{int d,p;
}a[10010];
int heap[10010],n;
void up(int p){
while(p>1){
if(heap[p] swap(heap[p],heap[p/2]);
p/=2;
}
else break;
}
}
void insert(int val){
heap[++n]=val;
up(n);
}
void down(int p){
int s=p*2;
while(s<=n){
if(sheap[s+1]))s++;
if(heap[s] swap(heap[s],heap[p]);
p=s;s=2*p;
}
else break;
}
}
void cover(int val){
heap[1]=val;
down(1);
}
bool cmp(const node &x,const node &y){
return x.d}
int main(){
int N,ans;
while(cin>>N){
if(N==0){cout<<"0"< }
memset(a,0,sizeof(a));
memset(heap,0,sizeof(heap));
n=0;
for(int i=0;i cin>>a[i].p>>a[i].d;
sort(a,a+N,cmp);
insert(a[0].p);
for(int i=1;i if(a[i].d>n)insert(a[i].p);
else if(a[i].d==n&&a[i].p>heap[1])cover(a[i].p);
}
ans=0;
for(int i=1;i<=n;i++)ans+=heap[i];
printf("%d\n",ans);
}
return 0;
}

转载于:https://www.cnblogs.com/alugonum/p/8954236.html

你可能感兴趣的:(POJ1456 Supermarket(二叉堆,小根堆))