zoukankan      html  css  js  c++  java
  • 股神小L

    题解 贪心

    若当前手中还持有股,则一定会卖出去。

    否则,考虑之前卖出的最便宜的股,若售价比当前的股高,就买下这个股,否则我们就把之前卖出的最便宜的股改为买入,这样一定会有股,然后再把这个股卖出即可。

    简单题,用堆维护即可。

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #define LL long long
    #define M 200020
    #define mid ((l+r)>>1)
    using namespace std;
    LL read(){
    	LL nm=0,fh=1; char cw=getchar();
    	for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
    	for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
    	return nm*fh;
    }
    LL n,m,ans;
    priority_queue<LL> Q;
    int main(){
    	n=read();
    	for(LL tot=0,i=1;i<=n;i++){
    		m=read();
    		if(tot) tot--,ans+=m,Q.push(-m);
    		else if(!Q.empty()&&m>-Q.top()){
    		    ans+=m+(Q.top()*2ll);
    			Q.pop(),Q.push(-m),tot++;
    		}
    		else tot++,ans-=m;
    	}
    	printf("%lld
    ",ans); return 0;
    }
    

      

  • 相关阅读:
    ZOJ4125 Sekiro
    ZOJ4118 Stones in the Bucket
    ZOJ4115 Wandering Robot
    ZOJ4113 Calandar
    【递归】N皇后问题 和 2n皇后问题 dfs
    7-18
    7_13
    二维前缀和
    64位整数乘法
    【分治】魔法石的诱惑
  • 原文地址:https://www.cnblogs.com/OYJason/p/9693180.html
Copyright © 2011-2022 走看看