(正难则反)D - Supermarket POJ - 1456

贪心,从最大的

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 typedef long long ll;
 6 vector <int> V[10005];
 7 priority_queue <int,vector<int>,less<int> > Q;
 8 int main(){
 9     int n;
10     std::ios::sync_with_stdio(false);
11     while(cin>>n){
12         int cnt=0;
13         for(int i=1;i<=10000;i++) V[i].clear();
14         for(int i=1;i<=n;i++){
15             int a;int b;cin>>a>>b;
16             V[b].push_back(a);
17             cnt=max(cnt,b);
18         }
19         //cout <<cnt<<endl;
20         ll ans=0;
21         for(int i=cnt;i>=1;i--)
22         {
23             //cout <<"in";
24             for(int j=0;j<V[i].size();j++){
25                 //cout <<V[i][j]<<endl;
26                 Q.push(V[i][j]);
27             }
28             if(!Q.empty()) 
29             {
30                 ans+=Q.top();Q.pop();
31             }
32         }
33         cout <<ans<<endl;
34         while(!Q.empty()) Q.pop();
35     }
36     return 0;
37 }

 

天数往前推,因为保质期时间长的可以在保质期之前卖掉,从最大天数开始维护优先队列,如果优先队列不空的话,每次找出最大价值的。

posted @ 2019-07-28 21:47  Chuhanjing  阅读(193)  评论(0编辑  收藏  举报