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. MySQL-快速入门(10)触发器

    1.什么是触发器 触发器是与表有关的命名数据库对象.触发器由事件来触发某个操作. 触发器是特殊的存储过程,触发器不需要call语句来调用,也不需要手工启动,只需要确定一个预定义的事件发生的时候,就会被 ...

  2. java基础笔记)(5)

    xml文件:树形存储格式:通过相同的xml文件实现不同的软件.不同的操作系统.不同的平台之间的信息通讯: 声明xml文件: <?xml version="1.0" encod ...

  3. P1067多项式输出

    这道题是2009普及组的题,仍然是一个字符串+模拟.(蒻到先不刷算法) 这道题的题干给了很多的提示,也很全面,但是当我把种种情况都考虑到了后,在写代码的过程中仍然出现了很多的错误,wa了三四次.其实导 ...

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

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

  5. Structs2下的MyFirstTest

    1.这是<Struts2-权威指南>第二章的例子 2.博文主要说明在eclipse下如何创建一个struts2项目 3.实现功能:在login.jsp输入用户名和密码,若用户名为scott ...

  6. spring boot 枚举使用的坑2

    上一篇说到在枚举当在controller的方法做参数时的坑,解决方法是配置了一个converter,后来想想,如果不闲每次都加一个注解麻烦的话,可以在参数前面加一个注解,添加一个解析器应该也可以解决这 ...

  7. css控制文本内容显示省略号

    1,单行文字显示省略号 div{ width:200px; overflow:hideen; white-space:nowrap; text-overflow:ellipsis; } 2,多行文字显 ...

  8. Hadoop本地模式搭建

    官方文档,不同版本修改url地址中的数字即可 http://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-common/Single ...

  9. Python 项目转化为so文件

    思路是先将py转换为c代码,然后编译c为so文件,所以要安装以下内容: python 安装:cython pip install cython linux 安装:python-devel,gcc yu ...

  10. window下lamp环境搭建

    软件: apache_2.2.25.msi php-5.4.30-Win32-VC9-x86.zip mysql-5.6.11-win32.msi下载地址:http://download.csdn.n ...