12. 17 哈理工网络赛

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12. 17 哈理工网络赛相关的知识,希望对你有一定的参考价值。

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 哈理工网络赛的主要内容,如果未能解决你的问题,请参考以下文章

哈理工2015暑假训练赛 zoj 2078Phone Cell

哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

记TJPUのACM新生赛——Stay young, stay simple

河南理工大学算法协会暑期集训积分赛网络同步赛-Numbers of interval-尺取法

哈理工东区大四毕业大清仓(自觉收藏 有效期到6.28)

哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-高年级组)D - 数圈圈