tokitsukaze and Soldier

Topic links:

https://ac.nowcoder.com/acm/problem/50439

Ideas:

(Novice chicken dish did not start thinking about a queue QAQ) s first to require every soldier as a standard descending order, then traverse, after when the queue is less than the current number of soldiers required number of people, the soldiers put into the smallest the value of the priority queue, contrary to pop the smallest queue capacity value of people (ie, team head), until qualified. Maximum value recorded during the traversal.

Problem-solving Code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <string>
 6 #include <cstring>
 7 #include <map>
 8 #include <queue>
 9 using namespace std;
10 const long long maxn = 1e5 + 7;
11 typedef long long ll;
12 struct people
13 {
14     int v;
15     intS;
 16 } P [ 100010 ];
 . 17 The priority_queue < int , Vector < int >, Greater < int >> P2; // minimum priority queue 
18 is  BOOL CMP (A people, people B)
 . 19  {
 20 is      return AS> BS ;
 21 is  }
 22 is  int main ()
 23 is  {
 24      int n-;
 25      LL SUM = 0 , MaxNum = 0 ;
 26 is      CIN >> n-;
 27      for ( int I =. 1 ; I <= n-; I ++ )
 28      {
 29          CIN >> P [I] .v >> P [I] .s;
 30      }
 31 is      Sort (P + . 1 , P + . 1 + n-, CMP);
 32      for ( int = I . 1 ; I <= n-; I ++ )
 33 is      {
 34 is          the while (p2.size ()> = P [I] .s)   // once the queue is less than the current number of soldiers is not required, the minimum value of the queue pop people 
35          {
 36              SUM - = p2.top ();
 37 [              p2.pop ();
 38 is          }
 39          SUM = + p[i].v;
40         p2.push(p[i].v);
41         maxnum = max(maxnum,sum);
42     }
43     cout << maxnum << endl;
44 
45     return 0;
46 }

 

Guess you like

Origin www.cnblogs.com/emhhbw/p/12566345.html