版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/82054463
题意:
给定n天,每天有个价格,可以买一个物品,可以把手中的物品卖掉
思路:
codeforces 一个题跟这个题很像,贪心+优先队列:https://blog.csdn.net/xiang_6/article/details/78156338
看完那个题,我们只需要解决最少买卖次数就好了;
手动模拟队列的实现过程会发现,整个问题就是一些上升序列对价值的转移,上篇中我们将两个新的比较大的值放入队列后,一个表示它本身,可能会作为后面的改变对,另一个表示代表前面它代替的价值的转移, 为了最终买卖的次数尽量少,所以尽量让他们较多的转移,也就是新的值x替换队列首的数的时候,前面的数先选用过的进行转移
#include<iostream>
#include <cstdio>
#include<algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
const int maxn = 1e5 + 7;
const ll mod = 1e9 + 7;
map<int,int> vis;
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
priority_queue<int, vector<int>, greater<int> > qu;
while(!qu.empty()) qu.pop();
ll ans = 0, cnt = 0;
vis.clear();
for(int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
if(qu.empty() || qu.top() >= x) {
qu.push(x);
}
else {
cnt++;
int t = qu.top(); qu.pop();
ans += (x-t);
if(vis[t] > 0) {
cnt--;
vis[t]--;
}
qu.push(x); qu.push(x);
vis[x]++;
}
}
printf("%lld %lld\n", ans, cnt*2);
}
return 0;
}