【CodeForces 777E】 Hanoi Factory(贪心+sort+模拟)
来源:互联网 发布:java linux ping ip 编辑:程序博客网 时间:2024/06/05 05:12
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
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个环,给出内圈半径、外圈半径、高度,将环套放,要求下外半径 >= 上外半径,下内半径 < 上外半径,问最大高度。
思路:先按外半径从大到小排序,外相同内半径从大到小。开始简单依次模拟,wa了
5 6 8
6 7 1
3 8 9
17 像这组数据。后来用栈模拟就好了。
#include<bits/stdc++.h>#define manx 100005typedef long long LL;using namespace std;struct R{ LL out,in,h;}a[manx];bool cmp(R a,R b){ if(a.out==b.out) return a.in > b.in; return a.out > b.out;}stack<R>s;int main(){ LL n; scanf("%I64d",&n); for (int i=0; i<n; i++) scanf("%I64d%I64d%I64d",&a[i].in,&a[i].out,&a[i].h); sort(a,a+n,cmp); s.push(a[0]); LL cnt=s.top().h,ans=0; for (int i=1; i<n; i++){ if(s.empty()){ cnt=a[i].h; s.push(a[i]); continue; } R t=s.top(); if(a[i].out > t.in){ cnt+=a[i].h; s.push(a[i]); } else{ ans=max(ans,cnt); while(a[i].out <= t.in && !s.empty()){ cnt-=t.h; s.pop(); if(!s.empty()) t=s.top(); } cnt+=a[i].h; s.push(a[i]); } } ans=max(ans,cnt); printf("%I64d\n",ans); return 0;}
- 【CodeForces 777E】 Hanoi Factory(贪心+sort+模拟)
- Codeforces 777E Hanoi Factory 贪心+DP
- codeforces 777E Hanoi Factory (栈+贪心)
- 【Codeforces E Hanoi Factory】+ 贪心 + 栈
- codeforces 777E Hanoi Factory
- CodeForces 777E Hanoi Factory
- codeforces 777E. 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 贪心+栈
- 777E Hanoi Factory
- 【codeforces 777E】Hanoi Factory【动态规划+线段树】
- codeforce 777e Hanoi Factory 叠塔(贪心,从后往前推)
- Codeforces Round #401 (Div. 2 )E. Hanoi Factory
- codeforces 777E. Hanoi Factory (LIS+dp+树状数组/线段树维护区间最大值及单点更新)
- Codeforces Round #401 (Div. 2)E. Hanoi Factory(离散化+线段树)
- DP Codeforces Round 401#div2 E.Hanoi Factory
- Codeforces Round #401 (Div. 2):E. Hanoi Factory
- PAT甲级练习1045. Favorite Color Stripe (30)
- 设计模式C++实现-简单工厂模式、工厂方法模式、抽象工厂模式
- Node.js批量刷CSDN博客访问量
- 【Javaweb】笔面试题 ---(1)
- Erlang编程实践启示
- 【CodeForces 777E】 Hanoi Factory(贪心+sort+模拟)
- 关于WIN10 VS2015下调试DirectX11 DEMO错误的解决方案
- 详细的Dagger2+MVP融合,一行一行分析,一点一点进步,之三
- 215. Kth Largest Element in an Array
- 软件测试面试题
- 九度OJ(1153题)
- hdu2307
- Java现实WebSocket
- css布局常用知识点,及注意事项(一)