题目传送门

题目大意:

现在一共有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 ≤ 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
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 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.

codeforces-777E Hanoi Factory (栈+贪心)的更多相关文章

  1. Codeforces 777E Hanoi Factory(线段树维护DP)

    题目链接 Hanoi Factory 很容易想到这是一个DAG模型,那么状态转移方程就出来了. 但是排序的时候有个小细节:b相同时看a的值. 因为按照惯例,堆塔的时候肯定是内半径大的在下面. 因为N有 ...

  2. Codeforces 777E - Hanoi Factory(贪心+栈)

    题目链接:http://codeforces.com/problemset/problem/777/E 题意:有n个环给你内环半径.外环半径和高度,叠这些环还要满足以下要求: ①:下面的环的外径要&g ...

  3. CF #401 (Div. 2) E. Hanoi Factory (栈+贪心)

    题意:给你一堆汉诺塔的盘子,设内半径为a,设外半径为b,高度为h,如果bj ≤ bi 同时bj > ai 我们就认为i盘子能落在在j盘子上,问你最高能落多高 思路:一看题意我们就能想到贪心,首先 ...

  4. Codeforces 777E:Hanoi Factory(贪心)

    Of course you have heard the famous task about Hanoi Towers, but did you know that there is a specia ...

  5. Codeforces 777E:Hanoi Factory(贪心+栈)

    http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...

  6. Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  8. Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

随机推荐

  1. appium如何连接模拟器代码实例

    from appium import webdriver def connect(self): self.desired_caps = {} self.desired_caps['platformNa ...

  2. DHCP工作工程

    1.客户端请求IP 客户端发一个DHCP DISCOVEY(包含主机名.mac地址)广播包 2.服务端响应请求 DHCP服务器收到请求后,查看自己的地址池是否有合法的地址.如果有,广播一个DHCP o ...

  3. c++11: thread_local

    thread_local变量是C++ 11新引入的一种存储类型.它会影响变量的存储周期(Storage duration),C++中有4种存储周期: automatic static dynamic ...

  4. mac 彻底卸载Paragon NTFS

    之前安装了paragon NTFS,试用期过了就卸载了,但是每天还是会提示“试用期已到期”,看着很烦. 百度了一下,发现网上的版本可能比较老了,和我的情况不太一样,但道理应该是一样的. 彻底删除方法: ...

  5. Java3D-对象基本变换

    一个球体与三个圆柱体形成一个组合体,在该组合体中,球体的透明度属性是由全透明到不透明之间变换,而且包括:旋转.平移等变换. package com.vfsd.test0621; import java ...

  6. Python 网络爬虫 006 (编程) 解决下载(或叫:爬取)到的网页乱码问题

    解决下载(或叫:爬取)到的网页乱码问题 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 20 ...

  7. Arduino Serial库的使用

    1 Serial.begin() 2 Serial.end() 3 Serial.available() 4 Serial.read() 5 Serial.peek() 6 Serial.flush( ...

  8. 6.Model类

    Basic Concepts      在Model/View结构中,Model提供标准的接口让View和Delegate获得数据.在QT中,标准的接口都被定义在QAbstractItemModel类 ...

  9. Entity Framework Code-First(23):Entity Framework Power Tools

    Entity Framework Power Tools: Entity Framework Power Tools (currently in beta 3) has been released. ...

  10. STL源码剖析--迭代器(转)

    一.为什么需要traits编程技术 前面说了很多关于traits的光荣事迹,但是却一直没有介绍traits究竟是个什么东西,究竟是用来干什么的?traits在英文解释中就是特性,下面将会引入trait ...