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. java 中的equals()小结

    转载自http://www.cnblogs.com/jackyrong/archive/2006/08/20/481994.html Java中的equals是十分重要的,和= =要区别开来,最近在看 ...

  2. SQL Server中的扩展事件学习系列

    SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events SQL Server 扩展事件(Extented Event ...

  3. Java数据结构之单向环形链表(解决Josephu约瑟夫环问题)

    1.Josephu(约瑟夫.约瑟夫环)问题: 设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m ...

  4. rem和css3的相关知识点

    ☆☆☆rem和css3的相关知识点☆☆☆ 一. Web front-end development engineer rem是根据页面的根元素的font-size的一个相对的单位,即 html{ fo ...

  5. 【源码解读】pix2pix(一):训练

    源码地址:https://github.com/mrzhu-cool/pix2pix-pytorch 相比于朱俊彦的版本,这一版更加简单易读 训练的代码在train.py,开头依然是很多代码的共同三板 ...

  6. 如何让form2中的数据源,显示在form1的dataGridView控件中呢????

    定义一个static的静态变量,即可全局访问

  7. https://www.cnblogs.com/cncc/p/7804511.html?foxhandler=RssReadRenderProcessHandler

    https://www.cnblogs.com/cncc/p/7804511.html?foxhandler=RssReadRenderProcessHandler 一.本文主要是使用Costura. ...

  8. MAKEDEV - 建立设备

    总览 (SYNOPSIS) cd dev; ./MAKEDEV -V cd dev; ./MAKEDEV [ -n ] [ -v ] update cd dev; ./MAKEDEV [ -n ] [ ...

  9. blazeFace

    围绕四个点构造模型 1.扩大感受野 使用5*5卷积替换3*3来扩大感受野,在深度分离卷积中,pw与dw计算比为d/k^2,d为输出通道,k为 dw的卷积核,即增加dw的卷积核所带来的计算并不大. 在M ...

  10. 1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录

    <Redis深度历险:核心原理和应用实践>1.基础: 万丈高楼平地起——Redis基础数据结构 学习记录http://naotu.baidu.com/file/b874e2624d3f37 ...