2016-05-31 18:22:32
Title Link: Gopher Game Codevs no.1245
Main topic:
At first, all the rats appeared, but the duration (s) and the points of the hit were different, finding the best strategy (1s dozen) to hit the hamster.
Solution:
Greedy + heap Optimization
Select by time, and each time the current time end of the gopher is added to the collection
Take the top point in the maximum heap per second to add the answer
Places to be aware of:
1. The chronological order must be correct, the election is wrong
1 //Gopher Games (Codevs no.1052)2 //Greedy3#include <stdio.h>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn= the;8 structnode9 {Ten intkey; One intTime ; A }; - node F[MAXN]; - BOOLComp (node A,node b) the { - returnA.time>B.time; - } -Priority_queue <int>Q; + intans; - intN; + intMain () A { atscanf"%d",&N); - for(intI=1; i<=n;i++) scanf ("%d",&f[i].time); - for(intI=1; i<=n;i++) scanf ("%d",&f[i].key); -Sort (f+1, f+n+1, comp); - intnow=f[1].time; - intLoc=1; in while(now) - { to while(f[loc].time==Now ) + { - Q.push (f[loc].key); theLoc++; * } $ if(!q.empty ())Panax Notoginseng { -ans+=q.top (); the Q.pop (); + } Anow--; the } +printf"%d", ans); - return 0; $}
Gopher Game Codevs no.1245