zoukankan      html  css  js  c++  java
  • Codeforces 777E:Hanoi Factory(贪心)

    Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.

    There are (n) rings in factory's stock. The (i)-th ring has inner radius (a_i), outer radius (b_i) and height (h_i). The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:

    • Outer radiuses form a non-increasing sequence, i.e. one can put the (j)-th ring on the (i)-th ring only if (b_j ≤ b_i).
    • Rings should not fall one into the the other. That means one can place ring (j) on the ring (i) only if (b_j > a_i).
    • The total height of all rings used should be maximum possible.

    Input

    The first line of the input contains a single integer (n (1 ≤ n ≤ 100 000)) — the number of rings in factory's stock.

    The (i)-th of the next (n) lines contains three integers (a_i, b_i) and (h_i (1 ≤ a_i, b_i, h_i ≤ 10^9, b_i > a_i)) — inner radius, outer radius and the height of the (i)-th ring respectively.

    Output

    Print one integer — the maximum height of the tower that can be obtained.

    Examples

    Input

    3
    1 5 1
    2 6 2
    3 7 3
    

    Output

    6
    

    Input

    4
    1 2 1
    1 3 3
    4 6 2
    5 7 1
    

    Output

    4
    

    Note

    In the first sample, the optimal solution is to take all the rings and put them on each other in order (3, 2, 1).

    In the second sample, one can put the ring (3) on the ring (4) and get the tower of height (3), or put the ring (1) on the ring (2) and get the tower of height (4).

    题意

    (n)个空心圆柱体,第(i)个圆柱体的内径、外径、高分别为:(a_i,b_i,h_i)。将这些圆柱体堆起来,要求:从上到下,外径非递减,并且上面的外径小于下面的内径。问最高能堆多高

    思路

    贪心

    先按外径从大到小排序,如果外径相同,按内径从大到小排序,如果外径和内径均相同,按高度从高到低排序。这样可以满足如果第(i)个不能取,那么第(i+1)个一定不能取,而且已经取过的高度最高

    然后用一个栈来维护。将每次可以往上放的圆柱加在栈顶,如果不可以放,就将栈顶元素弹出,维护栈内圆柱的总高度,取最大值

    代码

    #include <bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    #define ms(a,b) memset(a,b,sizeof(a))
    const int inf=0x3f3f3f3f;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int maxn=1e6+10;
    const int mod=1e9+7;
    const int maxm=1e3+10;
    using namespace std;
    struct wzy
    {
        int a,b,h;
    }p[maxn];
    bool cmp(wzy u,wzy v)
    {
        if(u.b==v.b)
        {
            if(u.a==v.a)
                return u.h>v.h;
            return u.a>v.a;
        }
        return u.b>v.b;
    }
    int main(int argc, char const *argv[])
    {
        #ifndef ONLINE_JUDGE
            freopen("/home/wzy/in.txt", "r", stdin);
            freopen("/home/wzy/out.txt", "w", stdout);
            srand((unsigned int)time(NULL));
        #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>p[i].a>>p[i].b>>p[i].h;
        sort(p+1,p+1+n,cmp);
        ll ans=1LL*p[1].h;
        ll sum=1LL*p[1].h;
        stack<wzy>st;
        st.push(p[1]);
        for(int i=2;i<=n;i++)    
        {
            while(!st.empty()&&(st.top().a>=p[i].b||st.top().b<p[i].b))
            {
                sum-=1LL*st.top().h;
                st.pop();
            }
            sum+=1LL*p[i].h;
            st.push(p[i]);
            ans=max(ans,sum);
        }
        cout<<ans<<endl;
        #ifndef ONLINE_JUDGE
            cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
        #endif
        return 0;
    }
    
  • 相关阅读:
    数据结构与算法 -- 动态规划算法
    数据结构与算法 -- 回溯算法
    数据结构与算法 -- 图
    数据结构与算法无用随笔
    算法集锦
    基于Zookeeper实现多进程分布式锁
    自己动手写线程池
    maven配置国内阿里云镜像
    自己动手写java锁
    使用jconsole监控JVM内存
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11571769.html
Copyright © 2011-2022 走看看