code1052 Hamster Game

Source: Internet
Author: User

Greedy algorithm, from the back forward

From the Codevs:

my tangled thinking process:
If every second has no duplicate of the gopher appears then surely it's a fight next to
If there are duplicate hamsters then consider hitting that better of course it's the most selected score
It
's reasonable to think of it this way, but ignoring a situation where the score is small, the hamster appears early, and the back-repeating hamster scores a lot more than the one that appeared earlier in the land.
We're not going to hit this little one, but we're going to use the time to beat the rat on the back.
e.g. 1 2 2
5 6 7
So, what about the score of the hamster and then the next fight?
And it's not right because a hamster with a big score may stay longer. We hit him early, and he's going to be short-lived, and the time is empty, ignoring what can be done.
such as 2 1
9 5
then read the puzzle to understand
The combination of the above two mistaken ideas is a positive solution.
We're going to enumerate the time and hit the hamster. This avoids the second case of error
and then, from all the hamsters, you can hit the biggest score. This avoids the first case of error
.

Set up a big top pile, the elements in the heap are the scores of the hamsters that can hit the current time.
The rats were sorted from the time of disappearance to the temp=, and the time of the last vanishing of the hamster disappeared to 1
Put all the dead time equal to temp hamster into the heap (indicating that you can hit them)
Then take out the largest (heap top) to abort, accumulate the score can

The code is as follows:

#include <iostream>#include<algorithm>#include<queue>#defineSize 105using namespacestd;intN;structmouse{intT,w;} Mouse[size];p riority_queue<int>Q;BOOLCNT (Mouse m1,mouse m2) {returnM1.t>m2.t;} intMain () {CIN>>N;  for(intI=1; i<=n;i++) {cin>>mouse[i].t; }     for(intI=1; i<=n;i++) {cin>>MOUSE[I].W; } sort (Mouse+1, mouse+n+1, CNT); intans=0;  for(inttemp=mouse[1].t,k=1;temp>0; temp--){         while(mouse[k].t==temp)            {Q.push (MOUSE[K].W); K++; }         if(!Q.empty ()) {ans+=Q.top ();        Q.pop (); }} cout<<ans<<Endl; return 0;} 

code1052 Hamster Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.