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. 【VS开发】使用WinPcap编程(1)——获取网络设备信息

    pcap_if_t是一个interface数据结构,表明网络接口的信息.网络接口就是interface,就是我们用来上网的设备,一般为网卡,还有一些虚拟网卡也算作这样的接口.它的结构如下: struc ...

  2. 安卓手机上传同一张图片第二次不触发onchange

    清空上一次file内部的值  <script type="text/javascript"> var file = document.getElementById(&q ...

  3. django 的 MTV 流程图

  4. Hive-多分隔符

    ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe' WITH SERDEPROPERTIES (&qu ...

  5. 【C语言--数据结构】线性表链式存储结构

    直接贴代码 头文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__ typedef void LinkList; typedef struct _tag_L ...

  6. [转帖]mysql.sock的作用

    mysql.sock的作用 链接:http://blog.itpub.net/28602568/viewspace-1797619/ 标题:mysql.sock的作用 作者:lōττéry©版权所有[ ...

  7. net 架构师-数据库-sql server-003-T-SQL 基本语句

    3.1 基本SELECT语句 SELECT [ALL|DISTINCT] [TOP (<expression>)  [PERCENT] [WITH TIES]] <coloumn  ...

  8. Monkey常用命令详解

    使用monkey help 命令查看命令参数,如下: C:\Users\chenfenping>adb shell monkey -help usage: monkey [-p ALLOWED_ ...

  9. [BZOJ 2154]Crash的数字表格(莫比乌斯反演+数论分块)

    [BZOJ 2154]Crash的数字表格(莫比乌斯反演+数论分块) 题面 求 \[\sum_{i=1}^{n} \sum_{j=1}^{m} \mathrm{lcm}(i,j)\] 分析 \[\su ...

  10. 微信小程序获得微信头像和昵称

    微信小程序之登录态的探索 { wx.getSetting({ success: res => { if (res.authSetting && res.authSetting[' ...