思路:若用dp[i][j]表示走到(i,j)的最大值,那么dp[i][j] = max(dp[i - 1][j],dp[i][j - 1],dp[i - 1][j - 1] + v),显然O(n^2)超时。但是我们可以优化这个dp,我们用f[j]表示第i行第j列当前最大值,那么f[j] = max(f[j],f[k] + v[i][j]),k范围0~j - 1,所以我们只要用O(logn)找到f[k]就行了,显然可以用线段树维护f[j]。我们先离散化,然后从上到下,从右到左排序,从右到左是因为我们在更新第i行第j列时,我们所需要的f[k]是第i-1行以前的f[k],这里需要注意。

代码:

#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = + ;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
struct node{
int x, y, v;
}q[maxn];
bool cmp(node a, node b){
if(a.x < b.x) return true;
if(a.x == b.x && a.y > b.y) return true;
return false;
} int Max[maxn << ];
void update(int l, int r, int pos, int v, int rt){
if(l == r){
Max[rt] = max(Max[rt], v);
return;
}
int m = (l + r) >> ;
if(pos <= m)
update(l, m, pos, v, rt << );
else
update(m + , r, pos, v, rt << | );
Max[rt] = max(Max[rt << ], Max[rt << | ]);
}
int query(int l, int r, int L, int R, int rt){
if(L <= l && R >= r){
return Max[rt];
}
int MAX = ;
int m = (l + r) >> ;
if(L <= m)
MAX = max(MAX, query(l, m, L, R, rt << ));
if(R > m)
MAX = max(MAX, query(m + , r, L, R, rt << | ));
return MAX;
}
int x[maxn], y[maxn], f[maxn];
int main(){
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d%d%d", &x[i], &y[i], &q[i].v);
q[i].x = x[i];
q[i].y = y[i];
}
sort(x, x + n);
sort(y, y + n);
int cnt1 = unique(x, x + n) - x;
int cnt2 = unique(y, y + n) - y;
for(int i = ; i < n; i++){
q[i].x = lower_bound(x, x + cnt1, q[i].x) - x + ;
q[i].y = lower_bound(y, y + cnt2, q[i].y) - y + ;
}
sort(q, q + n, cmp); memset(Max, , sizeof(Max));
for(int i = ; i < n; i++){
int ret;
if(q[i].y == ){
ret = q[i].v;
}
else{
ret = query(, cnt2, , q[i].y - , ) + q[i].v;
}
update(, cnt2, q[i].y, ret, );
}
printf("%d\n", Max[]);
}
return ;
}

HDU6447 网络赛 YJJ's Salesman(DP + 线段树)题解的更多相关文章

  1. 南京网络赛G-Lpl and Energy【线段树】

    During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...

  2. 省选模拟赛 4.26 T1 dp 线段树优化dp

    LINK:T1 算是一道中档题 考试的时候脑残了 不仅没写优化 连暴力都打挂了. 容易发现一个性质 那就是同一格子不会被两种以上的颜色染.(颜色就三种. 通过这个性质就可以进行dp了.先按照左端点排序 ...

  3. 2019南昌网络赛-I(单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...

  5. 2019CCPC网络赛——array(权值线段树)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6703 题目大意: 给出一个n(n<1e5)个元素的数组A,A中所有元素都是不重复的[1,n]. 有 ...

  6. hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】

    https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 当时忽略了一个重要问题,就是ax*ay要最小时,x.y可以相等,那就简单 ...

  7. 2019南昌网络赛-I. Yukino With Subinterval 线段树套树状数组,CDQ分治

    TMD...这题卡内存卡的真优秀... 所以以后还是别用主席树的写法...不然怎么死的都不知道... 树套树中,主席树方法开权值线段树...会造成空间的浪费...这道题内存卡的很紧... 由于树套树已 ...

  8. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 i题 Minimum(线段树)

    描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...

  9. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

随机推荐

  1. LeetCode——Submission Details

    Description: Given a string of numbers and operators, return all possible results from computing all ...

  2. mouseover&mouseout和mouseenter&mouseleave

    mouseenter&mouseleave: 进入被选元素触发,进入被选元素的子元素不会重复触发. mouseover&mouseout: 进入被选元素触发,从被选元素进入其子元素会再 ...

  3. Centos7 安装zabbix3.0 服务端 详细

    参考: https://www.cnblogs.com/37yan/p/6879218.html http://blog.csdn.net/hao134838/article/details/5712 ...

  4. Sublime Less 自动编译成css

    1.note编译 .下载notejs https://nodejs.org/en/ .首先你要安装lessc.我是用npm包管理器直接安装的,只需要一条命令,如下: npm install less ...

  5. CHMOD命令怎么用?

    文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 其语法格式为:chmod [w ...

  6. OneThink视图模型进行组合查询!文档组合文档详情

    测试方法:twoCate: public function twoCate(){ $where = array( 'category_id'=>43 ); $list = D('TwoView' ...

  7. 110道python题+理解(不断更新)

    此篇题目在网上已经广为流传,但好多都不做解释,所以我想着自己一道一道的做一遍,并将相关涉及的做个补充,个人知识毕竟片面,有不足的地方还请大家多多指正 一.请用一行代码实现1-100之和 >> ...

  8. 170504、MongoDB和MySQL对比(译)

    一.概要 几十年来,关系型数据库已经成为企业应用程序的基础,自从MySQL在1995年发布以来,它已经成为一种受欢迎并且廉价的选择.然而随着近年来数据量和数据的不断激增,非关系数据库技术如MongoD ...

  9. 170503、centos6.5安装mysql5.6.30

    准备:虚拟机地址:192.168.0.110 安装目录/usr/local/ 首先卸载已经安装的mysql使用命令查看rpm -qa | grep mysql卸载使用 rpm -e xxx 或者 yu ...

  10. SaltStack生产案例-服务部署(redis,mysql,apache+php,haproxy+keepalived)

    顺序代码资料链接 课上资料.zip 接上篇:SaltStack生产案例-系统初始化 1,redis 主从 2,mysql 主从 2.1 mysql-install.sls (安装 初始化) 2.2   ...