码迷,mamicode.com
首页 > 其他好文 > 详细

[bzoj 1208]STL水过

时间:2017-09-25 23:00:43      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:结构   return   iter   ref   get   数据   ret   names   直接   

题目链接: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;
}

 

[bzoj 1208]STL水过

标签:结构   return   iter   ref   get   数据   ret   names   直接   

原文地址:http://www.cnblogs.com/acmsong/p/7594133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!