http://codeforces.com/problemset/problem/777/E

题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且bj > ai,这样j才可以放在i的上面,问最大能达到的高度是多少。

思路:一开始用数组dp错了,主要是推错转移方程。用不到之前的信息了。如果要利用之前的信息,其实是可以用栈来维护的。先按照外半径从大到小,外半径相同内半径从大到小排序,这样能保证如果前面符合,后面放上去能使高度最大。

 #include <bits/stdc++.h>
using namespace std;
#define N 100010
typedef long long LL;
struct node {
LL a, b, h;
} p[N];
stack<node> sta; bool cmp(const node &a, const node &b) {
if(a.b == b.b) return a.a < b.a;
return a.b > b.b;
} int main() {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lld%lld%lld", &p[i].a, &p[i].b, &p[i].h);
sort(p + , p + + n, cmp);
LL ans = p[].h, cnt = p[].h;
sta.push(p[]);
for(int i = ; i <= n; i++) {
if(sta.empty()) {
cnt += p[i].h; sta.push(p[i]);
continue;
}
node top = sta.top();
if(top.a < p[i].b) {
cnt += p[i].h;
sta.push(p[i]);
} else {
ans = max(ans, cnt);
while(top.a >= p[i].b) {
cnt -= top.h;
sta.pop();
if(sta.empty()) break;
top = sta.top();
}
sta.push(p[i]);
cnt += p[i].h;
}
ans = max(ans, cnt);
}
printf("%lld\n", ans);
return ;
}

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

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

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

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

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

  3. codeforces-777E Hanoi Factory (栈+贪心)

    题目传送门 题目大意: 现在一共有N个零件,如果存在:bi>=bj&&bj>ai的两个零件i,j,那么此时我们就可以将零件j放在零件i上.我们现在要组成一个大零件,使得高度 ...

  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. 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 ...

  6. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  7. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

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

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

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

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

随机推荐

  1. C#同步SQL Server数据库Schema

    C#同步SQL Server数据库Schema 1. 先写一个sql加工类: using System; using System.Collections.Generic; using System. ...

  2. WPF DatePicker默认显示当前日期,格式化为年月日

    原文:WPF DatePicker默认显示当前日期 WPF的日历选择控件默认为当前日期,共有两种方法,一种静态,一种动态. 静态的当然写在DatePicker控件的属性里了,动态的写在对应的cs文件里 ...

  3. C# IDisposable接口的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. javascript高程笔记-------第四章 变量、作用域和内存问题

    首先JavaScript中的变量分为基本类型和引用类型. 基本类型就是保存在栈内存中的简单数据段,而引用类型指的是那些保存在堆内存中的对象. 1.参数传递 javascript中所有参数的传递都是值传 ...

  5. C# 不重启程序修改并保存配置文件(appSettings节点)

    原文:C# 不重启程序修改并保存配置文件(appSettings节点) private static void UpdateAppConfig(string newKey, string newVal ...

  6. 获取控件中应用的模版的内部的控件,使用LoadContent()方法获取模版跟节点

    treeview获取内部控件元素 Button btnAdd = ((tvks.HeaderTemplate as DataTemplate).LoadContent() as StackPanel) ...

  7. UWP使用AppService向另一个UWP客户端应用程序提供服务

    原文:UWP使用AppService向另一个UWP客户端应用程序提供服务 在上篇里,我使用的是寄宿在WPF上的WCF进行两个程序间的通信,在解决问题的同时,我的同事也在思考能否使用UWP来做这件事.于 ...

  8. UWP-标题栏”后退“按钮

    原文:UWP-标题栏"后退"按钮 标题栏”后退“按钮,系统级导航 应用必须启用所有硬件和软件系统后退按钮的后退导航.执行此操作的方法是注册 BackRequested 事件的侦听器 ...

  9. Attention is all you need及其在TTS中的应用Close to Human Quality TTS with Transformer和BERT

    论文地址:Attention is you need 序列编码 深度学习做NLP的方法,基本都是先将句子分词,然后每个词转化为对应的的词向量序列,每个句子都对应的是一个矩阵\(X=(x_1,x_2,. ...

  10. VC 函数调用的 汇编代码 浅析

    摘要:主要谈谈vc里面函数调用汇编成汇编代码的情形,首先针对之前的一个小程序,说说vc编译器的优化. 例子程序: #include <iostream>using namespace st ...