Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏
1 second
256 megabytes
standard input
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.
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.
3
1 5 1
2 6 2
3 7 3
6
4
1 2 1
1 3 3
4 6 2
5 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.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define LL long long struct node
{
LL r,R,h;
} p[100005]; bool cmp(node a,node b)
{
if(a.R!=b.R)
return a.R>b.R;
return a.r>b.r;
} stack<node>s; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%lld%lld%lld",&p[i].r,&p[i].R,&p[i].h);
sort(p+1,p+n+1,cmp); while(!s.empty())
s.pop(); LL ans=0;
LL mx=-1;
for(int i=1; i<=n; i++)
{
int flag=0; while(!s.empty())
{
if(s.top().r>=p[i].R)
{
ans-=s.top().h;
s.pop();
}
else
{
ans+=p[i].h;
s.push(p[i]);
flag=1;
mx=max(mx,ans);
break;
}
}
if(flag==0)
{
ans+=p[i].h;
s.push(p[i]);
mx=max(mx,ans);
}
}
printf("%lld\n",mx);
}
return 0;
}
Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏的更多相关文章
- const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏
const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. 事实上这个概念谁都有只是三种声明方式非常相似很容易记混. Bjarne在他的 ...
- HDU2976 Dropping tests 2017-05-11 18:10 39人阅读 评论(0) 收藏
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12187 Accepted: 4257 D ...
- 团体程序设计天梯赛L2-003 月饼 2017-03-22 18:17 42人阅读 评论(0) 收藏
L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...
- c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏
c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...
- Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏
全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...
- HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏
Automatic Judge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏
Doubles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19954 Accepted: 11536 Descrip ...
- 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏
制作运行库 由于ubuntu的Qt运行库在/usr/local/Trolltech/Qt-4.7.3/下,由makefile可以看到引用运行库是 INCPATH = -I/usr//mkspecs/d ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
随机推荐
- JavaScript eval_r() 函数
定义和用法 eval_r() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval_r(string) 参数 描述 string 必需.要计算的字符串,其中含有要计算的 ...
- SQL语句查询年龄分段分组查询
此情况用于数据库中没有“年龄”这个字段,只有“出生日期”这个字段.先计算出“年龄”,在分组查询. 1.SELECT *, ROUND(DATEDIFF(CURDATE(), popBirthday)/ ...
- js 触发 change 事件
首先,请各位包涵,我本人对 JS 不是很熟,不知道"触发change事件"和"触发onchange事件"哪个更加合适.有园友知道的麻烦指出,先行谢过. 起因是这 ...
- 84. Largest Rectangle in Histogram (Array, Stack; DP)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 8.String to Integer (atoi) (INT; Overflow)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 关于"undefined reference"错误
这个错误换句话说: 链接的时候找不到实现的文件(谨记从这个入手!). 可能导致的原因有: 1. 没有链接库文件,包括静态库或动态库. 2. 链接文件的顺序问题,先后依赖问题,把被依赖的放后面. 3. ...
- MySQL5.7.10配置和使用
前两天搞mybatis和springmvc的结合,搞了半天项目就是跑不起来,由于都没有接触过,也不知道原因出在哪里.今天想彻底学习下mybatis,于是就想使用MySQL.从官网上下了,MySQL-5 ...
- Python 安装路径, dist-packages 和 site-packages 区别
Stack Overflow's answer 译: dist-packages is a Debian-specific convention that is also present in its ...
- Codeforces 670D1. Magic Powder - 1 暴力
D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: stand ...
- UDP接收数据
http://blog.csdn.net/xingzheouc/article/details/49946191 http://blog.csdn.net/robertkun/article/deta ...