Codeforces Round #401 (Div. 2):E. Hanoi Factory
来源:互联网 发布:电力系统短路计算软件 编辑:程序博客网 时间:2024/05/17 00:02
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.
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.
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
http://codeforces.com/contest/777/problem/E
比赛的时候码到一半时间到了,但思路是没错的,用栈模拟一下就好了,所有圆环按大R排序,相同则按小r排序
(都是从大到小),如果一个圆环放不上去,那么后面的圆环会全部都放不上去,这个时候要将汉诺塔顶端的圆环
一个一个撤下,直到能放为止继续放,中间最大的高度即是答案
#include<stdio.h>#include<stack>#include<algorithm>#define LL long longusing namespace std;typedef struct Res{int h;int R, r;bool operator < (const Res &b) const{if(R>b.R || R==b.R && r>b.r)return 1;return 0;}}Res;Res s[100005];stack<Res> st;int main(void){int i, n;LL sum, ans;scanf("%d", &n);for(i=1;i<=n;i++)scanf("%d%d%d", &s[i].r, &s[i].R, &s[i].h);sort(s+1, s+n+1);sum = ans = s[1].h;st.push(s[1]);for(i=2;i<=n;i++){while(st.empty()==0 && s[i].R<=st.top().r){sum -= st.top().h;st.pop();}st.push(s[i]);sum += s[i].h;ans = max(ans, sum);}printf("%I64d\n", ans);return 0;}
- 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 Round #401 (Div. 2)Hanoi Factory CDQ分治
- DP Codeforces Round 401#div2 E.Hanoi Factory
- codeforces 777E Hanoi Factory
- CodeForces 777E Hanoi Factory
- Codeforces Round #401 (Div. 2) A~E
- Codeforces Round #401 (Div. 2) (A~E)
- Codeforces Round #401 (Div. 2) A---E
- 【Codeforces E Hanoi Factory】+ 贪心 + 栈
- Codeforces 777E Hanoi Factory 贪心+DP
- Codeforces Round #276 (Div. 2) Factory
- Codeforces Round #230 (Div. 2) D - Tower of Hanoi
- Codeforces Round #401 (Div. 2)(C,D,E)
- Android笔记之xml(SAX)解析
- 34、编译、链接和运行时的常见错误
- Head First设计模式学习笔记-------(2)观察者模式
- 框架之SpringMVC
- 机器学习历史简介
- Codeforces Round #401 (Div. 2):E. Hanoi Factory
- Android 之低版本高版本实现沉浸式状态栏
- 73. Set Matrix Zeroes
- 利用VPS配合Shadowsocks 科学上网
- Java基础(流程控制语句、方法、数组)
- 【HDU-OJ】-6016-Count the Sheep(二分图,vector)
- use vue vuex vue-router, not use webpack
- 51nod 贪心入门之四 独木舟问题
- JAVA 加密技术