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;
    }
  • 相关阅读:
    oracle-DML-2
    Oracle--DML
    jquery 调用asp.net后台代码
    hdu--1505--稍微特别的子矩阵求和<修改第一次发表的错误>--<增加stack写法>
    hdu--2159--二维费用背包<一维错误解法>
    hdu--2571--dp
    hdu--1231&&1003--dp
    hdu--1251--字典树
    hdu--1075--字典树||map
    hdu--1421--dp&&滚动数组
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7436050.html
Copyright © 2011-2022 走看看