#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#pragma comment(linker, "/STACK:1024000000");
#define LL long long int
using namespace std;
const int MAXN = 1010;
struct N
{
int time,cn,id;
}heap[MAXN];
void Remove(N *heap,int s,int n)
{
int l = s<<1,r = s<<1|1;
N temp;
// cout<<s<<endl;
if(r <= n)
{
if(heap[l].time < heap[r].time || (heap[l].time == heap[r].time && heap[l].id < heap[r].id) )
{
if(heap[l].time < heap[s].time || (heap[l].time == heap[s].time && heap[l].id < heap[s].id))
{
temp = heap[s];
heap[s] = heap[l];
heap[l] = temp;
Remove(heap,l,n);
}
else
return ;
}
else if(heap[r].time < heap[l].time || (heap[r].time == heap[l].time && heap[r].id < heap[l].id) )
{
if(heap[r].time < heap[s].time || (heap[r].time == heap[s].time && heap[r].id < heap[s].id))
{
temp = heap[s];
heap[s] = heap[r];
heap[r] = temp;
Remove(heap,r,n);
}
else
return ;
}
}
else if(l <= n)
{
if(heap[l].time < heap[s].time || (heap[l].time == heap[s].time && heap[l].id < heap[s].id))
{
temp = heap[s];
heap[s] = heap[l];
heap[l] = temp;
Remove(heap,l,n);
}
else
return ;
}
}
void Init_Heap(N* heap,int n)
{
for(int i = n>>1;i >= 1; --i)
{
Remove(heap,i,n);
// cout<<"ENDL"<<endl;
}
}
void Insert(N *heap,N Heap_Top,int n)
{
heap[1] = Heap_Top;
int i = 1,mid = n>>1,l,r;
N temp;
while(i <= mid)
{
l = i<<1,r = i<<1|1;
if(r <= n)
{
if(heap[l].time < heap[r].time || (heap[l].time == heap[r].time && heap[l].id < heap[r].id) )
{
if(heap[l].time < heap[i].time || (heap[l].time == heap[i].time && heap[l].id < heap[i].id))
{
temp = heap[i];
heap[i] = heap[l];
heap[l] = temp;
i = l;
}
else
return ;
}
else if(heap[r].time < heap[l].time || (heap[r].time == heap[l].time && heap[r].id < heap[l].id) )
{
if(heap[r].time < heap[i].time || (heap[r].time == heap[i].time && heap[r].id < heap[i].id))
{
temp = heap[i];
heap[i] = heap[r];
heap[r] = temp;
i = r;
}
else
return ;
}
}
else if(l <= n)
{
if(heap[l].time < heap[i].time || (heap[l].time == heap[i].time && heap[l].id < heap[i].id))
{
temp = heap[i];
heap[i] = heap[l];
heap[l] = temp;
i = l;
}
else
return ;
}
else return ;
}
}
int main()
{
int i = 1,id,k,cn;
char s[20];
N Heap_Top;
while(scanf("%s",s) && strcmp(s,"#"))
{
scanf("%d %d%*c",&id,&cn);
heap[i].cn = cn;
heap[i].id = id;
heap[i].time = cn;
i++;
}
Init_Heap(heap,i-1);
scanf("%d",&k);
while(k--)
{
Heap_Top = heap[1];
printf("%d\n",Heap_Top.id);
Heap_Top.time += Heap_Top.cn;
Insert(heap,Heap_Top,i-1);
}
return 0;
}
POJ 2051 Argus 堆排序 贴个模板
最新推荐文章于 2017-03-13 20:39:44 发布