zoukankan      html  css  js  c++  java
  • HZOJ trade

    强烈谴责$skyh$的没$Huge 脸$行为。

    很经典的可反悔贪心,然而我一直以为是sbdp还一直想着怎么优化……

    正常的贪心肯定是不对的。

    但是由于A-C=A-B+B-C,

    所以用一个小根堆维护,每次将当前天的a加入,表示当前天可以买入,

    如果堆顶小于a,取出堆顶,ans加上差,再次将当天的a加入,那么如果之后卖出更优则会减掉当前的a利用差价满足了贪心性质。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #define int LL
     6 #define LL long long
     7 using namespace std;
     8 int n,a[100010];
     9 
    10 inline int read();
    11 signed main()//dp xds//优化dp//单调
    12 {
    13 //    freopen("trade14.in","r",stdin);
    14 
    15     n=read();for(int i=1;i<=n;i++)a[i]=read();
    16     
    17     priority_queue<int,vector<int>,greater<int> >A;
    18     LL ans=0;A.push(a[1]);
    19     for(int i=2;i<=n;i++)
    20     {
    21         if(A.top()<a[i])
    22         {
    23             ans+=a[i]-A.top();A.pop();
    24             A.push(a[i]);
    25         }
    26         A.push(a[i]);
    27     }
    28     printf("%lld
    ",ans);
    29 }
    30 inline int read()
    31 {
    32     int s=0,f=1;char a=getchar();
    33     while(a<'0'||a>'9'){if(a=='-')f=-1;a=getchar();}
    34     while(a>='0'&&a<='9'){s=s*10+a-'0';a=getchar();}
    35     return s*f;
    36 }
    View Code
  • 相关阅读:
    Erlang/OTP:基于Behaviour的回调函数
    使用ACE创建进程
    linux查看硬件信息
    测试~~
    很好的:纠错函数linux
    转帖
    sss
    转帖
    普通函数、虚函数、纯虚函数、
    ACE_Event_Handle
  • 原文地址:https://www.cnblogs.com/Al-Ca/p/11636250.html
Copyright © 2011-2022 走看看