Codeforces Round #401(Div. 2)E. Hanoi Factory【贪心+栈】
来源:互联网 发布:编程语言 使用范围 编辑:程序博客网 时间:2024/05/17 07:23
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. Thei-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 ifbj ≤ 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.
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 andhi (1 ≤ ai, bi, hi ≤ 109,bi > ai) — inner radius, outer radius and the height of thei-th ring respectively.
Print one integer — the maximum height of the tower that can be obtained.
31 5 12 6 23 7 3
6
41 2 11 3 34 6 25 7 1
4
In the first sample, the optimal solution is to take all the rings and put them on each other in order3, 2, 1.
In the second sample, one can put the ring 3 on the ring4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
题目大意:
现在一共有N个零件,如果存在:bi>=bj&&bj>ai的两个零件i,j,那么此时我们就可以将零件j放在零件i上。我们现在要组成一个大零件,使得高度最高,问这个最高高度。
思路:
1、首先按照b从大到小排序,如果b相等,那么按照a从大到小排序。将数组排序完成之后,我们考虑贪心寻找这个最大高度。
2、接下来我们直接模拟即可。模拟过程的关键思路就是寻找一条最长的链,所以我们可以加入一个栈,当遇到零件i无法和i-1相连接的时候,我们将第i-1个零件扔掉,判断第i个零件能否再和之前的零件连接,如果还不能,继续扔掉零件,直到能够相连接或者零件扔空了为止(让当前零件作为基出现),让当前零件组装起来。
按照此时排序方案排序结束之后,我们只要能够保证了更多的零件相组装起来就能保证结果尽可能的大了。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<stack>using namespace std;#define ll __int64struct node{ ll a,b,h;}a[100500],now,pre;int cmp(node a,node b){ if(a.b==b.b) { return a.a>b.a; } else return a.b>b.b;}int main(){ int n; while(~scanf("%d",&n)) { stack<node>s; for(int i=1;i<=n;i++) { scanf("%I64d%I64d%I64d",&a[i].a,&a[i].b,&a[i].h); } ll sum=0; ll output=0; sort(a+1,a+1+n,cmp); for(int i=1;i<=n;i++) { if(s.empty()) { sum=a[i].h; s.push(a[i]); } else { now=s.top(); if(now.a<a[i].b) { sum+=a[i].h; s.push(a[i]); } else { output=max(output,sum); while(s.size()>0&&s.top().a>=a[i].b) { pre=s.top(); s.pop(); sum-=pre.h; } sum+=a[i].h; s.push(a[i]); } } } output=max(output,sum); printf("%I64d\n",output); }}
- Codeforces Round #401 (Div. 2) E. Hanoi Factory 贪心+栈
- Codeforces Round #401(Div. 2)E. Hanoi Factory【贪心+栈】
- Codeforces Round #401 (Div. 2) E. Hanoi Factory 贪心+栈
- Codeforces Round #401 (Div. 2):E. Hanoi Factory
- Codeforces Round #401 (Div. 2 )E. Hanoi Factory
- Codeforces Round #401 (Div. 2)E. Hanoi Factory(离散化+线段树)
- 【Codeforces E Hanoi Factory】+ 贪心 + 栈
- Codeforces Round #401 (Div. 2)Hanoi Factory CDQ分治
- codeforces 777E Hanoi Factory (栈+贪心)
- Codeforces 777E Hanoi Factory 贪心+DP
- DP Codeforces Round 401#div2 E.Hanoi Factory
- 【CodeForces 777E】 Hanoi Factory(贪心+sort+模拟)
- codeforces 777E Hanoi Factory
- CodeForces 777E Hanoi Factory
- Codeforces Round #262 (Div. 2)E(贪心+暴搜)
- Codeforces Round #364 (Div. 2) E. Connecting Universities 贪心 + dfs
- Codeforces Round #401 (Div. 2) A~E
- Codeforces Round #401 (Div. 2) (A~E)
- leetcode 520 python
- java安全架构____RSA加密解密
- 正则表达式
- Android AccessibilityService拦截事件及VR眼镜返回按键捕捉
- PAT 1004
- Codeforces Round #401(Div. 2)E. Hanoi Factory【贪心+栈】
- Mathtype与word字号对照
- 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- OSI七层协议模型和TCP/IP四层模型比较
- [Haskell on HackerEarth] Modify Sequence
- SD卡的使用
- 引用妙用
- JAVA 知识
- 【SAP HANA】SAP HANA开篇