Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High [贪心 II][数据结构 I]

题目:http://codeforces.com/contest/867/problem/E

题意:模拟股票操作,每天只能买一只股票或者卖一只股票或者什么也不做,求最大利润。

题解:仔细想想是非常简单的一个贪心问题,理解为连续的多次贪心买入卖出可以合并为一次的买入卖出,且值为最优。只需要用一个最小堆每次寻找前面的最小且没有被标记为购买点的值即可。如果自己为最小值,continue。如果那个值没有被选过,为买入点,pop。如果那个值被选为了售出的点,那么取消售出的标记,把当前点标记为售出点,利润直接加为和堆顶的差值。

#include<bits/stdc++.h>
#define pii pair<int, int>
#define mod 1000000007
#define mp make_pair
#define pi acos(-1)
#define eps 0.00000001
#define mst(a,i) memset(a,i,sizeof(a))
#define all(n) n.begin(),n.end()
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)|1)
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 3e5+5;
priority_queue<pii,vector<pii>,greater<pii>>a;
int has[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int i, j, k, m, n;
    cin>>n;
    ll ans =0;
    for(int i=  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;
}
时间: 2024-08-24 13:38:20

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High [贪心 II][数据结构 I]的相关文章

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)

A 签到 B 题意 分析 C 题意 现有两种pizza, 每张pizza可分为s块,有n个人,分别给出n的人需要的块数,吃第一种1块获得的价值,吃第二种1块获得的价值,问在需要最少的pizza的数量下的可以获得最大价值为多少 分析 关键点:每个人都取最优,两种pizza余下的不会超过两张pizza 故可以将所有取最优,如果余下的可以组成一张,分别考虑第一种转为第二种和第二种转为第一种取最优即可 处理余下的最优的方法: 将第一种价值和第二种价值的差值进行排序 D 较难 E. Buy Low Sel

Codeforces Round #437 (Div. 2)

Codeforces Round #437 (Div. 2) codeforces 867 A. Between the Offices(水) 题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从西雅图飞到旧金山次数更多. 题解:只要判断第一天和最后一天状态即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 10

Codeforces Round #500 (Div. 2) [based on EJOI]

Codeforces Round #500 (Div. 2) [based on EJOI] https://codeforces.com/contest/1013 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define IT set<node>::iterator 6 #def

MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)

http://codeforces.com/contest/452/problem/B B. 4-point polyline time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rectangular grid of lattice points from (0, 0) to (n, m) inc

MemSQL Start[c]UP 2.0 - Round 2 - Online Round

搞到凌晨4点一个没出,要gg了. A. Golden System http://codeforces.com/contest/458/problem/A 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const double q=(sqrt(5.0)+1)/2; 7 const int M=100

MemSQL Start[c]UP 2.0 - Round 1

搞了好久才把大部分题目题解看完了,真是太弱了. A题简单暴力题一个一个匹配,对应位置字母要么相同,要么是'.'. B题给定一个矩阵,左下角(0,0),右上角(n, m),取4个不同的点连成一段折线,要有最长的折线长度. 排除n == 0 和m == 0 ,剩下的情况中总共由4中情况: 枚举一下就可以了 1. (0,0)->(n,m)->(0, m)->(n, 0) 2. (0,0)->(n,m)->(n, 0)->(0, m) 3.(n,m-1)->(0,0)-&

MemSQL Start[c]UP 2.0 - Round 2

反正晚上睡不着,熬到1点开始做比赛,6个题目只做了2个题目,而且手速还比较慢,待提升空间还很大呢. A题:给定两个0,1串(len<=100000), 但是不是普通的二进制串,而是q进制串,q = (√5 + 1)/2,比较这两个串的大小. 分析:q的长度很大,即使用大数似乎也不怎么合理,其实我写了半天大数,用最大长度数据测试发现根本出不了结果,也许我写的太挫了,也学时间就耗在上面了. 最终只能另想方法,而且题目中也说明了q^2 = q+1,所以很容易想到利用这个来做这个题, 观察到对于一个串中

MemSQL Start[c]UP 2.0 - Round 1 B. 4-point polyline (线段的 枚举)

昨天cf做的不好,居然挂零了,还是1点开始的呢.,,, a题少了一个条件,没判断长度. 写一下B题吧 题目链接 题意: 给出(n, m),可以得到一个矩形 让你依次连接矩形内的4个点使它们的长度和最长,而这三条线段可以相交.交叉 分析:这种情况下,枚举对角线的四个点,当时我也想过,我只用了其中的一种 方式,其实有四种方式判断,好像没什么道理. 上图吧: MemSQL Start[c]UP 2.0 - Round 1 B. 4-point polyline (线段的 枚举)

MemSQL Start[c]UP 2.0 - Round 2 - Online Round A,B,C

MemSQL Start[c]UP 2.0 - Round 2 - Online Round 题目链接 不得不说这场真心不好打啊... A:黄金分割进制数,满足一个性质,对于第i位xi=xi?1+xi?2,这样一来就可以把所有位推到前两位去比较大小,不过单单这样直接搞果断爆longlong无限WA8,最后发现在推的过程中,有一位上面差值大于1,就可以直接判断了 B:不得不吐槽一下毛子的英语,Today's ying yu is very hao,题目看了非常非常久,不能更逗.其实看懂的就挺简单了