1. 程式人生 > >12. 17 哈理工網絡賽

12. 17 哈理工網絡賽

ons seq expect may lis tput hose include clu

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
*/

  

12. 17 哈理工網絡賽