zoukankan      html  css  js  c++  java
  • Hdoj 1509 Windows Message Queue 优先队列最小堆实现

    一开始提交后得到了好几个WA,搞得我一直很郁闷,于是重新看了一下题目,发现有一个地方我没有考虑到

    Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.)

    意思是一段信息可以重复出现,并且如果两串信息的优先级相同,则输出先输入的字符串

    思路:建立一个最小堆,维护字符串的优先级,其中维护操作如下:

    1. 如果两串字符串的优先级相同,则维护字符串的编号(显然按编号最小优先)
    2. 如果两串字符串的优先级不同,直接维护字符串的优先级(显然按优先级最小优先)

    Code

    AC  46ms  228k

    #include <stdio.h>
    #include <string.h>
    
    const int maxn = 60004;
    const int INF  = 0x3F3F3F3F;
    
    struct MesQueue
    {
        int  argu, w, num;
        char meg[10];
    }heap[maxn];
    
    int  n=0, cnt=0;
    char cmd[10];
    
    bool cmp(struct MesQueue x, struct MesQueue y)
    {
        if (x.w == y.w)
            return (x.num>y.num ? 1:0);
        else
            return (x.w>y.w ? 1:0);
    }/* cmp */
    
    void shiftdown(int k, int n)    /* From top to bottom */
    {
        int son;
        struct MesQueue key = heap[k];  /* key=heap[k].w */
        
        for (; k<=n>>1; k=son)
        {
            son = k<<1;
            if (k!=n && cmp(heap[son], heap[son+1]))
                son = son + 1;
            
            if (cmp(key, heap[son]))
                heap[k] = heap[son]; 
            else
                break;
        }/* End of For */
        heap[k] = key;
    }/* shiftdown */
    
    void GetHeapTop()
    {
        printf("%s %d\n", heap[1].meg, heap[1].argu);
        
        heap[1] = heap[n];
        
        heap[n].w = INF;
        heap[n].num = 0;
        heap[n].argu = 0;
        strcpy(heap[n].meg, "\0");
    
        shiftdown(1, --n);
    }/* GetHeapTop */
    
    void InsertHeap()
    {
        ++n;    /* number of node in heap increase one */
        ++cnt;  /* Mark of node in heap */
        
        scanf("%s %d %d", &heap[n].meg, &heap[n].argu, &heap[n].w);
        heap[n].num = cnt;
        
        int p = n; /* heap[p>>1].w>tmp.w */
        struct MesQueue tmp = heap[n];
        
        while (p!=1 && cmp(heap[p>>1], tmp))    /* From bottom to top */
        {
            heap[p] = heap[p>>1];
            p >>= 1;
        }/* End of While */
        heap[p] = tmp;
    }/* InsertHeap */
    
    int main()
    {
        while (~scanf("%s", cmd))
        {
            if (cmd[0] == 'G')
            {   /* GET */
                if (n == 0)
                    printf("EMPTY QUEUE!\n");
                else
                    GetHeapTop();
            }/* End of IF */
            else
            {
                InsertHeap();
            }
        }/* End of While */
        
        return 0;
    }
    
  • 相关阅读:
    Excel文件上传,解析,下载(一 文件上传,使用MultipartFile来实现)
    从svn下载的Mavn项目,到本地后不识别(MyEcplise)
    Tomcat源代码阅读----服务器的启动(2)
    Tomcat源代码阅读----源代码部署(1)
    针对dhtmlX当中的treegrid在java类当中的封装实现的步骤(后台代码)
    Eclipse的maven插件最新地址
    oracle-创建JOB
    简单旋转特效的实现
    Spark SQL 小文件问题处理
    学好Spark/Kafka必须要掌握的Scala技术点(三)高阶函数、方法、柯里化、隐式转换
  • 原文地址:https://www.cnblogs.com/yewei/p/2484643.html
Copyright © 2011-2022 走看看