HDU6447 网络赛 YJJ's Salesman(DP + 线段树)题解
思路:若用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 + 线段树)题解的更多相关文章
- 南京网络赛G-Lpl and Energy【线段树】
During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...
- 省选模拟赛 4.26 T1 dp 线段树优化dp
LINK:T1 算是一道中档题 考试的时候脑残了 不仅没写优化 连暴力都打挂了. 容易发现一个性质 那就是同一格子不会被两种以上的颜色染.(颜色就三种. 通过这个性质就可以进行dp了.先按照左端点排序 ...
- 2019南昌网络赛-I(单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- 2019CCPC网络赛——array(权值线段树)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6703 题目大意: 给出一个n(n<1e5)个元素的数组A,A中所有元素都是不重复的[1,n]. 有 ...
- hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】
https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 当时忽略了一个重要问题,就是ax*ay要最小时,x.y可以相等,那就简单 ...
- 2019南昌网络赛-I. Yukino With Subinterval 线段树套树状数组,CDQ分治
TMD...这题卡内存卡的真优秀... 所以以后还是别用主席树的写法...不然怎么死的都不知道... 树套树中,主席树方法开权值线段树...会造成空间的浪费...这道题内存卡的很紧... 由于树套树已 ...
- 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. ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
随机推荐
- vue脚手架一
一准备: 在F:/xampp/htdocs/文件夹下检查: 1,node -v; 2,npm -v; 3,淘宝镜像(选装): npm install -g cnpm --registry= https ...
- 配置linux DNS
DNS服务器地址配置 在Linux下面,有一个默认的DNS服务器地址配置文件的设置,存放在 /etc/resolv.conf 设置方法很简单,通过编辑 vi /etc/resolv.conf 设置首选 ...
- 【BZOJ1040】[ZJOI2008]骑士 树形DP
[BZOJ1040][ZJOI2008]骑士 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情 ...
- Android 让GridView的高度为Wrap_content根据内容自适应高度
From:http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/ 如果把Grid ...
- centos samba搭建
1.需求: 建立两个用户(zx,zxadmin),zxadmin能访问所有目录,zx只能访问指定目录. 2.安装smb [root@vi /]# yum install samba -y 3.创建用户 ...
- 解决pip install 安装慢问题
使用豆瓣源 比如安装pyspark pip install -i https://pypi.douban.com/simple/ pyspark 速度就比用pip install快N倍 关注公众号:
- spring boot实战(第一篇)第一个案例
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...
- Webservice实践(七)CXF 与Spring结合+tomcat发布
上一节介绍了如何使用CXF 来发布服务,但是没有介绍使用web 容器来发布,很多项目需要用tomcat 这样的容器来发布.另外本节将介绍CXF 与spring 结合的方法. 一 目标: 1.利用spi ...
- Integer.valueof 和 Integer.parseInt
System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); System.out.p ...
- HTTP错误 404.17–Not Found 请求的内容似乎是脚本,因而将无法有静态文件处理程序来处理
解决方案:切换应用程序池的模式.