Codeforces Round #401 (Div. 2 )E. Hanoi Factory

E. Hanoi Factory

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

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 ai, outer radius bi and height hi. 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 bj ≤ bi.
• Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.
• 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 ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — 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个环的内半径,外半径,高度。要求下外半径>=上外半径。下内半径<上外半径。问最大高度。
题解;我们按照外半径降序优先,内半径降序其次将数组排序(为什么之后说)。然后模拟这个累加过程。如果出现某个零件不符的情况,我们有两种选择。1:放弃该零件,2:不放弃该零件。
对于1:此时的情况是下内半径>上外半径(外半径降序,必满足要求1),但是后续的零件的外半径一定小于等于该零件,也就是下内半径>上外半径一定被满足。这种情况显然是最坏的,不取(这也是数组排序的原因)
对于2:我们就得把之前的零件拆下来使得该零件可以符合。这里很明显需要栈来储存。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100010;
struct node
{
    int in,out,h;
    bool operator < (const node&t)const{
       return out>t.out||(out==t.out&&in>t.in);
    }
}a[N],e;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d",&a[i].in,&a[i].out,&a[i].h);
    }
    sort(a,a+n);
    stack<node>k;
    ll mx,ans;
    mx=ans=0;
    for(int i=0;i<n;i++)
    {
        if(k.empty())
        {
            mx=a[i].h;
            k.push(a[i]);
            continue;
        }
       e=k.top();
        if(e.in<a[i].out)
        {
            mx+=a[i].h;
            k.push(a[i]);
        }
        else
        {
            ans=max(ans,mx);
            while(!k.empty()&&e.in>=a[i].out)
            {
                mx-=e.h;
                k.pop();
                if(!k.empty())
                    e=k.top();
            }
        mx+=a[i].h;
        k.push(a[i]);
        }
    }
    ans=max(ans,mx);
    printf("%I64d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值