Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=3288
Resource allocationdescription
Hdu-sailormoon is made up of three girls~~~wj, XQ, LFF, usually they work together----solve a variety of problems. So they have to use some resources in the process.
In order to do it more convenient, they would put some resources in a box big enough, each resource have its ID and level of priority. When they want a kind of resources, they'll give its ID and get it from the box. If there is several ones available in the box, they'll get the highest priority ones. If there is still several ones available, they'll get the one which puts in the box first.
Input
The input would consist of several cases, please deal with till the end of file. Each case contains a integer n (0<n<=10000), representing there is N steps following. For example, if input is "R x y" (x, Y was integers,0<=x,y<=10000), representing they put a resource to the box, its ID is x, and it priority is Y (the higher of the priority is, the smaller of the Y is). If input is "name R" (name could be "WJ" or "XQ" or "Lff", 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 would 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 are the mark number of resource, X is the resource ' s ID and y are 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!
Priority queue:
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 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingSTD::Set; the usingstd::p air; - usingstd::vector; - usingStd::multiset; - usingstd::p riority_queue; + #definePB (E) push_back (e) - #defineSZ (c) (int) (c). Size () + #defineMP (A, b) Make_pair (A, B) A #defineAll (c) (c). Begin (), (c). End () at #defineITER (c) Decltype ((c). Begin ()) - #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intN =10010; intypedef unsignedLong Longull; - structNode { to intfix, POS; +Node (inti =0,intj =0): Fix (i), POS (j) {} -Inline friendBOOL operator< (ConstNode &a,ConstNode &b) { the returnA.fix = = B.fix? A.pos > B.pos:a.fix >B.fix; * } $ };Panax NotoginsengPriority_queue<node>Que[n]; - intMain () { the #ifdef LOCAL +Freopen ("In.txt","R", stdin); AFreopen ("OUT.txt","w+", stdout); the #endif + Charbuf[Ten]; - intN, id, fix; $ while(~SCANF ("%d", &N)) { $ intpos =1; - Rep (i, N) { -scanf"%s", buf); the if(buf[0] =='R') { -scanf"%d%d", &id, &fix);WuyiQue[id].push (Node (fix, pos++)); the}Else { -scanf"%d", &ID); Wu if(Que[id].empty ()) {Puts ("No One fits!");Continue; } -Node T =que[id].top (); Que[id].pop (); Aboutprintf"%s gets Num%d:%d%d!\n", buf, T.pos, ID, t.fix); $ } - } -Rep (i, N) while(!que[i].empty ()) Que[i].pop (); - } A return 0; +}
View Code
HDU 3288 Resource Allocation