码迷,mamicode.com
首页 > 其他好文 > 详细

12. 17 哈理工网络赛

时间:2017-12-19 01:41:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:integer   equal   first   decide   lis   expec   ast   val   一个   

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 哈理工网络赛

标签:integer   equal   first   decide   lis   expec   ast   val   一个   

原文地址:http://www.cnblogs.com/ccut-ry/p/8058933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!