codeforces-777E Hanoi Factory (栈+贪心)
题目大意:
现在一共有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;
}
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.
codeforces-777E Hanoi Factory (栈+贪心)的更多相关文章
- Codeforces 777E Hanoi Factory(线段树维护DP)
题目链接 Hanoi Factory 很容易想到这是一个DAG模型,那么状态转移方程就出来了. 但是排序的时候有个小细节:b相同时看a的值. 因为按照惯例,堆塔的时候肯定是内半径大的在下面. 因为N有 ...
- Codeforces 777E - Hanoi Factory(贪心+栈)
题目链接:http://codeforces.com/problemset/problem/777/E 题意:有n个环给你内环半径.外环半径和高度,叠这些环还要满足以下要求: ①:下面的环的外径要&g ...
- CF #401 (Div. 2) E. Hanoi Factory (栈+贪心)
题意:给你一堆汉诺塔的盘子,设内半径为a,设外半径为b,高度为h,如果bj ≤ bi 同时bj > ai 我们就认为i盘子能落在在j盘子上,问你最高能落多高 思路:一看题意我们就能想到贪心,首先 ...
- Codeforces 777E:Hanoi Factory(贪心)
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a specia ...
- Codeforces 777E:Hanoi Factory(贪心+栈)
http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...
- 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 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
随机推荐
- windows 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- Codeforces 1108F (MST Unification) (树上倍增 or 改进 kruksal)
题意:给你一张n个节点和m条边的无向连通图, 你可以执行很多次操作,对某一条边的权值+1(对于每条边,可以不加,可以无限次加),问至少进行多少次操作,可以使这张图的最小生成树变得唯一,并且最小生成树的 ...
- session,cookie总结
不同的域名生成的session_id是不一样的,(就算是相同的主域,例如:www.test.com, blog.test.com 都不一样); 相同的主域,不同的二级域名,例如www和blog都是不共 ...
- linux sdcv命令
一.简介 sdcv全称为stardict console version,是终端下的词典. 二.安装 1)安装sdcv yum install -y sdcv 2)安装字典 http://www. ...
- 1、awk打开多个文件的方法
转载:http://www.cnblogs.com/Berryxiong/p/6209324.html 1.当awk读取的文件只有两个的时候,比较常用的有三种方法(1)awk 'NR==FNR{... ...
- zookeeper集群安装(转)
转载地址:http://www.blogjava.net/hello-yun/archive/2012/05/03/377250.html 本方法,本人亲自试验,可以成功. ZooKeeper是一个分 ...
- pentaho和spark-sql对接
pentaho可以和hive做对接,所以和spark-sql做对接也是妥妥的.结果让人很失望了啊,我配置了很久都搞不定,最后脑袋突然灵机一动打通了. 1:替换pentaho自带的hive驱动. 路径 ...
- 数据结构 lucky_ming幸运的小明
问题描述 在快速排序过程中, 每次会找一个划分值, 将小于划分值的放到其左边, 大于划分值的放右边, 然后再依次递归左右两边, 对子序列进行同样的操作, 直到子序列为空则停止操作.最后就得到了有序的序 ...
- UVa 1204 Fun Game (状压DP)
题意:有一些小孩(至少两个)围成一圈,有 n 轮游戏,每一轮从某个小孩开始往左或者往右伟手帕,拿到手帕写上自己的性别(B,G),然后以后相同方向给下一个. 然后在某个小孩结束,给出 n 轮手帕上的序列 ...
- springcloud 定义切面实现对请求操作记录日志,方便后面分析接口详情
package com.idoipo.infras.gateway.open.config; import com.alibaba.fastjson.JSON; import com.alibaba. ...