• 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;
    }
  • 相关阅读:
    第一次冲刺(1)
    从一维数组扩展到二维数组求子数组的最大值
    电梯调度结对项目
    关于从一个整数数组中求得最大的子整数组和
    对于对英文文本文档进行分析,统计文本里面单词出现最多的10个单词
    【一些小常识】关于各操作系统拷贝文件后文件的创建修改时间变化
    windows系统搭建禅道系统(BUG管理工具)
    JAVA+SELENIUM+MAVEN+TESTNG框架(二)新建项目
    jmeter接口自动化部署jenkins教程
    提交测试流程和【开发提测申请模板】
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/7206695.html
Copyright © 2020-2023  润新知