codevs 2830 蓬莱山辉夜

二次联通门 : codevs 2830 蓬莱山辉夜

 

 

 

 

/*
    codevs 2830 蓬莱山辉夜

    堆模拟
    心血来潮就写了一下手写堆。。
    
    1A比较开心 
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define Max 100000

void read (int &now)
{
    register char word = getchar ();
    for (now = 0; word < '0' || word > '9'; word = getchar ());
    for (; word >= '0' && word <= '9'; now = now * 10 + word - '0', word = getchar ());
}

inline void swap (int &a, int &b)
{
    int now = a;
    a = b;
    b = now;
}

int Using[Max];

class Heap_Type
{
    
    private :
        
        int heap[Max];
        int name_heap[Max];
        int Count;
        
    public :
        
        void push (int name, int Time)
        {
            Count++;
            heap [Count] = Time;
            name_heap [Count] = name;
            int now = Count;
            for (; now > 1; )
            {
                int next = now >> 1;
                if (heap [now] < heap [next])
                {
                    swap (heap [now], heap [next]);
                    swap (name_heap [now], name_heap [next]);
                }
                now = next;        
            }
        }
        
        int time_top ()
        {
            return heap [1];
        }
        
        int name_top ()
        {
            return name_heap [1];
        }
        
        void pop ()
        {
            int now = 1;
            heap [now] = heap [Count];
            name_heap [now] = name_heap [Count];
            Count--;
            for (; (now << 1) < Count; )
            {
                int next = now << 1;
                if ((next | 1) <= Count && heap [next | 1] < heap [next])
                    next++;
                if (heap [now] > heap [next])
                {
                    swap (heap [now], heap [next]);
                    swap (name_heap [now], name_heap [next]);
                }
                else 
                    break;
                now = next;
            }
        }
        
        bool Empty ()
        {
            return this->Count == 0;
        }
};

Heap_Type Heap;

int Name_list[Max];
int Time_list[Max];

int TotalTotal = 0;

char line[Max];

int main (int argc, char *argv [])
{
    int Name, Time;
    
    for (; scanf ("%s", line) && line[0] != '#'; )
    {
        read (Name);
        read (Time);
        Using [Name] = Time;
        Heap.push (Name, Time);
    }
    
    int N;
    read (N);
    register int Cur;
    
    for (int i = 1; i <= N; i++)
    {
        Cur = 0;
        
        memset (Name_list, 0, sizeof (Name_list));
        
        register int name_now = Heap.name_top ();
        register int time_now = Heap.time_top ();
        
        Cur++;
        
        Name_list [Cur] = name_now;
        Time_list [Cur] = time_now;
        
        Heap.pop ();
        
        for (; Heap.time_top () == time_now && !Heap.Empty () ; Heap.pop ())
        {
            Cur++;
            Name_list [Cur] = Heap.name_top ();
            Time_list [Cur] = Heap.time_top ();
        }        
            
        for (int j = 1; j <= Cur; j++)
            Heap.push (Name_list [j], Time_list [j] + Using [Name_list [j]]);
            
        std :: sort (Name_list + 1, Name_list + Cur + 1);    
        
        for (int j = 1; j <= Cur; j++)
        {
            TotalTotal ++;

            printf ("%d\n", Name_list[j]);
            if (TotalTotal == N)
                return 0;
        }
    }
    return 0;
}

 

posted @ 2017-07-19 17:00  ZlycerQan  阅读(195)  评论(0编辑  收藏  举报