[bzoj 1208]STL水过

本文介绍了一种利用C++ STL中的set数据结构解决特定匹配问题的方法。通过使用set的lower_bound函数,可以有效地找到最接近的目标值并进行匹配操作。这种方法避免了手动实现复杂的数据结构,简化了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208

看网上的题解都用的手写数据结构……然而直接用set的lower_bound就水过去了……

#include<bits/stdc++.h>
using namespace std;

const int md=1000000;
set<int> pet,people;

int main()
{
    int n;
    scanf("%d",&n);
    int ans=0;
    while (n--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if (op==0)
        {
            if (people.empty())
            {
                pet.insert(x);
            }
            else
            {
                set<int>::iterator it = people.lower_bound(x);
                if (it==people.begin())
                {
                    ans=(ans+abs(*it-x))%md;
                }
                else if (it==people.end())
                {
                    --it;
                    ans=(ans+abs(*it-x))%md;
                }
                else
                {
                    set<int>::iterator it2=--it;
                    ++it;
                    if (abs(*it2-x)<=abs(*it-x)) --it;
                    ans=(ans+abs(*it-x))%md;
                }
                people.erase(it);
            }
        }
        else
        {
            if (pet.empty())
            {
                people.insert(x);
            }
            else
            {
                set<int>::iterator it = pet.lower_bound(x);
                if (it==pet.begin())
                {
                    ans=(ans+abs(*it-x))%md;
                }
                else if (it==pet.end())
                {
                    --it;
                    ans=(ans+abs(*it-x))%md;
                }
                else
                {
                    set<int>::iterator it2=--it;
                    ++it;
                    if (abs(*it2-x)<=abs(*it-x)) --it;
                    ans=(ans+abs(*it-x))%md;
                }
                pet.erase(it);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/acmsong/p/7594133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值