科林明伦杯哈尔滨理工大学第七届程序设计团队赛-Aggie’s Tasks

Aggie’s Tasks
Time Limit: 1000 MSMemory Limit: 262144 K
Total Submit: 5(4 users)Total Accepted: 1(1 users)Rating: Special Judge: No
Description

Aggie is faced with a sequence of tasks, each of which with a difficulty value di and an expected profit pi. For each task, Aggie must decide whether or not to complete it. As Aggie doesn’t want to waste her time on easy tasks, once she takes a task with a difficulty di, she won’t take any task whose difficulty value is less than or equal to di.

Now Aggie needs to know the largest profits that she may gain.

Input

The first lineconsists of onepositive integer t (t 10), which denotes the number of test cases.

For each test case, the first line consists one positive integer n (n 100000), which denotes the number of tasks. The second line consists of n positive integers, d1, d2, …, dn (di  100000), which is the difficulty value of each task. The third line consists of n positive integers, p1, p2, …, pn (pi  100), which is the profit that each task may bring to Aggie.

Output

For each test case, output a single numberwhich is the largest profits that Aggie may gain.

Sample Input

1

5

3 4 5 1 2

1 1 1 2 2

Sample Output

4

Source

“科林明伦杯”哈尔滨理工大学第七届程序设计团队赛


题意为,有n个任务,每个任务有各自的价值和利润,必须按顺序做任务并且下一步只能做价值比以前高的任务,问能带来的最大利润。

带权上升子序列权值和的最大值,带权LIS,需要用线段树维护DP过程,后来觉得好像树状数组也可以,随性来了一发。

与普通树状数组不同的是,在单点更新和区间查询时,维护过程为寻找目前状态所能得到的最大值,也就是在增加数据时,看一遍后面跟这个点有关的点的值是否小于这个数据,如果小于则换掉,在差区间和时,也是寻找前方所存在的最大值的过程。

#include <bits/stdc++.h> 
using namespace std;
const int N = 1e5+10;
int t, n, ans, mx;
int d[N], p[N], tree[N], dp[N];
void add(int pos, int val) {
	while(pos <= mx) {
		tree[pos] = max(tree[pos], val);
		pos += pos&(-pos);
	}
}
int maxx(int pos) {
	int res = 0;
	while(pos) {
		res = max(res, tree[pos]);
		pos -= pos&(-pos);
	}
	return res;
}
int main() {
	scanf("%d", &t);
	while(t--) {
		ans = mx = 0;
		scanf("%d", &n);
		memset(tree, 0, sizeof(tree));
		for(int i = 1; i <= n; i++) {
			scanf("%d", &d[i]);
			mx = max(mx, d[i]);
		}
		for(int i = 1; i <= n; i++) scanf("%d", &p[i]);
		for(int i = 1; i <= n; i++) {
			dp[i] = maxx(d[i]-1) + p[i];
			ans = max(ans, dp[i]);
			add(d[i], dp[i]);
		}
		printf("%d\n", ans);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值