您的位置:首页 > 其它

codeforce 777e Hanoi Factory 叠塔(贪心,从后往前推)

2017-02-25 14:12 597 查看
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个棱台,顶面为ai,底面为bi,高为hi, 求棱台能叠多高,  棱台能叠起来的要求是,  b1<=b2  &&  b1>a1  

排序b从小到大,在b相同排序a从小到大,使其满足如果i-1不能取,那么i-2也不能取, 

这样从后往前推入栈,如果不满足推出栈直到满足再推入栈,每次推入的答案记录最大值即可。 

至于为什么b相同,a要从小到大排,因为尽量小的要在上,  a1<b1 , 说明底面相同的一定能叠,则让最小的放在上层。

/*
E. Hanoi Factory
时间: 2017/02/25
题意:叠塔,但要满足a(i+1) < bi <= b(i+1)
题解:
标程是线段树+LIS
群里说贪心也能做,排序b从小到大,在b相同排序a从小到大,使其满足如果i-1不能取,那么i-2也不能取,
这样从后往前推入栈,如果不满足推出栈直到满足再推入栈,每次推入的答案记录最大值即可。
*/

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define LL long long
#define N 100010
#define INF 0x3f3f3f3f

struct point
{
int x,y,h;
} a
;
bool cmp(point a,point b)
{
if(a.y != b.y) return a.y < b.y;
return a.x < b.x;
}
int dp
;
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 0; i < n; i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].h);
sort(a,a+n,cmp);
stack<point> p;
LL ans = 0;
LL mx = 0;
for(int i = n-1; i >= 0; i--)
{
while(1)
{
if(p.empty())
break;
point f = p.top();
if(f.y >= a[i].y && f.x < a[i].y)
break;
ans -= f.h;
p.pop();
}
p.push(a[i]);
ans += a[i].h;
mx = max(mx,ans);
}
printf("%I64d\n",mx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: