codeforces-777E Hanoi Factory (棧+貪心)

題目傳送門

題目大意:

現在一共有N個零件,如果存在:bi>=bj&&bj>ai的兩個零件i,j,那麼此時我們就可以將零件j放在零件i上。我們現在要組成一個大零件,使得高度最高,問這個最高高度。

思路:看了題解,先將木塊按b從大到小排序,相同的再按a從大到小排序。(這樣排序後滿足兩點性質,第一,如果第i塊不能放在已經放好的木臺上,說明此時的b小於木臺最上面的a,而這個序列後續所有的b都比當前的b小,也就是後面所有的木塊都不能放在現在的木臺上,要想繼續放,必須把當前木塊最上面那塊拿出來)。這樣就滿足了先進後出的原則,用棧來模擬,用sum表示當前木臺的高度,ans表示歷史最大的高度。比一下就好了。

代碼。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
const int maxn=100010;
struct dian{
	ll a,b,h;
}s[maxn];
int n;
bool cmp(dian x,dian y){//相同的b  應該把a小一點的放上面 
	if(x.b-y.b)return x.b>y.b;
	return x.a>y.a;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].h);
	}
	sort(s+1,s+1+n,cmp);
	stack<dian>q;
	ll ans,sum;
	ans=sum=s[1].h;
	q.push(s[1]);
	for(int i=2;i<=n;i++){
		while(!q.empty()&&s[i].b<=q.top().a){//如果不能放在當前的這個上面,說明此時的b小於下面的a,那後面的所有b肯定都會小於下面的a 
			sum-=q.top().h;                  //所以如果想繼續放  就必須把下面那一塊拿出來 
			q.pop();
		}
		sum+=s[i].h;
		q.push(s[i]);
		ans=max(ans,sum);
	}
	cout<<ans<<endl;
}

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 ≤ 109bi > 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
Copy
3
1 5 1
2 6 2
3 7 3
output
Copy
6
input
Copy
4
1 2 1
1 3 3
4 6 2
5 7 1
output
Copy
4
Note

In the first sample, the optimal solution is to take all the rings and put them on each other in order 321.

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.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章