Codeforces Round #437 (Div. 2) E. Buy Low Sell High
2017-10-03 12:21
459 查看
题目链接
题意是有n天,你知道接下来每一天股票的价格,你可以选择卖一张股票,买一张或者什么都不做,一开始你没有股票,且没有股票的时候不能卖,问说你n天后能获得的最大利润。
非常玄妙的贪心题。假设我们当前天得到一个股票的价格x,那么我们去之前的日子里找一个价格最低的股票y,如果y<x的话那么我们相当于可以在这天把y股票升值为x且赚到(x-y)元,同时注意这时股票y仍然可以买。
举个例子 1 , 2 , 3 , 4
第一天没有股票;
第二天我们可以把第一天的股票升值的2,那么就是 2, 2(赚1);
第三天我们可以把第二天的股票升值为3,变成2 , 3 , 3(赚1) ;
第四天我们把第一天的股票升值为4,(赚2)。
这个过程等价于是把第一天买,第四天卖第一天的,第二天买,第三天卖第二天的。
还有比如 1 , 5 , 2 ,4这种情况
第二天把第一天的股票升值为5之后,第三天的股票比之前的都低,并不能盈利,于是我们不做操作,等到第四天再操作。
这个过程可以用堆来模拟实现。
题意是有n天,你知道接下来每一天股票的价格,你可以选择卖一张股票,买一张或者什么都不做,一开始你没有股票,且没有股票的时候不能卖,问说你n天后能获得的最大利润。
非常玄妙的贪心题。假设我们当前天得到一个股票的价格x,那么我们去之前的日子里找一个价格最低的股票y,如果y<x的话那么我们相当于可以在这天把y股票升值为x且赚到(x-y)元,同时注意这时股票y仍然可以买。
举个例子 1 , 2 , 3 , 4
第一天没有股票;
第二天我们可以把第一天的股票升值的2,那么就是 2, 2(赚1);
第三天我们可以把第二天的股票升值为3,变成2 , 3 , 3(赚1) ;
第四天我们把第一天的股票升值为4,(赚2)。
这个过程等价于是把第一天买,第四天卖第一天的,第二天买,第三天卖第二天的。
还有比如 1 , 5 , 2 ,4这种情况
第二天把第一天的股票升值为5之后,第三天的股票比之前的都低,并不能盈利,于是我们不做操作,等到第四天再操作。
这个过程可以用堆来模拟实现。
#include<bits/stdc++.h> using namespace std; #define LONG long long const LONG INF=0x3f3f3f3f; const LONG MOD= 97 ; const double PI=acos(-1.0); #define clrI(x) memset(x,-1,sizeof(x)) #define clr0(x) memset(x,0,sizeof x) #define clr1(x) memset(x,INF,sizeof x) #d 4000 efine clr2(x) memset(x,-INF,sizeof x) #define EPS 1e-10 #define lowbit(x) (x&-x) struct P { LONG val ; bool operator < (const P x) const & { return val > x.val ; } }p[330100] ; priority_queue<P > que ; int main() { int n ; cin >> n ; while(!que.empty()) que.pop() ; LONG ans =0 ; for(int i =1 ;i <= n ; ++ i) { scanf("%lld",&p[i].val) ; if(que.empty()) que.push(p[i]) ; else { P tmp = que.top() ; if(p[i].val <= tmp.val ) que.push(p[i]) ; else { ans += p[i].val - tmp.val ; que.pop() ; que.push(p[i]) ; que.push(p[i]) ; } } } cout<<ans<<endl ; }
相关文章推荐
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E.Buy Low Sell High
- Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)E - Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- MemSQL Start[c]UP 3.0 - Round 2 and Codeforces Round #437 (Div. 1) D. Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 Div. 2 E. Buy Low Sell High
- Codeforces Round #437 (Div. 2)E. Buy Low Sell High(优先队列)
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- codeforces 867 E Buy Low Sell High(优先队列)
- Codeforces 867 E. Buy Low Sell High (贪心)