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.ios

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:c++

  • 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.this

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.spa

Output

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

Examples

Inputorm

3
1 5 1
2 6 2
3 7 3

Output排序

6

Inputthree

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

Outputci

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\).get

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;
}
相關文章
相關標籤/搜索