I think this is a top priority queue. If you don't want to learn it, what should you say? Although sometimes I can imitate its functions, but some questions will be a lot of bloodletting, and more learn to use a small Dongdong will not hurt, not to mention I will never let go, oh yeah, hey
Priority queue is a type of queue. Remember the stack I wrote and the queue said that normal queues do not have Q. Top? Well, I didn't seem to add the word 'normal' at the time. The priority queue is that only the returned function is different from the normal queue. One is top and the other is front, but the function is different, the priority queue can be selected based on your own priority rules. What is the alternative queue? Are you stupid? This is not clear yet? Go, find a Chinese teacher in your book. The so-called selectivity is that there are three methods of selecting rules for priority queue, maybe the next one is your first one ~~ Hey
First, I like it most, that is, writing rules directly in the defined struct.
Struct ssss
{
Int X;
Bool operator <(const ssss & A) const
{
Return x> A. X; // The smaller the value, the higher the priority.
}
}
If you define this queue, you can directly write priority_queue <ssss> q. You can see it clearly. If you open your titanium alloy eye, everyone on Earth knows that the queue is preceded by an underscore, don't tell me you can't see the semicolon behind Q --!
Do you know what the priority removing rule is? I will tell you one second in advance when I pay the protection fee for 10 minutes->.->! Okay, one second passed.
It can be understood that the in the bool line takes the address and is what you operate on. What you want is to set the higher the priority of the X value in Priority, this way... hey, it forms a great struct-the one above -. -! So I finally understood it, right? Okay, let me talk about it again. I assume it is used for a struct element a to load the current optimal element. The highest priority is used to compare it with other elements in the queue, if the X of the elements to be compared is smaller than the X of A, the return value is true. Won't you understand it here? Hey, it seems you are not doing this yet. Yes, not true. It should be false.
->.->! You can think this by yourself. When the comparison returns true, it means that the elements compared with him are higher than those in the priority rule you set. Then, is it swollen? Wipe, of course, it's ugly to roll one side, isn't this top priority to keep it half?
Alas, Do you understand it? I don't know how to give you the time to digest a song? Forget it. Don't let me scold you for being stupid. I also checked the priority queue two hours ago...
Then, aren't there two long and ugly statements? I'm not going to talk about columns. I don't think it's practical. What? Do you say I won't use it? If it is written, you want to invite me to dinner and divide my wife into half of me.
Struct CMP
{
Bool operator () (Int & A, Int & B)
{
Return A> B; // The smaller the value, the higher the priority.
}
}
If this queue is defined, you can directly write priority_queue <int, vector <int>, CMP> q. The first int in the queue is the data type installed in the queue, just like my ssss above (SSSS is my favorite, you are not allowed to use it), CMP is the struct, which is the so-called priority rule. I will not talk about columns here, it's a lot of trouble to write another struct to hold these things. You want to say that you can define multiple things and then use which one to use? Hey, you are so smart. What's the difference with some pig? Don't tease me OK?
There is another one. There is no need to write these things, that is, the system sends you a package, but there are also flaws in convenience. At least I think it is, that is, it can only compare the size of one layer, you can't beat the data in minutes. For example, if you want to help me pick my wife, I want to be tall, but it cannot be higher than me, Grandpa, you don't need to pick up the tall ones and lose them to me. You'll get a 2m1 mother, Yao Ming, and I'll have to pick up my toes for a nap later, do you mean I'm tired? But let's take a look at this stuff, that is, the definition.
Priority_queue <Int. Vector <int>, greater <int> q1; // priority
Priority_queue <Int. Vector <int>, less <int> Q2; // The smaller the value, the higher the priority.
These are two definitions. I want to say that the first Int Is the data type, that is, you can use Q. (INT). Do you understand? Don't plug in anything in the queue. Why? I put a bunch of spaces in front of my head. You can't see it --! You can't really tease me. Okay, let's talk about why. You'll know it if you don't try it with a space. It's just worth mentioning, oh, yeah! If you don't put it, it will cause a compilation error. The computer will use the two '<' as something else. If you forget what it is called, it is a bit operation, but you don't want to learn how exaggerated it is, A space is good. I am afraid you will not be able to see it before coming!
Have you lost so many useless words, or have a super simple example come true? Well, I will dedicate my priority queue virgins to the work of virgins, not virgins, because I am a male, and I am a male, thank you!
This is a case. Can I enter up to 60000 times? Hey, then there is a difference, or it is a put. Then input a string and two numbers. The first number is useless and will be followed during output, the second one is used for comparison. The smaller the meaning is, the higher the priority is. Then it may be get. At this time, you need to output the highest priority in the queue, however, if the queue is empty, output the English sentence. I hate English. Don't force me to write empty.
Queue! Well, I wrote this sentence! The last key point is that if two or more elements are given the highest priority, that is, the second input number is equal and is the smallest data in the queue, the oldest optimal element is output ~ Do you not know how to mark it with a K? I will tell you how to directly use k ++? Okay, I betrayed my exercises again. Let's take a look at the example!Windows Message Queue
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2576 accepted submission (s): 1008
Problem descriptionmessage queue is the basic fundamental of Windows system. for each process, the system maintains a message queue. if something happens to this process, such as mouse click, text change, the system will add a message to the queue.
Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. note that the less priority value means the higher priority. in this problem, you are asked to simulate the message queue for putting
Messages to and getting message from the message queue.
Inputthere's only one test case in the input. each line is a command, "get" or "put", which means getting message or putting message. if the command is "put", there're one string means the message name and two integer means the Parameter
And priority followed. there will be at most 60000 command. note that one message can be 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 .) process to the end-of-file.
Outputfor each "get" command, output the command getting from the Message Queue with the name and parameter in one line. If there's no message in the queue, output "Empty queue! ". There's no output for" put "command.
Sample Input
GETPUT msg1 10 5PUT msg2 10 4GETGETGET
Sample output
EMPTY QUEUE!msg2 10msg1 10EMPTY QUEUE!
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
Using namespaceSTD;
StructSsss
{
IntNumber,P,X;
CharName[111];
Bool operator <(constSsss&S) Const
{
If (P=S.P) Return
X>S.X; // If the number inside is smaller, the priority is higher, that is, the first number entered above.
ReturnP>S.P; // Otherwise, output the first bird element that comes in.
}
}SS;
Priority_queue<Ssss>Q; // This is the definition in the legend
Int main (void)
{
IntX=0;
CharS[11];
While (Scanf("% S",S)! =EOF)
{
If (!Strcmp(S,"Put") // Determine whether it is put-
-!
{
Scanf("% S % d",SS.Name,&SS.Number,&SS.P);
// Enter the name, number, and number used for priority comparison, that is, the second number.
SS.X=X++;Q.Push(SS);
// Here we use X ++ to record it. I only need to output x ++ once, so that X will only grow bigger and bigger. Is the smaller the result, the more advanced?
} Else
{
If (Q.Empty())Puts("EMPTY
Queue! "); // Don't tell me to tell you why I output this
ElsePrintf("% S % d \ n",Q.Top().Name,Q.Top().Number),Q.Pop();
// Output the output
}
}
Return0;
}
Conclusion: To sum up, I just got a look at the top queue, But I was inspired to learn a little faster and deeper. Don't smoke me, I'm a child, still very pure ....