YJJ's Salesman

YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. 
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0) (0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.) 
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

线段树+区间离散化+dp

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define P pair<ll,ll>
#define sc(x) scanf("%I64d",&x);
#define maxn 100005
struct Nod
{
int x,y,v; };
Nod A[maxn];
int N;
int L[maxn*],R[maxn*],V[maxn*];
bool cmp(Nod a,Nod b)
{
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
void build(int l,int r,int x)
{ L[x]=l;
R[x]=r; if(l==r)
{
V[x]=;
return ;
}
int mid=(l+r)/;
build(l,mid,*x);
build(mid+,r,*x+);
V[x] =;
}
int query(int l,int r,int x)
{
if(r==)return ;
if(L[x]>=l&&R[x]<=r)
{
return V[x]; }
else
{
int mid=(L[x]+R[x])/;
if(r<=mid)return query(l,r,*x);
else if(l>mid)return query(l,r,*x+);
else return max(query(l,r,*x),query(l,r,*x+)); }
}
void update(int x,int pos,int w)
{
if(L[x]==R[x])
{
V[x]=max(w,V[x]);
return;
}
int mid=(L[x]+R[x])/;
if(mid>=pos)update(x*,pos,w);
else update(x*+,pos,w);
V[x]=max(V[*x],V[*x+]); }
int B[maxn];
signed main()
{
int T;
sc(T);
while(T--)
{
sc(N);
for(int i=; i<=N; i++)
{
sc(A[i].x);
sc(A[i].y);
sc(A[i].v);
B[i]=A[i].y;
}
sort(B+,B+N+);
int siz=unique(B+,B+N+)-B-;
for(int i=; i<=N; i++)
{
A[i].y=lower_bound(B+,B+siz+,A[i].y)-B;
}
build(,N,);
sort(A+,A+N+,cmp);
int ans=;
for(int i=; i<=N; i++)
{
ll t=query(,A[i].y-,)+A[i].v;
// cout<<A[i].y-1<<" "<<t<<'\n';
update(,A[i].y,t);
ans=max(ans,t);
}
cout<<ans<<'\n';
}
}

树状数组大法好

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define P pair<ll,ll>
#define sc(x) scanf("%I64d",&x);
#define maxn 100005
struct Nod
{
int x,y,v; };
Nod A[maxn];
int N;
int V[maxn*];
bool cmp(Nod a,Nod b)
{
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
void add(int x,int val)
{
while(x<=N){
V[x]=max(val,V[x]);
x+=(x&-x);
}
}
int get(int x)
{
if(x==)return ;
int ans=;
while(x){
ans=max(V[x],ans);
x-=(x&-x);
}
return ans;
}
int B[maxn];
signed main()
{
int T;
sc(T);
while(T--)
{
memset(V,,sizeof V);
sc(N);
for(int i=; i<=N; i++)
{
sc(A[i].x);
sc(A[i].y);
sc(A[i].v);
B[i]=A[i].y;
}
sort(B+,B+N+);
int siz=unique(B+,B+N+)-B-;
for(int i=; i<=N; i++)
{
A[i].y=lower_bound(B+,B+siz+,A[i].y)-B;
}
//build(1,N,1);
sort(A+,A+N+,cmp);
int ans=;
for(int i=; i<=N; i++)
{
ll t=get(A[i].y-)+A[i].v;
// cout<<A[i].y-1<<" "<<t<<'\n';
add(A[i].y,t);
ans=max(ans,t);
}
cout<<ans<<'\n';
}
}

YJJ's Salesman的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  2. HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...

  3. hdu6447 YJJ's Salesman

    这个题意和数据范围一看就是离散化之后树状数组优化DP.给的"从左下方走上去才能拿到收益"的性质其实可以当成"必须从横纵坐标严格比某个点小的地方转移过来".1A了 ...

  4. 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) ...

  5. HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)

    题意: 二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动, 该N个点只有从它的左下方格点可达,此时可获得收益.求该过程最大收益. 分析:我们很容易就可以想到用 ...

  6. 2018 CCPC网络赛

    2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution

    A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...

  8. hdu6447

    YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. 2018CCPC网络赛

    A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...

随机推荐

  1. 第九周课程总结&实验报告7

    实验任务详情: 完成火车站售票程序的模拟.要求:(1)总票数1000张:(2)10个窗口同时开始卖票:(3)卖票过程延时1秒钟:(4)不能出现一票多卖或卖出负数号票的情况. 实验代码: package ...

  2. SQLSERVER 备份恢复之后提示缺少创建表权限的问题解决

    1. 同事这边出现异常: 执行功能是报错, 这个错误 应该是创建临时表时没有权限触发的. 方法应该很简单 缺少权限增加权限即可 2. 这里需要使用sa 或者是 具有sa权限的用户登录 打开 数据库级别 ...

  3. 在git bash 中配置git用户名和邮箱及查看配置信息

    Administrator@LuoTong- MINGW32 ~ $ git config --global user.name "mrluotong" Administrator ...

  4. Linux 最常用命令整理,建议收藏!

    Linux是目前应用最广泛的服务器操作系统,基于Unix,开源免费,由于系统的稳定性和安全性,市场占有率很高,几乎成为程序代码运行的最佳系统环境. linux不仅可以长时间的运行我们编写的程序代码,还 ...

  5. 洛谷P3366【模板】最小生成树-克鲁斯卡尔Kruskal算法详解附赠习题

    链接 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M&l ...

  6. 计算机系统结构总结_Instruction Set Architecture

    Textbook:<计算机组成与设计——硬件/软件接口>  HI<计算机体系结构——量化研究方法>          QR 这节我们来看CPU内部的一些东西. Instruct ...

  7. go & cron

    https://github.com/robfig/cron - 源码 cron - GoDoc 参考 go---定时任务 cron,gin 静态文件 go语言里比较好用的计划任务调度模块

  8. maven联通网络下中央仓库不能访问的解决办法

    最近刚开始学习maven工具,下载解压完毕,环境变量配置完毕,运行如下命令尝试快速构建一个maven项目: mvn archetype:generate 结果就有问题: [INFO] Scanning ...

  9. Linux系统性能测试工具(四)——CPU性能测试工具之super_pi、sysbench

    本文介绍关于Linux系统(适用于centos/ubuntu等)的CPU性能测试工具-sysbench.CPU性能测试工具包括: super_pi: sysbench——不仅可以测试CPU性能,而且可 ...

  10. Zabbix--06主动模式和被动模式、低级自动发现、性能优化、

    目录 一. Zabbix主动模式和被动模式 1.克隆模版 2.修改克隆后的模版为主动模式 3.修改监控主机关联的模版为主动模式 4.修改客户端配置文件并重启 5.查看最新数据 二.Zabbix低级自动 ...