(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:Portal传送门
Portal传送门
原题目描述在最下面。
出过很多次:51nodNOIP提高组贪心专题A,牛客寒假多校(不记得那场了),CodeforcesContest865D。
低买高卖问题。
n
(
1
e
5
)
n(1e5)
n(1e5)天共
n
n
n种利率,你每天只能买入或卖出一件商品或者啥都不干,你买得起所有商品,问你最多盈利多少?
ps:FZU2281 Trades是可以买入卖出许多件物品.
Solution:
首先对于当前价格B而言,只要前面有比这个价格低的价格A,那么当前情况A买入B卖出一定盈利。
但是A买入B卖出不一定是最优解,所以为了有后悔药吃,就再push两个B进入优先队列,一个表示卖出,一个表示买入。
每天都卖出,每次累加差值就可以了。累加很多个差分值肯定会得到最优解的。
因为A买入B卖出B买入C卖出 和 A买入C卖出 效果一样
活捉一个分治+dp的大佬:here
AC_Code:
#include<bits/stdc++.h>
#define mme(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int main() {
int tim;
int n;
scanf("%d", &tim);
while(tim--){
scanf("%d",&n);
priority_queue<pair<int,int>>q;
LL ans = 0;
int tot = 0;
for(int i = 0, x; i < n; ++i){
scanf("%d", &x);
q.push(make_pair(-x, 1));//买入
q.push(make_pair(-x, 2));//卖出
LL tmp = x + q.top().first;//卖出x并选择之前最便宜的一个买入
if(q.top().second == 1) tot += 2;//对于实际交易,卖出和买入肯定是对应的
ans += tmp; q.pop();
}
printf("%lld %d\n", ans, tot);
}
return 0;
}