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;
    }
  • 相关阅读:
    ajax的优缺点
    Service层事务不能回滚的解决方式
    java---springMVC与strutsMVC的区别
    java----监听器的作用
    ActionContextCleanUp
    Struts2请求处理流程及源码分析
    Struts2的拦截器是如何使用AOP工作的
    SSH三大框架的知识题
    java--Struts中请求的过程
    java----Servlet的生命周期
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7436050.html
Copyright © 2011-2022 走看看