简单的模拟,我是用multipset 维护左右集合
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
multiset<int> Left,Right;
int main()
{
int t,now,to,counter,l,n,choice,x,manys=1;
ll ans;
scanf("%d",&t);
while(t--)
{
counter=0;
ans=0;
now=0;
Left.clear(),Right.clear();
scanf("%d%d",&l,&n);
for(int i=0;i<n;i++)
{
scanf("%d",&choice);
if(choice==0)
{
scanf("%d",&x);
if(x==now)
counter++;
else if(x<now)
Left.insert(-x);
else
Right.insert(x);
}
else
{
if(counter)
counter--;
else
{
if(Left.empty()&&Right.empty())
continue;
else if(Left.empty())
{
int temp=*Right.begin();
Right.erase(Right.begin());
ans+=(temp-now);
now=temp;
to=1;
}
else if(Right.empty())
{
int temp=-*Left.begin();
Left.erase(Left.begin());
ans+=(now-temp);
now=temp;
to=0;
}
else
{
int temp1=-*Left.begin();
int temp2=*Right.begin();
if(now-temp1<temp2-now)
{
ans+=now-temp1;
Left.erase(Left.begin());
now=temp1;
to=0;
}
else if(now-temp1>temp2-now)
{
ans+=temp2-now;
Right.erase(Right.begin());
now=temp2;
to=1;
}
else
{
if(to==1)
{
ans+=temp2-now;
Right.erase(Right.begin());
now=temp2;
}
else
{
ans+=now-temp1;
Left.erase(Left.begin());
now=temp1;
}
}
}
}
}
}
printf("Case %d: %d\n",manys++,ans);
}
return 0;
}