zoukankan      html  css  js  c++  java
  • 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
    ",tol,ans);
        }
        return 0;
    }
  • 相关阅读:
    C# datatable 与 xml文件之间的转换
    服务器能访问共享,但是ping不通解决方案
    Oracle报错,ORA-28001: 口令已经失效
    解决苹果虚拟机安装过程中出现的问题
    安卓WiFi基本使用
    自定义Adapter
    listview学习之simpleadapter详细介绍
    listview学习之arrayadapter初探
    listview学习之初探adapter
    专注,追求卓越!
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7436050.html
Copyright © 2011-2022 走看看