hdu 6180

题意:略

思路:是简单的贪心,但是,一直不知道怎么才能更好地实现,然后发现了multiset这种神奇的东西,然后发现一些随时查找随时删除的时候很好用的工具,STL的map、multimap、set、multiset都有三个比较特殊的函数,lower_bound、upper_bound、equal_range。

原型如下: 

iterator lower_bound (const value_type& val) const;

iterator upper_bound (const value_type& val) const;

pair<iterator,iterator> equal_range (const value_type& val) const;

这样就可以在log时间内完成这道题需要的查找了~

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
const int maxn=1e5+10;
typedef long long LL;

struct node
{
    int l,r;
    node(int _l=0,int _r=0):l(_l),r(_r){}
    bool operator < (const node &t)const {return l<t.l;}
}p[maxn];
multiset<int>st;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int T,n,l,r;
    scanf("%d",&T);
    while(T--)
    {
        st.clear();

        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&l,&r);
            p[i]=node(l,r);
        }
        sort(p,p+n);
        LL tol=0,ans=0;
        multiset<int>::iterator it;
        for(int i=0;i<n;i++)
        {
            it=st.upper_bound(p[i].l);
            if(it==st.begin())
            {
                ans+=p[i].r-p[i].l;
                st.insert(p[i].r);
                tol++;
            }
            else
            {
                it--;
                ans+=p[i].r-(*it);
                st.erase(it);
                st.insert(p[i].r);
            }
        }
        printf("%lld %lld\n",tol,ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/MeowMeowMeow/p/7436050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值