首页 > 其他分享 >buy

buy

时间:2023-09-23 16:23:50浏览次数:40  
标签:buy int top ans include scanf define

Buy Low Sell High

考虑反悔贪心。

对于三个股票 \(i,j,k,p_i<p_j<p_k\),我们可以在发现有利可图时立刻卖掉 \(p_i\),然后插入 \(p_j\)(用来反悔),然后再插入一个 \(p_j\)(真正用来交易),下次遇到 \(p_k\),我们搞用来反悔的 \(p_j\),发现 \((p_k-p_j)+(p_j-p_i)=p_k-p_i\),符合要求。

注意由于只能交易一次,所以应该用 if。(我们发现反悔实际上中途也是只能进行一次交易的)。

#include<cstdio>
#include<queue>
using namespace std;
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while

const int N=300010;
int n;
typedef long long ll;
ll ans;
priority_queue<int,vector<int>,greater<int>>q;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    #endif
    scanf("%d",&n);
    E(i, n){
        int x;
        scanf("%d",&x);
        if(!q.empty()&&x>q.top()){
            ans+=(x-q.top());
            q.pop();
            q.push(x);
        }
        q.push(x);
    }
    printf("%lld",ans);
    return 0;
}

标签:buy,int,top,ans,include,scanf,define
From: https://www.cnblogs.com/wscqwq/p/17724544.html

相关文章

  • B. Buying gifts[贪心]
    Problem-1801B-Codeforces题意是需要给两个人买礼物,有n个商店,每个商店只能给一个人买,而且每个商店给两个人买的礼物的价钱也可能不同,问给两人买的礼物的最大价格之差最小是多少。我们考虑这种情况。如果当前给b买的礼物最大值为x,那么那些商店里给b礼物价格小于等于x的我们......
  • [LeetCode][309]best-time-to-buy-and-sell-stock-with-cooldown
    ContentYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Findthemaximumprofityoucanachieve.Youmaycompleteasmanytransactionsasyoulike(i.e.,buyoneandselloneshareofthestockmultipletimes)w......
  • [LeetCode][121]best-time-to-buy-and-sell-stock
    ContentYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitbychoosingasingledaytobuyonestockandchoosingadifferentdayinthefuturetosellthatstock.Returnthemaximu......
  • POJ - Buy Tickets
    Smiling&Weeping----你看这个人,嘴里说着喜欢我却又让我这么难过DescriptionRailwayticketsweredifficulttobuyaroundtheLunarNewYearinChina,sowemustgetupearly......
  • CF865D Buy Low Sell High
    CF865DBuyLowSellHigh我发现自己是真的学不会贪心……太玄学了。这是一道反悔贪心的题目,比较简单的那种。题意你是一棵韭菜,喜欢炒股,每天可以买入一股或卖出一股,且最后一天之后你持有的股票数目应该为\(0\)。你现在知道\(n\)天的股票价格,求最大获利。思路首先一个很显......
  • Buyer's Call
    http://www.investopedia.com/terms/b/buyerscall.asp#axzz1r7gvLjmmhttp://www.investopedia.com/terms/c/call.asp#axzz1r7gvLjmmDefinitionof'Buyer'sCall'Anagreementbetweenabuyerandsellerwherebyacommoditypurchaseoccursataspecifi......
  • [USACO08NOV]Buying Hay S
    [USACO08NOV]BuyingHayS题目描述FarmerJohnisrunningoutofsuppliesandneedstopurchaseH(1<=H<=50,000)poundsofhayforhiscows.HeknowsN(1<=N<=100)haysuppliersconvenientlynumbered1..N.Supplierisellspackagesthatcon......
  • Missing binding E:\server\dovip\buyer-pc-web\node_modules\node-sass\vendor
    errorin./src/components/Search.vue?vue&type=style&index=0&id=7cb41050&scoped=true&lang=scss&SyntaxError:Error:MissingbindingE:\server\dovip\buyer-pc-web\node_modules\node-sass\vendor\win32-x64-83\binding.nodeNod......
  • 洛谷 P4544 [USACO10NOV]Buying Feed G - 题解
    https://www.luogu.com.cn/problem/P4544感觉是很没意思的DP+DS优化,以前做过几道更难的题:https://codeforces.com/contest/1788/problem/Ehttps://atcoder.jp/contests/abc288/tasks/abc288_f这种题只要是让我写总是能把代码整的伤痕累累(逃第一眼:我艹不就是一个sbDP吗......
  • java.lang.NoSuchMethodException: com.innovation.web.BuyServlet.get(javax.servlet
    问题描述我将路径定义到相应的servlet的函数方法里面,然后就出现了这个问题,很明显的找不到相应的函数方法;问题解决将目光重新放到我定义的相关路径那里,发现我出于习惯,将servlet里面原本应该是名为checkIt的函数方法写成了get方法,改回去之后,这个问题也就解决啦!......