POJ 2051 Argus 堆排序 贴个模板

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值