cf437E(贪心+优先队列)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/83246200

题意:知道每天股票价格,可以在任意一天购买或出售一个股票,求最大获利

这个可以这样考虑,把这些股票分成2部分,买的和卖的。。然后对一个股票的决策,有2种情况可以操作,一种是直接从买的股票中选最小的和他配对。那这样就会产生一些问题,这个股票的最佳配对可能不是最小的,可能是买这个股票。那么就有第二种操作,就是把一个卖的股票变成买的股票,然后用当前的股票来代替他,这样就可以化解上面的情况了。。

然后需要用2个堆来维护这2堆股票,从2个堆中选最小的股票去进行处理就行了。。如果这个股票都没有这2堆股票大,显然是要放进买的那堆。。最后对于没有和他匹配的买的股票,就可以不进行任何操作了。。

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 300005
#define nm 2005
#define pi 3.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}







int n,a[NM];
ll ans;
priority_queue<int,vector<int>,greater<int> >q,t;

int main(){
    n=read();inc(i,1,n)a[i]=read();
    q.push(a[1]);
    inc(i,2,n){
	if(t.empty()||(!q.empty()&&q.top()<t.top())){
	    if(q.top()<a[i]){
		t.push(a[i]);ans+=a[i]-q.top();q.pop();
	    }else q.push(a[i]);
	}else{
	    if(t.top()<a[i])q.push(t.top()),t.push(a[i]),ans+=a[i]-t.top(),t.pop();
	    else q.push(a[i]);
	}
    }
    return 0*printf("%lld\n",ans);
}

E. Buy Low Sell High

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.

Input

Input begins with an integer N (2 ≤ N ≤ 3·105), the number of days.

Following this is a line with exactly N integers p1, p2, ..., pN (1 ≤ pi ≤ 106). The price of one share of stock on the i-th day is given by pi.

Output

Print the maximum amount of money you can end up with at the end of N days.

Examples

Input

Copy

9
10 5 4 7 9 12 6 2 10

Output

Copy

20

Input

Copy

20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4

Output

Copy

41

Note

In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is  - 5 - 4 + 9 + 12 - 2 + 10 = 20.

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/83246200
今日推荐