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 line consists of one positive 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 number which 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
题目分析 : 类似于LIS,也可以采取维护一个最大的和的序列,但是有一点不同的是,就是在后续插入一个值后,要讲该值后面的元素中键值大于插入的,但是和却小于插入的和的 全部删除掉, 这里用 map 来维护 。
代码示例 :
#include <cstdio> #include <iostream> #include <map> using namespace std; const int eps = 1e5+5; int a[eps], b[eps]; map<int, int>mp; int main (){ int t; int n; cin >> t; while(t--){ cin >> n; for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); } for(int i = 1; i <= n; i++){ scanf("%d", &b[i]); } mp.clear(); mp[0] = 0; map<int, int>::iterator it; int ans = -1; for(int i = 1; i <= n; i++){ for(it = mp.begin(); it != mp.end(); it++){ if (a[i] < it->first) break; } it--; int p = it->second + b[i]; if (mp.count(a[i])) { mp[a[i]] = max(mp[a[i]], p); } else mp[a[i]] = p; ans = max(ans, mp[a[i]]); for(it = mp.begin(); it != mp.end(); it++){ if (it->first > a[i] && it->second <= mp[a[i]]) { mp.erase(it); it--; // 坑啊,删除一个元素后,it 所指向的是删除元素的下个位置,这样以来的话,出这个循环it在加加,就会造成无线循环的局面 } } } // for(it = mp.begin(); it != mp.end(); it++){ // printf("%d %d\n", it->first, it->second); // } printf("%d\n", ans); } return 0; } /* 10 7 3 7 2 9 6 8 5 1 1 2 1 1 2 2 */