HDU-6447-YJJ's Salesman(离散化+01dp,线段树维护)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447
Problem Description 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. Input The first line of the input contains an integer T (1≤T≤10) ,which is the number of test cases. In each case, the first line of the input contains an integer N (1≤N≤105) .The following N lines, the k -th line contains 3 integers, xk,yk,vk (0≤vk≤103) , which indicate that there is a village on (xk,yk) and he can get vk dollars in that village. Output The maximum of dollars YJJ can get. Sample Input 1 3 1 1 1 1 2 2 3 3 1 Sample Output 3 |
题目大意:在一个10^9*10^9的地图上,分布着10^5个村庄,一个人从0,0,处出发,前往10^9,10^9处,中间经过村庄可以获得村庄的宝藏,但是只有从村庄(x,y)的(x-1,y-1)坐标进入村庄才会获得宝藏,输出一个人从起点到终点能够获得的最多的宝藏;
一开始想的是DP,但是一看10^9的地图.....直接放弃,虽然想到离散化,但是不知道怎么离散化,结果到比赛结束也没有写出来..好菜,按照坐标离散化,之后建一个01dp;
状态转移方程:dp[x]=max(dp[x-1],dp[x]);用上线段树去维护这个dp
ac:
#include<stdio.h>
#include<string.h>
#include<math.h> #include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std; #define ll long long
#define INF 0x3f3f3f3f
//#define mod 1e9+7
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);const int MAXN=2e5+10;
const ll mod=1e9+7;
struct dot{int x,y;ll v;
}dots[MAXN];
ll tree[MAXN<<2];
ll dp[MAXN];
int num[MAXN];
int n;void intt()
{clean(dp,0);clean(tree,0);clean(num,0);
}bool cmp1(dot a,dot b)
{return a.x<b.x;
}bool cmp2(dot a,dot b)
{return a.y<b.y;
}void updata(int L,ll x,int l,int r,int rt)
{if(l==r){tree[rt]=max(x,tree[rt]);return ;}int mid=(l+r)>>1;if(L<=mid)updata(L,x,l,mid,rt<<1);elseupdata(L,x,mid+1,r,rt<<1|1);tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}ll Query(int L,int R,int l,int r,int rt)
{if(l>=L&&r<=R)return tree[rt];int mid=(l+r)>>1;ll ans=0;if(L<=mid)ans=max(ans,Query(L,R,l,mid,rt<<1));if(R>mid)ans=max(ans,Query(L,R,mid+1,r,rt<<1|1));return ans;
}int main()
{int T;scanf("%d",&T);while(T--){intt();scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d%d%lld",&dots[i].x,&dots[i].y,&dots[i].v);sort(dots+1,dots+n+1,cmp1);int temp=dots[1].x,id=1;//离散x dots[1].x=id;for(int i=2;i<=n;++i){if(dots[i].x==temp)dots[i].x=id;else{temp=dots[i].x;dots[i].x=++id;}}sort(dots+1,dots+1+n,cmp2);temp=dots[1].y,id=1;//离散y dots[1].y=id;num[id]=1;for(int i=2;i<=n;++i){if(dots[i].y==temp)dots[i].y=id;else{temp=dots[i].y;dots[i].y=++id;}num[id]++;}//离散化正确 id=0;int x,y;ll v;for(int i=1;i<=n;++i)//便利n个y {for(int j=1;j<=num[i];++j){int k=j+id;x=dots[k].x,y=dots[k].y,v=dots[k].v;if(x==1)//该位置的x是1,之前没有东西 dp[x]=v;else{ll ans1=Query(1,x-1,1,n,1)+v;ll ans2=Query(1,x,1,n,1);//cout<<ans1<<" "<<ans2<<endl;dp[x]=max(ans1,ans2);}}for(int j=1;j<=num[i];++j){int k=j+id;x=dots[k].x,y=dots[k].y,v=dots[k].v;updata(x,dp[x],1,n,1);//将该位置的dp值放入数组 }
// for(int j=1;j<=n<<2;++j)
// cout<<tree[j]<<" ";
// cout<<endl;
// for(int j=1;j<=n;++j)
// cout<<dp[j]<<" ";
// cout<<endl;id=id+num[i];}
// for(int i=1;i<=n<<2;++i)
// cout<<tree[i]<<endl;printf("%lld\n",tree[1]);}
}
HDU-6447-YJJ's Salesman(离散化+01dp,线段树维护)相关推荐
- HDU 6447 YJJ's Salesman(树状数组优化DP + 离散化)
HDU 6447 YJJ's Salesman 题目 给一个二维数组,从(0,0)走到(1e9, 1e9).每次只能走右,下,右下,三个方向.其中只有通过右下走到特定给出的点(村庄)时才会获得分值.问 ...
- HDU 6447 YJJ's Salesman(线段树+DP)
YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- hdu 4419 Colourful Rectangle (离散化扫描线线段树)
Problem - 4419 题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小. 比较明显,这题要从矩形面积并的方向出发.如果做过矩形面积并的题,用线段树 ...
- HDU - 6967 G I love data structure 线段树维护矩阵 + 细节
传送门 文章目录 题意: 思路: 题意: 给你两个长度为nnn的数组a,ba,ba,b,你需要完成如下四种操作: 思路: 思路还是比较简单的,首先建一颗线段树,线段树中维护a,b,a2,b2,aba, ...
- HDU 6447 YJJ's Salesman (dp+树状数组+莫干山算法)
题意:一个 1e9*1e9的方格,从(0,0)走到(1e9,1e9),有个方格有价值,特殊的经过方格可获得价值,每次只能向右.下.右下走,只有右下走到方格的才能获得价值,问最大获得的价值是多少 官方题 ...
- CCPC 2018网络预赛 hdu 6447 YJJ's Salesman
[题目链接] 题目意思 T组案例,给一个n,下面n行,每行三个数字(x,y,v)表示点(x,y)处的值为v,只有当从(x-1,y-1)走到(x,y)时,才能获得点(x,y)的v值,求从(0,0)走到( ...
- HDU 6447 YJJ's Salesman
题目传送门 代码: #include<bits/stdc++.h> using namespace std;#define lson rt<<1,l,mid #define r ...
- POJ 1177 Picture [离散化+扫描线+线段树]
http://poj.org/problem?id=1177 给若干矩形,求被覆盖的区域的周长. 将 y 坐标离散化后,按 x 坐标进行扫描.用线段树维护两个东西,当前竖线的叠加长度 len 和 条数 ...
- hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5511 题意:割一些边使得无向图变成不连通的,并且恰好割了两条给定生成树上的边.满足非树边两段一定在给定生成 ...
- HDU 2681 MM Programming Club(miaos的线段树维护+ycy的暴力贪心)
Description ACM is popular in HDU. Many girls want to learn more about programming skills in ACM. As ...
最新文章
- mysql 中文搜索插件_支持中文的MySQL 5.1+ 全文检索分词插件
- apk可以解压再复制到手机吗_不行了,这个打通手机和电脑的神器,必须得安利给你们...
- DM8168心得之SD卡快速分区制作
- Prim和Dijkstra算法的区别
- php常量数组吗,包含数组的PHP常量?
- BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
- Oracle expdp 导出
- 高斯主元素消去法c语言,列主元Gauss消去法(C++实现)
- 1002 C语言输入解决方案
- Marco:Filecash全网算力增加的趋势,将形成FIC价格上升的良性循环
- http与https的作用与区别
- 连接路由器后电脑连不上网_电信光纤猫与无线路由器连接怎么设置【图文教程】...
- 解决QML Window 增加radius效果
- 品读大话数据结构之六-----线性表(中)
- 高效获得准确的中国地图数据并进行可视化
- 韩顺平老师多用户即时通讯系统功能扩展:发送离线消息
- java打印设置_java如何设置系统默认打印机
- vivox27微信无法连接服务器,vivo X27微信拍照模糊怎么办?简单一个操作轻松解决...
- C语言链表的删除代码实现
- 51单片机实现简易闹钟(包含闹钟功能)