放一下我注释里面的模拟
1 2 10 11
到10的时候 queue top是2
此时map(2)=1 此时10直接与1交换,以二为跳板
到11的时候 queue top是2
此时map(2)=0 此时11直接与2交换,无跳板
1 2 2 10 11 12
到10的时候 queue top是2
此时map(2)=1 此时10直接与1交换,以二为跳板
到11的时候 queue top是2
此时map(2)=0 此时11直接与2交换,无跳板
到12的时候 queue top是2
此时map(2)=0 此时12直接与2交换,无跳板
(为啥这个重复的2没有map(2)=1? 因为没有执行if 啊 2==queue top 无法进入if)
1 2 2 10 11 12 1
同上三步
然后1太便宜了没有进入if
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
map<int,int> m;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,x,cnt=0;
long long ans=0;
scanf("%d",&n);
while(!q.empty()) q.pop();
m.clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(!q.empty()&&x>q.top())
{
int in=q.top();
q.pop();
ans+=x-in;
cnt++;
q.push(x);//跳板 设此x为ai 那么下一个x>ai 借助ai与ai之前的数直接交换 所以下面交易次数要减一(减少了中间商ai这个跳板)
if(m[in])//m[in]的值大于0 in是作为跳板使用的 交易数减一
{
cnt--;
m[in]--;
}
m[x]++;
}
q.push(x);//外层 如果上面if没执行 那么就是q.empty or x小于等于队列最小 这么便宜肯定买啦 if执行了那就说明买了x 也要push进去
}
printf("%lld %d\n",ans,2*cnt);
}
}