题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=3288
Resource Allocation
Description
HDU-Sailormoon is made up of three girls~~~wj, xq, lff, usually they work together ---- solve a variety of problems. So they has to use some resources in the process.
In order to make it more convenient, they will put some resources in a box big enough, each resource has its ID and level of priority. When they want a kind of resources, they will give its ID and get it from the box. If there are several ones available in the box, they will get the highest priority ones. If there are still several ones available, they will get the one which puts in the box first.
Input
The input will consist of several cases, please deal with till the end of file. Each case contains a integer N(0<N<=10000), representing there are N steps following. For example, if input is "R x y"(x, y are integers,0<=x,y<=10000), representing they put a resource to the box, its ID is x, and its priority is y(the higher the priority is, the smaller the y is). If input is "name r" (name may be "wj" or "xq" or "lff", r is an integer,0<=r<=10000), representing one girl called "name" wants a resource, which ID is r.
Output
When the input is "R x y", the resource will mark a number k (begin from 1). When the input is "name r", please find out a resource in the box, if there is one available, print "name gets Num k: x y!", name referred to the input, k is the mark number of resource, x is the resource‘s ID and y is the level of priority, or print "No one fits!".
Sample Input
9
R 1 5
R 2 3
R 1 5
R 2 0
wj 1
xq 2
lff 3
lff 2
xq 2
Sample Output
wj gets Num 1: 1 5!
xq gets Num 4: 2 0!
No one fits!
lff gets Num 2: 2 3!
No one fits!
优先队列。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<set> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::set; 15 using std::pair; 16 using std::vector; 17 using std::multiset; 18 using std::priority_queue; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 10010; 29 typedef unsigned long long ull; 30 struct Node { 31 int fix, pos; 32 Node(int i = 0, int j = 0) :fix(i), pos(j) {} 33 inline friend bool operator<(const Node &a, const Node &b) { 34 return a.fix == b.fix ? a.pos > b.pos : a.fix > b.fix; 35 } 36 }; 37 priority_queue<Node> que[N]; 38 int main() { 39 #ifdef LOCAL 40 freopen("in.txt", "r", stdin); 41 freopen("out.txt", "w+", stdout); 42 #endif 43 char buf[10]; 44 int n, id, fix; 45 while (~scanf("%d", &n)) { 46 int pos = 1; 47 rep(i, n) { 48 scanf("%s", buf); 49 if (buf[0] == ‘R‘) { 50 scanf("%d %d", &id, &fix); 51 que[id].push(Node(fix, pos++)); 52 } else { 53 scanf("%d", &id); 54 if (que[id].empty()) { puts("No one fits!"); continue; } 55 Node t = que[id].top(); que[id].pop(); 56 printf("%s gets Num %d: %d %d!\n", buf, t.pos, id, t.fix); 57 } 58 } 59 rep(i, N) while (!que[i].empty()) que[i].pop(); 60 } 61 return 0; 62 }