• 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;
    }
    
  • 相关阅读:
    golang模拟动态高优先权优先调度算法
    【2019腾讯暑期实习生正式批笔试1,2】
    golang优先队列
    git常见操作
    小L的试卷
    Unable to connect to the Redgate Client Service. Sql Prompt 报错不能用解决
    未能加载文件或程序集“Microsoft.VisualStudio.Enterprise.AspNetHelper, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。
    LumiSoft 邮件操作删除(无法删除解决方法)
    .net MVC 项目中 上传或者处理进度获取方案
    C# mvc Request 请求过长报404错误的解决思路分析
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11571769.html
Copyright © 2020-2023  润新知