题目传送门

题目大意:

现在一共有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. TypeError: 'append' called on an object that does not implement interface FormData 解决方法

    使用ajax提交form表单时,$("formId").serialize()不能提交type="file"类型的input,这个时候可以选择使用FormDat ...

  2. [Python Study Notes]折线图绘制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  3. Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation

    题意:给n!个n的排列,按字典序从小到大连成一条序列,例如3的情况为:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],问其中长度为n,且和为sum=n*(n+1) ...

  4. 02 mybatis环境搭建 【spring + mybatis】

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  5. Spring 框架学习 有用

    1.1.1 spring的优势 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.用户也不必再为单例模式类.属性文件 ...

  6. Android studio中ShredPreferences 的简单使用

    ShredPreferences是一个轻量级的数据存储方式,只能存取字符串了整型数据这一类的数据,如果要存储复杂的数据这个存储方式就不再适用 首先是要新建一个ShredPreferences的对象 p ...

  7. EZOJ #80

    传送门 分析 经典的树型DP 我们记录dp[i][0/1]表示i的子树中到i的长度分别为偶数和奇数的长度和 dp2[i][0/1]则表示不在i的子树中的点到i的长度分别为偶数和奇数的长度和 然后根据边 ...

  8. hihocoder1513 小Hi的烦恼

    传送门 分析 论bitset的妙用......我们利用桶排将输入的数据排序,之后分别考虑5维,a[i][j]表示考虑第i个人第j维的情况下于其它人的大小关系.最后将5维的信息并起来求1的个数即可 代码 ...

  9. PCL基础3.2-如何编写新的PCL类

    1.文件名为mainBilateralFilter.cpp的文件内容如下 #include <pcl/point_types.h> #include <pcl/io/pcd_io.h ...

  10. Linq 和 Lambda 查询中按照多个值进行分组GroupBy

    创建要查询的对象: class Employee { public int ID { get;set; } public string FName { get; set; } public int A ...