没过样例,但是AC,能否解释一下
查看原帖
没过样例,但是AC,能否解释一下
291509
Cupricion楼主2024/8/1 11:19
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define int long long
tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> pt,hm;
unordered_map<int,int> samh;
unordered_map<int,int> samp;
int myabs(int A)
{
    return A>=0? A:0-A;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,cntp=2,cnth=2,last=0,ans=0;
    pt.insert(make_pair(-2147483648,0));
    pt.insert(make_pair(2147483647,1));
    hm.insert(make_pair(-2147483648,0));
    hm.insert(make_pair(2147483647,1));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int op,x;
        cin>>op>>x;
        if(op==0)
        {
            if(hm.size()>2)
            {
                if(samh[x])
                {
                    samh[x]--;
                    auto tmp=hm.lower_bound(make_pair(x,0));
                    hm.erase(tmp);
                    continue;
                }
                int a=hm.find_by_order(hm.order_of_key(make_pair(x,0))-1)->first,b=hm.find_by_order(hm.order_of_key(make_pair(x,2147483647)))->first;
                if(myabs(a-x)>myabs(b-x))
                {
                    auto tmp=hm.lower_bound(make_pair(b,0));
                    hm.erase(tmp);
                    ans+=myabs(b-x);
                }
                else
                {
                    auto tmp=hm.lower_bound(make_pair(a,0));
                    hm.erase(tmp);
                    ans+=myabs(a-x);
                }
            }
            else
            {
                pt.insert(make_pair(x,cntp++));
                samp[x]++;
            }
        }
        else
        {
            if(pt.size()>2)
            {
                if(samp[x])
                {
                    samp[x]--;
                    auto tmp=pt.lower_bound(make_pair(x,0));
                    pt.erase(tmp);
                    continue;
                }
                int a=pt.find_by_order(pt.order_of_key(make_pair(x,0))-1)->first,b=pt.find_by_order(pt.order_of_key(make_pair(x,2147483647)))->first;
                //cout<<a<<' '<<b<<' '<<endl;
                if(myabs(a-x)>myabs(b-x))
                {
                    auto tmp=pt.lower_bound(make_pair(b,0));
                    pt.erase(tmp);
                    ans+=myabs(b-x);
                }
                else
                {
                    auto tmp=pt.lower_bound(make_pair(a,0));
                    pt.erase(tmp);
                    ans+=myabs(a-x);
                }
            }
            else
            {
                hm.insert(make_pair(x,cnth++));
                samh[x]++;
            }
        }
      //  cout<<ans<<endl;
        ans=ans%1000000;
    }
    cout<<ans%1000000;
	return 0;
}
2024/8/1 11:19
加载中...