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]

Source: Internet
Author: User
Tags memsql

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.