用STL_map可以完成這題,map<int, int>Map,然後對應方式為Map[優先權] = 人員編號,因為Map為自動平衡的tree,所以優先權最大的會在tree的最右下方,也就是Map這個container的最後一個,而優先權最小的反之,在Map的第一個位置。另外map這container是用pair來存資料,因此要輸出人員編號要用->second。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <map> | |
using namespace std; | |
int main() | |
{ | |
map<int,int> Map; | |
int instruction, priority, number; | |
while (scanf("%d", &instruction)) { | |
switch (instruction) { | |
case 1: | |
scanf("%d%d", &number, &priority); | |
Map[priority] = number; | |
break; | |
case 2: | |
if (Map.empty()) puts("0"); | |
else { | |
printf("%d\n", (--Map.end())->second); | |
Map.erase(--Map.end()); | |
} | |
break; | |
case 3: | |
if (Map.empty()) puts("0"); | |
else { | |
printf("%d\n", Map.begin()->second); | |
Map.erase(Map.begin()); | |
} | |
break; | |
case 0: | |
return 0; | |
} | |
} | |
} |
沒有留言:
張貼留言