Link

简化题意

给你一张网格图,每个点有其对应的权值,让你找出来一条横纵坐标都单调不降的路径,并最大化经过点的权值。

分析

这是经典的二维数点或者二维偏序问题。

如果两维一直在变的话,我们不是很好处理,所以我们考虑对这些点排一下序,(按横纵坐标都可以)。

我一般按照横坐标来排序的。然后就变成了一维的最长不下降子序列问题。

设 \(f[i]\) 表示 以 \(i\) 这个高度为结尾的经过路径的最大权值。

则有转移 \(f[i] = max(f[j] + a[i].w) j\leq i\)

因为公交车可以延着横坐标走,所以他也可以由 \(f[i]\) 转移过来。

此外还有一个要注意的点就是公交车沿着纵坐标竖着走,要先更新高度比较小的 \(f\) 值,(我就在这里卡了好几回)

那我们一开始的排序就可以以横坐标为第一关键字,纵坐标为第二关键字排序,这样方便我们 \(dp\)。

这样的直接 \(dp\) 的复杂度是 \(O(n^2)\) 的,可以考虑用树状数组或者线段树维护一下。

树状数组常熟小,代码短,也比较好写,所以我一般选择树状数组。

最后的答案就是 \(max(f[i])\), 另外不要忘记离散化哦。

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define int long long//不开long long 见祖宗·
const int N = 1e5+10;
int n,m,k,tr[N],b[N];
inline int read()
{
int s = 0,w = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
struct node
{
int x,y,w;
}a[100010];
bool comp(node a,node b)
{
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
int lowbit(int x){ return x & -x; }
void chenge(int x,int val)
{
for(; x <= N-5; x += lowbit(x)) tr[x] = max(tr[x],val);
}
int ask(int x)
{
int res = 0;
for(; x; x -= lowbit(x)) res = max(res,tr[x]);
return res;
}
signed main()
{
n = read(); m = read(); k = read();
for(int i = 1; i <= k; i++)
{
a[i].x = read();
a[i].y = read();
a[i].w = read();
b[i] = a[i].y;
}
sort(a+1,a+k+1,comp);//排序
sort(b+1,b+k+1);
int num = unique(b+1,b+k+1)-b-1;
for(int i = 1; i <= k; i++) a[i].y = lower_bound(b+1,b+num+1,a[i].y)-b;//离散化
for(int i = 1; i <= k; i++)
{
int res = ask(a[i].y);//树状数组优化dp
chenge(a[i].y,res+a[i].w);
}
printf("%lld\n",ask(N-5));
return 0;
}

P3431 [POI2005]AUT-The Bus的更多相关文章

  1. 洛谷P3431 [POI2005]AUT-The Bus

    P3431 [POI2005]AUT-The Bus 题目描述 The streets of Byte City form a regular, chessboardlike network - th ...

  2. 「BZOJ1537」Aut – The Bus(变形Dp+线段树/树状数组 最优值维护)

    网格图给予我的第一反应就是一个状态 f[i][j] 表示走到第 (i,j) 这个位置的最大价值. 由于只能往下或往右走转移就变得显然了: f[i][j]=max{f[i-1][j], f[i][j-1 ...

  3. 树状数组 二维偏序【洛谷P3431】 [POI2005]AUT-The Bus

    P3431 [POI2005]AUT-The Bus Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 ...

  4. 洛谷 P3431:[POI2005]AUT-The Bus(离散化+DP+树状数组)

    题目描述 The streets of Byte City form a regular, chessboardlike network - they are either north-south o ...

  5. 二维偏序+树状数组【P3431】[POI2005]AUT-The Bus

    Description Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个 ...

  6. bzoj 1537: [POI2005]Aut- The Bus 线段树

    bzoj 1537: [POI2005]Aut- The Bus 先把坐标离散化 设f[i][j]表示从(1,1)走到(i,j)的最优解 这样直接dp::: f[i][j] = max{f[i-1][ ...

  7. BZOJ1537: [POI2005]Aut- The Bus

    1537: [POI2005]Aut- The Bus Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 158  Solved: 100[Submit][S ...

  8. BZOJ 1537: [POI2005]Aut- The Bus(dp + BIT)

    对y坐标离散化, 然后按x坐标排序, dp. 一个点(x, y), 设到达这个点接到的最多乘客数为t, 那么t可以用来更新y'>=y的所有点.用树状数组维护最大值. -------------- ...

  9. Bzoj 1537: [POI2005]Aut- The Bus 题解 [由暴力到正解]

    1537: [POI2005]Aut- The Bus Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 264[Submit][S ...

随机推荐

  1. js 数组与字符串互相转换

    1.数组转字符串 arr.join() 2.字符串转数组 str.split(',')

  2. Sorting It All Out (拓扑排序+思维)

    An ascending sorted sequence of distinct values is one in which some form of a less-than operator is ...

  3. Commando War (贪心)

    Waiting for orders we held in the wood, word from the front never came By evening the sound of the g ...

  4. Mybatis实例增删改查(二)

    创建实体类: package com.test.mybatis.bean; public class Employee { private Integer id; private String las ...

  5. el-select 封装

    这里打算封装一个全局el-select组件 MySelect.vue <template> <el-select v-if="options.length!==0" ...

  6. idea中右击的快捷键都找不到 Diagrams

    今天突然发现了一件很恐怖的事情,那就是我的IDEA的右击中找不到Diagrams了,因为我是用这个东西打开 .bpmn文件生成png的,突然没了.. 说一下解决吧 在FIle -> settin ...

  7. 深入理解 vue 中 scoped 样式作用域的规则

    哈喽!大家好!我是木瓜太香,今天我们来聊一个 vue 的样式作用域的问题,通常我们开发项目的时候是要在 style 上加上 scoped 来起到规定组件作用域的效果的,所以了解他们的规则也是很有必要的 ...

  8. MvvmLight框架使用入门(5)

    上一次写MvvmLight框架使用入门(4)的时候还在用Visual Studio 2015,我儿子也不会过来盖上我的XPS……重启这个系列一方面是因为最近又开始写UWP的东西了,另一个是因为Mvvm ...

  9. jmeter连数据库

    前提:jmeter不能直接连数据库,需要导入一个jar包 步骤: 1.右键线程组--添加--配置元件--JDBC Connection Configuration 2.jdbc的基本配置:可以修改jd ...

  10. Unit2:活动

    1.基本用法 1.创建活动 Generate LayoutFile 创建布局文件 Launcher Activity 自动注册为主活动 编写顺序 活动Activity 注册.xml 界面res.xx ...