zoukankan      html  css  js  c++  java
  • 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
    ", Name_list[j]);
                if (TotalTotal == N)
                    return 0;
            }
        }
        return 0;
    }
  • 相关阅读:
    SDUT OJ 河床
    BZOJ 1500: [NOI2005]维修数列( splay )
    BZOJ 2049: [Sdoi2008]Cave 洞穴勘测( LCT )
    BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )
    BZOJ 1552: [Cerc2007]robotic sort( splay )
    BZOJ 1251: 序列终结者( splay )
    BZOJ 1576: [Usaco2009 Jan]安全路经Travel( 树链剖分 )
    BZOJ 3408: [Usaco2009 Oct]Heat Wave 热浪( 最短路 )
    BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )
    BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题( dp )
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/7206695.html
Copyright © 2011-2022 走看看