题目链接 Hanoi Factory

很容易想到这是一个DAG模型,那么状态转移方程就出来了。

但是排序的时候有个小细节:b相同时看a的值。

因为按照惯例,堆塔的时候肯定是内半径大的在下面。

因为N有1e5,那么DP的时候用线段树优化一下,就可以了。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)              for(int i(a); i <= (b); ++i)

typedef long long LL;

const int N     =    200000      +       10;

struct Segtree{
int l, r;
LL num;
} segtree[N << 2]; struct node{
int a, b;
LL h;
friend bool operator < (const node &A, const node &B){
return A.b == B.b ? A.a > B.a : A.b > B.b;
}
} c[N]; struct Node{
int x, y;
friend bool operator < (const Node &a, const Node &b){
return a.x < b.x;
}
} f[N]; int aa[N], bb[N];
int n, m, cnt;
map <int, int> mp;
LL ans; inline void pushup(int i){
segtree[i].num = max(segtree[i << 1].num, segtree[i << 1 | 1].num);
} void build(int i, int l, int r){ segtree[i].l = l;
segtree[i].r = r;
segtree[i].num = 0; if (l == r) return ;
int mid = (l + r) >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
} void update(int i, int pos, LL value){
int L = segtree[i].l, R = segtree[i].r;
if (L == R && L == pos){
segtree[i].num = max(segtree[i].num, value);
return;
} int mid = L + R >> 1;
if (pos <= mid) update(i << 1, pos, value);
else update(i << 1 | 1, pos, value); pushup(i);
} LL query(int i, int l, int r){
int L = segtree[i].l, R = segtree[i].r;
if (L == l && R == r) return segtree[i].num;
int mid = L + R >> 1;
LL ret = 0;
if (r <= mid)
ret = max(ret, query(i << 1, l, r));
else
if (l > mid)
ret = max(ret, query(i << 1 | 1, l, r));
else
{
ret = max(ret, query(i << 1, l, mid));
ret = max(ret, query(i << 1 | 1, mid + 1, r));
} return ret;
} int main(){ scanf("%d", &n);
cnt = 0;
rep(i, 1, n){
scanf("%d%d%lld", aa + i, bb + i, &c[i].h);
f[++cnt].x = aa[i];
f[++cnt].x = bb[i];
} sort(f + 1, f + cnt + 1); f[1].y = 1;
rep(i, 2, cnt) f[i].y = f[i].x == f[i - 1].x ? f[i - 1].y : f[i - 1].y + 1;
rep(i, 1, cnt) mp[f[i].x] = f[i].y; rep(i, 1, n){
c[i].a = mp[aa[i]];
c[i].b = mp[bb[i]];
} sort(c + 1, c + n + 1); m = 0;
rep(i, 1, n){
m = max(m, c[i].a);
m = max(m, c[i].b);
} build(1, 1, m); rep(i, 1, n){
LL now = query(1, 1, c[i].b - 1);
LL cnt = now + c[i].h;
ans = max(ans, cnt);
update(1, c[i].a, cnt);
} printf("%lld\n", ans); return 0; }

Codeforces 777E Hanoi Factory(线段树维护DP)的更多相关文章

  1. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  4. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  5. 【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】

    题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的. 然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个.如下图 而如果一条链上面有分支,也 ...

  6. 2019牛客暑期多校训练营(第二场)E 线段树维护dp转移矩阵

    题意 给一个\(n\times m\)的01矩阵,1代表有墙,否则没有,每一步可以从\(b[i][j]\)走到\(b[i+1][j]\),\(b[i][j-1]\),\(b[i][j+1]\),有两种 ...

  7. Codeforces750E. New Year and Old Subsequence (线段树维护DP)

    题意:长为2e5的数字串 每次询问一个区间 求删掉最少几个字符使得区间有2017子序列 没有2016子序列 不合法输出-1 题解:dp i,p(0-4)表示第i个数匹配到2017的p位置删掉的最少数 ...

  8. CodeForces833 B. The Bakery 线段树维护dp

    题目链接:https://vjudge.net/problem/CodeForces-833B 题意:给长度为n的数组a,和一个整数k要求把数组分成连续的k段,每段的权值是该段中不同数的个数,输出最大 ...

  9. hdu4719 Oh My Holy FFF 线段树维护dp

    题意:给你一个长度为n的数组v,你需要把这个数组分成很多段,你需要保证每一段的长度不能超过k我们设一共有m段,每一段右边界那个数为bi那么我们要使得sum(bi*bi-b(i-1))最大 (1< ...

随机推荐

  1. 拓扑排序+不是字典序的优先级排列(POJ3687+HDU4857)

    一.前言 在过去的一周里结束了CCSP的比赛,其中有一道题卡了我9个小时,各种调错都没法完整的调处来这题,于是痛下决心开始补题,这个是计划的一部分.事实上,基于错误的理解我写了若干发拓扑排序+字典序的 ...

  2. Linux文件类型 扩展名的作用

    链接类型文件 查找显示管道文件 普通文件类型 file 查看文件的类型 data文件类型 创建块字和符设备 mknod 1,.tar .tar.gz .tgz .zip tar.bz 表示压缩文件,创 ...

  3. wcf第三方客户端与wcf服务之间调用入门

    Wcf服务与我们的客户端如何建立联系的呢.本文简单记录一下 1.创建我们的wcf服务程序. 第一个wcf服务库是创建我们的wcf库,运行时会单独来托管我们的程序,而非托管在iis下. 第二个wcf服务 ...

  4. git 使用规范

    git使用资料: https://github.com/peak-c/my-git 公司内部使用开发规范: 一. 代码库介绍 个人开发库(git@gitlab.adrd.sohuno.com:sper ...

  5. MySQL基础6-分组查询

    1.分组函数 需求20:查询所有商品平均零售价SELECT AVG(salePrice) FROM product 需求21:查询商品总记录数SELECT COUNT(id) count FROM p ...

  6. angular用$sce服务来过滤HTML标签

    angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...

  7. cf984e Elevator

    ref我好菜啊 #include <iostream> #include <cstring> #include <cstdio> #include <cmat ...

  8. 设计模式之第1章-工厂方法模式(Java实现)

    设计模式之第1章-工厂方法模式(Java实现) “我先来”,“不,老公,我先!”.远远的就听到几个人,哦不,是工厂方法模式和抽象工厂模式俩小夫妻在争吵,尼妹,又不是吃东西,谁先来不都一样(吃货的世界~ ...

  9. 【Word Break】cpp

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  10. leetcode NO.349 两个数组的交集 (python实现)

    来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...