【2018ccpc区域赛网络赛】【hdu6447 YJJ's Salesman】【dp+离散化+树状数组/线段树优化】

4 篇文章 0 订阅
3 篇文章 0 订阅

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=6447

分析:二维坐标排序,x->大,y->小,由于我们每次走必须x,y均变大,那么相当于只要考虑排序后的y的值。从左往右考虑y,dp[i]=max(dp[j])+val[i](i表示第i个点),由于y的数据范围为1e9,需要离散化,然后用树状数组维护求最大。

代码:

#pragma warning(disable:4996)
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, int>pdi;
typedef long long ll;
#define CLR(a,b) memset(a,b,sizeof(a))
#define _for(i, a, b) for (int i = a; i < b; ++i)
const int mod = (int)1e9 + 7;
const long double eps = 1e-10;
const int maxn = 1e5 + 7;
const int INF = 0x3f3f3f3f;

struct node {
	ll x, y, z;
}k[maxn];

ll c[maxn];

ll lowbit(ll i) {
	return i & (-i);
}

ll getsum(ll x) {
	ll res = 0;
	while (x) {
		res =max(res, c[x]);
		x -= lowbit(x);
	}
	return res;
}

void add(ll x, ll v) {
	while (x <= 1e5) {
		c[x] = max(c[x], v);
		x += lowbit(x);
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		CLR(c, 0);
		ll n;
		scanf("%lld", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%lld%lld%lld", &k[i].x, &k[i].y, &k[i].z);
		}
		sort(k + 1, k + 1 + n, [](const node &l, const node &r) {
			return l.y < r.y;
		});
		int cnt = 1;
		for (int i = 1; i <= n; ++i) {
			if (k[i].y != k[i + 1].y)
				k[i].y = cnt++;
			else
				k[i].y = cnt;
		}
		sort(k + 1, k + 1 + n, cmp);
		ll maxv = 0;
		for (int i = 1; i <= n; i++) {
			int tmp = getsum(k[i].y - 1) + k[i].z;//同行不能加,故要减1
			add(k[i].y, tmp);
		}
		printf("%lld\n", getsum(n));
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值