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

来源:互联网 发布:最新淘宝店铺教程视频 编辑:程序博客网 时间:2024/05/01 08:44
Aggie’s TasksTime Limit: 1000 MSMemory Limit: 262144 KTotal Submit: 5(4 users)Total Accepted: 1(1 users)Rating: Special Judge: NoDescription

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);}}

阅读全文
0 0
原创粉丝点击