HDU 1387 Team Queue(优先队列)
2015-08-21 13:56
661 查看
Team Queue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1524 Accepted Submission(s): 515
Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a
team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them.
If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves.
Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
ENQUEUE x - enter element x into the team queue
DEQUEUE - process the first element and remove it from the queue
STOP - end of test case
The input will be terminated by a value of 0 for t.
Output
For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even
after the last one.
Sample Input
2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202 ENQUEUE 103 ENQUEUE 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 ENQUEUE 259001 ENQUEUE 260001 ENQUEUE 259002 ENQUEUE 259003 ENQUEUE 259004 ENQUEUE 259005 DEQUEUE DEQUEUE ENQUEUE 260002 ENQUEUE 260003 DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 0
Sample Output
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001
Source
University of Ulm Local Contest 1998
#include<stdio.h> #include<queue> #include<string.h> using namespace std; const int N = 1000005 ; struct NODE { int pri1 , pri2 , id ; friend bool operator<(NODE aa , NODE bb){ if(aa.pri1 == bb.pri1) return aa.pri2>bb.pri2 ; return aa.pri1 > bb.pri1 ; } }; int pri[1005] , num[1005] ; int team ; void work() { priority_queue<NODE> q; NODE now ; int id , k =0 ; char str[100]; memset(num , 0 ,sizeof(num)) ; memset(pri , -1 , sizeof(pri)) ; while(scanf("%s",str) > 0){ if(str[0] == 'S') break; if(str[0] == 'E') { scanf("%d",&id); now.id = id ; now.pri2 = k ; if(team[id] == -1 ) now.pri1 = k ; else if(pri[ team[id] ] == -1 ){ now.pri1 = k ; pri[ team[id] ] = k ; num[ team[id] ]++; } else now.pri1 = pri[ team[id] ] , num[ team[id] ]++;; q.push( now ) ; k++; } else{ now = q.top() ; q.pop() ; num[ team[now.id] ]--; if(num[ team[now.id] ]==0) pri[ team[now.id] ] = -1 ; printf("%d\n",now.id) ; } } printf("\n"); } int main() { int t , n , id , _cas = 0; while(scanf("%d",&t)>0 && t ){ memset( team , -1 , sizeof(team)); for(int i = 0 ; i < t ; i++) { scanf("%d",&n) ; while(n--){ scanf("%d",&id); team[id] = i ; } } printf("Scenario #%d\n",++_cas) ; work(); } }
相关文章推荐
- 这里想经过一个小程序研究标准库为 vector 对象提供的内存分配策,因为vector容器比list和deque容器用的很多,而且它的存储方式是连续的
- 自定义UIAlertView及简单的回调函数使用说明
- 关于uboot的CONFIG_SPL_BUILD宏的一个小知识点
- [HDU 1530] Maximum Clique 最大团
- Espresso实战:快速的Android UI自动化测试
- 辛星简译MySQL中的last_query_cost
- @GeneratedValue 四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
- C#中的属性(get、set、value)
- C#中的属性(get、set、value)
- The GNU configure and build system-configure的详细介绍
- UITableView 多层详解
- UITextfield后面只能输入小数点后2位
- IOS UIPageControl(分页控制器) 用法总结
- Android常用UI组件属性和.9图制作
- INDEX && PRIMARY KEY && UNIQUE KEY
- HDU 5412 CRB and Queries【整体二分+树状数组】
- 根据字体多少使UILabel自动调节尺寸
- 被忽略的 UITableViewHeaderFooterView
- Android UI--Action Bar
- iOS UIView动画实践(五):Keyframe Animation