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