Gym - 101889F Fundraising(树状数组求带权最长上升子序列)

版权声明:本文为博主原创文章,转载请附上注明就行_(:з」∠)_。 https://blog.csdn.net/vocaloid01/article/details/82953763

ICPC Latin American Regional – 2017

Problem F – Fundraising

Author: Paulo Cezar Pereira Costa, Brasil

A prestigious politician aiming for presidency next year is planning a fundraising dinner for her cam-paign. She has a list of some wealthy people in the country and wants to invite them in a way that the amount of money raised is as great as possible.

Sometimes wealthy people have futile behavior and don’t like the idea that someone richer or prettier than them exists. Every time someone like this meets another person who is strictly prettier, but not strictly richer, then an argument ensues. Likewise, if they meet another person who is strictly richer,but not strictly prettier, an argument occurs as well. These two situations are the only possible causes of an argument involving two persons. Thus, two persons do not have an argument if one of them is strictly prettier and strictly richer than the other. Also, two persons do not have an argument if they are equally rich and equally pretty.Since the presidential candidate wants to raise as much money as possible, an argument should be avoided at all costs, as it could ruin the campaign. Given the characteristics of some wealthy people in the country, you must find a guest list that maximizes the donations while ensuring that no argument will happen during the dinner.

Input

The first line contains an integer N(1≤N≤10^5) representing the number of possible guests with known characteristics. Each of the next N lines describes a possible guest with three integers B,F and D(1≤B, F, D≤10^9), indicating respectively the person’s beauty, his/her fortune, and how much this person will donate if invited.

Output

Output a single line with an integer indicating the maximum sum of donations if guests are invited so that no argument will happen during the dinner.

Sample input 1

4

1 2 50

2 1 50

2 2 30

1 1 30

Sample output 1

60

Sample input 2

3

3 3 3

5 5 3

2 2 3

Sample output 2

9

Sample input 3

3

2 8 13

1 4 12

2 1 16

Sample output 3

25

题意:

每个人有两个属性值,以及一个权值,如果两个人的属性值都相同或者某个人的两个属性值分别大于另一个人,那么这两个人可以同时选出来,求选出一些人使得权值和最大。

题解:

先把两个属性值都相同的人合并,然后按第一个属性值从小到大排序,这样就能变成一维问题。需要注意的是当第一个属性值相同时第二个属性值要按从大到小排序(巧妙)。然后就是裸的带权最长上升子序列,肯定是用树状数组没跑了。

代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

long long tree[MAXN];
int len;

int bin[MAXN];//用于二分 

int lowbit(int t)
{
    return t&(-t);
}

void Updata(int index,long long value)
{
    for(int i=index ; i<=len ; i+=lowbit(i))
    {
        tree[i] = max(tree[i],value);
    }
}

long long Query(int index)//区间查询最大值 
{
    long long sum = 0;
    for(int i=index ; i>0 ; i-=lowbit(i))
    {
        sum = max(sum,tree[i]);
    }
    return sum;
}

struct cmp_key//map自定义排序 
{
	bool operator()(const pair<int,int> &k1,const pair<int,int> &k2) const
	{
		if(k1.first != k2.first)
		{
			return k1.first < k2.first;
		}
 		else return k1.second > k2.second;
 
		return false;
	}
};

int Bin(int x){
	int l = 1,r = len;
	while(l <= r){
		int m = l + (r-l)/2;
		if(bin[m] == x)return m;
		else if(bin[m] > x)r = m-1;
		else l = m+1;
	}
	return -1;
}

map<pair<int,int>,long long,cmp_key> MMP;

inline void init(){
	MMP.clear();
	memset(tree,0,sizeof tree);
}

int main(){
	
	int N;
	while(scanf("%d",&N) == 1){
		init();
		int a,b;
		long long v;
		for(int i=1 ; i<=N ; ++i){
			scanf("%d %d %lld",&a,&b,&v);
			if(MMP.find(make_pair(a,b)) == MMP.end())
				MMP[make_pair(a,b)] = v;
			else MMP[make_pair(a,b)] += v;
			bin[i] = b;
		}
		map<pair<int,int>,long long,cmp_key>::iterator it;
//		for(it=MMP.begin() ; it!=MMP.end() ; ++it){
//			printf("%d %d %lld\n",(*it).first.first,(*it).first.second,(*it).second);
//		}
		sort(bin+1,bin+1+N);
		len = unique(bin+1,bin+1+N)-(bin+1);//去重 
		for(it=MMP.begin() ; it!=MMP.end() ; ++it){
			int ind = Bin((*it).first.second);
			long long t = Query(ind-1);
			Updata(ind,t+(*it).second);
		}
		printf("%lld\n",Query(len));
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/vocaloid01/article/details/82953763