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 ...
随机推荐
- nodeJS基础---->nodeJS的使用(一)
Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V8引擎执行Javascript的速度非常快,性能非常好.Node.js对一些特殊用例 ...
- linux ctags
在vim 下查找函数的定义是比较方法的事情,尤其是是跨文件的时候. 这时候可以通过安装ctags来实现函数定义跳转. 安装可以直接百度. 安装好之后,首先需要配置vim, 因为一般只有vim打开的文件 ...
- Compass(更新中。。。)
compass Compass是Sass的工具库,就好像jQuery是js的库一样. sass有了compass的配合,就会更加事半功倍. Sass本身只是一个编译器,Compass在它的基础上,封装 ...
- Codeforces Round #375 (Div. 2)E. One-Way Reform
题目链接:传送门 题目大意:一副无向图,要求你给边定向(变为有向图),使出度等于入度的点最多,输出有多少 个点,并且输出定向后的边(前为起点,后为终点) 题目思路:欧拉路 我们这样考虑,先考虑无向图的 ...
- rpm方式安装 gitlab centos7
一.使用RPM安装 - 推荐 官方推荐的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/ Tips 1 : Centos 7使用el7 ...
- 【BZOJ1568】[JSOI2008]Blue Mary开公司 线段树
[BZOJ1568][JSOI2008]Blue Mary开公司 Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词“Query”或“P ...
- idea的svn插件中compare with the same repository version和compare with latest repository version的区别?
Idea的svn插件中compare with the same repository version和compare with latest repository version的区别? 1.com ...
- linux .zip 解压命令集
zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...
- log4j配置输出到多个日志文件(转)
參考资料:http://logging.apache.org/log4j/1.2/manual.html 通常我们项目里,有一些重要的日志想单独的输出到指定的文件,而不是全总输出到系统的日志文件中.那 ...
- 新一代Java模板引擎Thymeleaf
新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...