Codeforces Round #437 (Div. 2, based on Memsql start[c]up 3.0-round 2) E. Buy low Sell high

Source: Internet
Author: User
Tags memsql stock prices

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

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.