Title: Http://codeforces.com/contest/867/problem/E
Test instructions: Simulate stock operations, buy only one stock per day or sell a stock or do nothing for maximum profit.
Solution: It is very simple to think about a greedy problem, understood as the successive greedy buy and sell can be combined into a one-time buy and sell, and the value is optimal. You only need to use a minimum heap each time you look for a value that is the smallest in front and not marked as a point of purchase. If you are the minimum value, continue. If the value has not been selected, for the buy point, pop. If the value is selected for the point of sale, then the mark of the sale is canceled, the current point is marked as the point of sale, and the profit is directly added to the difference between the top of the heap.
#include <bits/stdc++.h>#definePII Pair<int, int>#defineMoD 1000000007#defineMP Make_pair#definePi ACOs (-1)#defineEPS 0.00000001#defineMST (A,i) memset (A,i,sizeof (a))#defineAll (n) n.begin (), N.end ()#defineLson (x) ((x<<1))#defineRson (x) ((x<<1) | |)#defineINF 0x3f3f3f3ftypedefLong Longll;typedef unsignedLong Longull;using namespacestd;Const intMAXN = 3e5+5;p Riority_queue<pii,vector<pii>,greater<pii>>A;intHAS[MAXN];intMain () {Ios::sync_with_stdio (false); Cin.tie (0); Cout.tie (0); intI, J, K, M, N; CIN>>N; ll ans=0; for(intI=1; i<=n;++i) {cin>>K; A.push (MP (K,i)); if(A.top (). first>=k)Continue; Ans+=k-A.top (). First; if(!Has[a.top (). Second]) {A.pop (); Has[i]=1; } Else{has[a.top (). Second]=0; Has[i]=1; }} cout<<ans<<Endl; return 0;}
Codeforces Round #437 (Div. 2, based on Memsql start[c]up 3.0-round 2) E. Buy low Sell high [greedy ii][data structure I]