码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 867e 思维

时间:2017-10-14 23:35:36      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:题意   span   class   ref   lin   pair   double   return   stdio.h   

Buy Low Sell High

题意:给出n天股票的价格,每天可以选择买入或者卖出一个股票,或者什么都不做,问最后一天的收益最多是多少(把所有股票卖掉)

思路:用优先队列将买入的股票存起来,每次取出最大值,若比当前的股票价钱低,则卖出,然后注意,无论当天是否卖出股票,都一定要买入,如果当天不能卖出股票,自然要买入,因为这个买入是不需要计算到支出里面去的,只有卖出的时候需要计算支出(即卖出价格与买入价格做差),当天卖出也要买入,原因是当天在整个n天是时间中并不一定是需要卖出的,这里买入类似于求最大流回流的作用

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=3e5+100;

priority_queue<ll,vector<ll>,greater<ll> > Q;
ll n,x,ans;
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; ++i){
        cin>>x;
        ll now=inf;
        if(!Q.empty()) now=Q.top();
        if(x>now){
            ans+=x-now;
            Q.pop();
            Q.push(x);
        }
        Q.push(x);
    }
    cout<<ans<<endl;
    return 0;
}

 

codeforces 867e 思维

标签:题意   span   class   ref   lin   pair   double   return   stdio.h   

原文地址:http://www.cnblogs.com/max88888888/p/7668910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!