AtCoder Beginner Contest 137 D title [greedy]

[Title] meaning a total of N and M-day mission, one day can only do one task, you can get the job done after Bi wages (Ai-1) day after day, you can take up to days to ask M how much to pay.

Links: https://atcoder.jp/contests/abc137/tasks/abc137_d

[Thinking] sort tasks by time, time to do the task ii are thrown into the pile, because the task is disposable, so each went into the task will no longer put up. Then look at the stack each time there are no numbers, some words taken out of the top of the heap, and each time only to take time. Obviously correctness.

Code:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define int long long
 6 struct str{
 7     int day,money;
 8 }st[120000];
 9 priority_queue<int> q;
10 bool cmp(str a,str b){
11     return a.day<b.day;
12 }
13 signed main(){
14     int n,m;
15     cin>>n>>m;
16     for(int i=1;i<=n;i++)
17         cin>>st[i].day>>st[i].money;
18     int ans=0;
19     int now=1;
20     sort(st+1,st+1+n,cmp);
21     for(int i=1;i<=m;i++){
22         while(st[now].day<=i&&now<=n){
23             q.push(st[now++].money);
24         }
25         if(!q.empty()){
26             years + = q.top ();
27              q.pop ();
28          }
 29      }
 30      cout << years;
31      return  0 ;
32  } 
 33  //

 

Guess you like

Origin www.cnblogs.com/pengge666/p/11564230.html