[hdoj6447] YJJ's Salesman - CheaSim Blog

[hdoj6447] YJJ's Salesman

[hdoj6447] YJJ’s Salesman

题意

给定一个地图,只能向右或者向上走,地图上有很多点,有以下条件

  • 每个点有钱$val$
  • 你只能从该点的左下方进入点才可以拿钱。

问最多能获得多少钱。

题解

dp + 树状数组优化

单dp就是$O(n^2)$的复杂度。但是题目数据范围$1e5$所以不行。只有树状数组优化了。

树状数组处理的是$dp[i]$代表着以$i$点为重点的权值。

并且由于$x,y$范围是$1e9$所以必须离散化。

有一步我看了好久代码才看懂。在dp方程转移的时候,先对于$x$进行排列,之后在之后更大的$x$再更新之后的dp数组,保证每一次update的dp数组都是最优的。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define per(i,a,n) for(int i=(n-1);i>=(a);i--)
#define fi first
#define se second
typedef pair <int,int> pII;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//head
const int maxn = 1e5 + 10;
struct node{
int x,y,val;
bool operator <(const node& t)const{
return x < t.x;
}
}a[maxn];
int T,n,tot;
int bit[maxn],dp[maxn];
int lowbit(int x){
return x&-x;
}
void update(int x,int val){
for(int i=x;i<=tot;i+=lowbit(i)) bit[i] = max(bit[i],val);
}
int query(int x){
int s = 0;
for(int i=x;i;i-=lowbit(i)){
s = max(s,bit[i]);
}
return s;
}
int main(){
#ifdef LOCAL
freopen("2.in","r",stdin);
#endif
scanf("%d",&T);
while(T--){
vector<int> v;
memset(bit,0,sizeof(bit));
scanf("%d",&n);
rep(i,0,n){
int x,y,v;scanf("%d%d%d",&x,&y,&v);
a[i].x = x;a[i].y = y;a[i].val = v;
}
rep(i,0,n) v.push_back(a[i].y);
sort(v.begin(),v.end());
sort(a,a+n);
v.erase(unique(v.begin(),v.end()),v.end());
tot = v.size();
rep(i,0,n) a[i].y = lower_bound(v.begin(),v.end(),a[i].y) - v.begin() + 1;
rep(i,0,n) dp[i] = a[i].val;
int ans = 0;int pos = 0;
rep(i,0,n){
while(pos < i && a[pos].x < a[i].x){
update(a[pos].y,dp[pos]);
pos ++;
}
dp[i] = query(a[i].y-1) + a[i].val;
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
作者

CheaSim

发布于

2018-08-27

更新于

2018-08-27

许可协议

You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

评论