Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
2013-03-07 13:37
483 查看
一开始提交后得到了好几个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.)
意思是一段信息可以重复出现,并且如果两串信息的优先级相同,则输出先输入的字符串
思路:建立一个最小堆,维护字符串的优先级,其中维护操作如下:
如果两串字符串的优先级相同,则维护字符串的编号(显然按编号最小优先)
如果两串字符串的优先级不同,直接维护字符串的优先级(显然按优先级最小优先)
Code
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.)
意思是一段信息可以重复出现,并且如果两串信息的优先级相同,则输出先输入的字符串
思路:建立一个最小堆,维护字符串的优先级,其中维护操作如下:
如果两串字符串的优先级相同,则维护字符串的编号(显然按编号最小优先)
如果两串字符串的优先级不同,直接维护字符串的优先级(显然按优先级最小优先)
Code
#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 ; heap .w = INF; heap .num = 0; heap .argu = 0; strcpy(heap .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 .meg, &heap .argu, &heap .w); heap .num = cnt; int p = n; /* heap[p>>1].w>tmp.w */ struct MesQueue tmp = heap ; 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; }
相关文章推荐
- Hdoj 1509 -Windows Message Queue 优先队列最小堆实现
- hdoj 1509 Windows Message Queue 【STL 优先队列】
- HDOJ 1509 Windows Message Queue (简单优先队列 )
- hdoj 1509 Windows Message Queue【优先队列】
- hdoj 1509 Windows Message Queue [STL 优先队列]
- HDU 1509:Windows Message Queue【优先队列】
- HDU1509 Windows Message Queue(优先队列priority_queue及重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU 1509 Windows Message Queue 【优先队列】
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- hud 1509 Windows Message Queue 简单优先队列
- HDU - 1509 Windows Message Queue (优先队列 重载运算符)
- HDUoj 1509 Windows Message Queue ( 优先队列
- hdu 杭电1509 Windows Message Queue【优先队列】
- 优先队列——Windows Message Queue