Test instructions: Some stock prices, we can choose to buy and sell, but only one operation a day, ask the maximum profit
Idea: For the day, if the sale && before he was smaller, we must be looking for a minimum day of buy, but do not know whether the sale is the most profitable, so we can use Multiset, this and set similar, but can store the same number, and sort
So we delete that smallest, add 2 current, one is relay, one is number,
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4multiset<int>s;5multiset<int>:: iterator it;6 7 intMain () {8 intN;9Cin>>N;Tenll sum=0; One for(intI=1; i<=n;i++){ A intx; -scanf"%d",&x); - if(!s.empty () &&x>*S.begin ()) { thesum+= (X-*s.begin ()) *1LL; - s.erase (S.begin ()); - S.insert (x); - S.insert (x); + } - ElseS.insert (x); + } Acout<<sum<<Endl; at return 0; -}
Codeforces Round #437 (Div. 2, based on Memsql start[c]up 3.0-round 2) E. Buy low Sell high