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. 剑指Offer编程题(Java实现)——链表中倒数第k个结点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 注意: 该题目不可以用先反转链表再输出第k个结点的方式,因为反转链表会改变该结点的next指向 思路一 使用栈Stack倒序存储,顺序pop第k个 ...

  2. c++ 判断两圆位置关系

    对于两圆的位置一般有五种关系: (1) 外离:两圆的半径之和小于两圆圆心距离 (2) 外切:两圆的半径之和等于两圆圆心距离 (3) 相交:两圆的半径之和大于两圆圆心距离,两圆圆心距离大于两圆半径之差 ...

  3. [Luogu 5465] [LOJ 6435] [PKUSC2018]星际穿越(倍增)

    [Luogu 5465] [LOJ 6435] [PKUSC2018]星际穿越(倍增) 题面 n个点的图,点i和[l[i],i)的所有点连双向边.每次询问(l,r,x)表示x到[l,r]的所有点的最短 ...

  4. 【SSL2325】最小转弯问题

    题面: \[\Large\text{最小转弯问题}\] \[Time~Limit:1000MS~~Memory~Limit:65536K\] Description 给出一张地图,这张地图被分为 n× ...

  5. Intellij IDEA奇巧妙计(不停更新)

    1,在pom.xml文件中,Ctrl+Shift+Alt+U打开Manven依赖视图 2,Alt+7 查看类里面方法,变量等结构 3, Shift+Esc 收缩编译提示框 4, ctrl+r 替换本页 ...

  6. nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG

    需求描述,由于工作的需要,需要将原本用于1280 720的网页改为1920 1080的网页(电视端页面).需求可以拆分为两部分,代码部分的修改以及图片的修改.在代码部分,需要将所有位置以及大小相关的值 ...

  7. jupyter与requests的初步使用

    upyter 是一个简易的,方便的写Python代码的工具包,requests是Python里非常好用的用来发送 http 请求的包. 开始学习本教程之前,请确保你已经安装了Python,并且安装了P ...

  8. Runnable、Callable和Future三者对比

    Runnable是个借口,使用简单: 1. 实现该接口并重写run方法 2. 利用该类的对象创建线程 3. 线程启动时就会自动调用该对象的run方法     通常在开发中结合ExecutorServi ...

  9. luogu P5331 [SNOI2019]通信

    传送门 有匹配次数限制,求最小代价,这显然是个费用流的模型.每个点暴力和前面的点连匹配边,边数是\(n^2\)的. 然后发现可以转化成一个set,每次加入一个点,然后入点对set里面的出点连边.这个s ...

  10. Ajax加载数据后百度分享实例

    <script type="text/javascript"> //百度分享 function baidu_share() { var title_val = $(&q ...