Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
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.
因a和b数组中的数较大,因此需要离散化。可以用类似于LIS的方法进行dp转移,但因为题目要求时间复杂度为O(nlogn),所以还要用树状数组或线段树进行优化,维护1到某个半径的最大高度。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
struct ss
{
long long a,b,c;
};
ss a[];
long long n,len,tree[],m=;
struct Hash : vector<int> { //离散化
void prepare() {
sort(begin(), end());
//erase(unique(begin(), end()), end());
}
long long get(long long x) {
return lower_bound(begin(), end(), x)-begin()+;
}
} has;
void upd(long long x,long long y)
{
for (;x<=m;x+=x&(-x)) tree[x]=max(tree[x],y);
}
long long sum(long long x)
{
long long p=;
for (;x;x-=x&(-x)) p=max(p,tree[x]);
return p;
}
inline bool cmp(ss a,ss b)
{
return (a.b>b.b||a.b==b.b&&a.a>b.a);
}
int main()
{
scanf("%lld",&n);
long long i;
for (i=;i<=n;i++)
scanf("%lld%lld%lld",&a[i].a,&a[i].b,&a[i].c);
has.clear();
for (i=;i<=n;i++)
has.push_back(a[i].a),has.push_back(a[i].b);
has.prepare();
for (i=;i<=n;i++)
a[i].a=has.get(a[i].a),a[i].b=has.get(a[i].b),m=max(a[i].b,m);
m*=;
sort(a+,a+n+,cmp);
//for (i=1;i<=n;i++)
// printf("%d %d %d\n",a[i].a,a[i].b,a[i].c);
//cout<<m<<endl;
memset(tree,,sizeof(tree));
long long ans=;
for (i=;i<=n;i++)
{
long long now=sum(a[i].b-)+a[i].c;
//cout<<sum(a[i].b-1)<<endl;
upd(a[i].a,now);
ans=max(ans,now);
}
printf("%lld\n",ans);
return ;
}
Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)的更多相关文章
- BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)
题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...
- [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】
题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...
- BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树
题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- st表、树状数组与线段树 笔记与思路整理
已更新(2/3):st表.树状数组 st表.树状数组与线段树是三种比较高级的数据结构,大多数操作时间复杂度为O(log n),用来处理一些RMQ问题或类似的数列区间处理问题. 一.ST表(Sparse ...
- bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1384 Solved: 629[Submit][Stat ...
- [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】
题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...
- POJ 1195 Mobile phones (二维树状数组或线段树)
偶然发现这题还没A掉............速速解决了............. 树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用......... #include < ...
- 【BZOJ3196】二逼平衡树(树状数组,线段树)
[BZOJ3196]二逼平衡树(树状数组,线段树) 题面 BZOJ题面 题解 如果不存在区间修改操作: 搞一个权值线段树 区间第K大--->直接在线段树上二分 某个数第几大--->查询一下 ...
随机推荐
- 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl
目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...
- 【C++】类型转换简述:四种类型转换方式的说明及应用
本文主要简述在C++中四种类型转换的方式:static_cast.reniterpret_cast.const_cast和dynamic_cast. 在介绍C++类型转换方式之前,我们先来看看C语言的 ...
- Node.js——post方式提交的图片如何保存
https://www.cnblogs.com/bruce-gou/p/6399766.html 没有使用express框架,主要是对于 request 的监听,data的时候对数据进行保存,end的 ...
- CREATE CONSTRAINT TRIGGER - 定义一个新的约束触发器
SYNOPSIS CREATE CONSTRAINT TRIGGER name AFTER events ON tablename constraint attributes FOR EACH ROW ...
- Vim中文编码问题
1.影响中文编码的设置项 encoding(enc):encoding是Vim的内部使用编码,encoding的设置会影响Vim内部的Buffer.消息文字等.在 Unix环境下,encoding的默 ...
- CAD交互绘制带颜色宽度的直线(com接口)
用户可以在控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY ...
- Spring全局异常捕获
package org.xxx.ac.zpk.exception; import java.io.IOException; import javax.servlet.http.HttpServletR ...
- springboot实现web应用过程中的摸爬打滚(持续更新ing)
最近在做公司的网站项目,后端用到springboot.怎么说呢,记录总结一下自己开发过程中遇到的坑和一些心得体会,以及一些技巧.方便以后回顾复习,也供同行们参考. 开发环境:eclipse2018-1 ...
- 第2节 mapreduce深入学习:9、手机上行流量排序
还是上次那个例子,需求二:上行流量倒序排序(递减排序) 分析,以需求一的输出数据作为排序的输入数据,自定义FlowBean,以FlowBean为map输出的key,以手机号作为Map输出的value, ...
- 原生j获取元素的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...