Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏

时间:2023-03-10 07:10:04
Codeforces777E. Hanoi Factory                                                                                            2017-05-04 18:10             42人阅读              评论(0)              收藏
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 aibi 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个盘子的内径外径高度,外径小的不能放大的上面(可以相等),上面的外径必须比下面的内径大,问最多垒多少高度
思路,先按外径从大到小排序,相等按内径排,我们可以清楚的发现,如果一个取不了,那么他之后的都去不了,这时只能去了下面的。因此我们可用栈维护。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define LL long long struct node
{
LL r,R,h;
} p[100005]; bool cmp(node a,node b)
{
if(a.R!=b.R)
return a.R>b.R;
return a.r>b.r;
} stack<node>s; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%lld%lld%lld",&p[i].r,&p[i].R,&p[i].h);
sort(p+1,p+n+1,cmp); while(!s.empty())
s.pop(); LL ans=0;
LL mx=-1;
for(int i=1; i<=n; i++)
{
int flag=0; while(!s.empty())
{
if(s.top().r>=p[i].R)
{
ans-=s.top().h;
s.pop();
}
else
{
ans+=p[i].h;
s.push(p[i]);
flag=1;
mx=max(mx,ans);
break;
}
}
if(flag==0)
{
ans+=p[i].h;
s.push(p[i]);
mx=max(mx,ans);
}
}
printf("%lld\n",mx);
}
return 0;
}