在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。 第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。 但是这些士兵有特殊的要求:如果选了第i个士兵,这个

链接:https://ac.nowcoder.com/acm/contest/1080/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。
第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。
但是这些士兵有特殊的要求:如果选了第i个士兵,这个士兵希望团的人数不超过s[i]。(如果不选第i个士兵,就没有这个限制。)
tokitsukaze想知道,团的战力最大为多少。
输入描述:

第一行包含一个正整数n(1≤n≤10^5)。
接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。

输出描述:

输出一个正整数,表示团的最大战力。

示例1
输入

2
1 2
2 2

输出

3

示例2
输入

3
1 3
2 3
100 1

输出

100

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;

struct node
{
	int v, s;
}a[100008];

bool comp(node a,node b)
{
	return a.s > b.s;
}
int main()
{
	multiset<int> S;
	int n;
	long long ans = 0, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].v >>a[i].s;
	}
	sort(a, a + n, comp);
	for (int i = 0; i < n; i++)
	{
		S.insert(a[i].v);
		sum += a[i].v;
		while (S.size() > a[i].s)
		{
			sum -= *S.begin();
			S.erase(S.begin());
		}
		ans = max(ans, sum);
	}
	cout << ans;
	return 0;
}
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
	int x, y;
}a[100008];

bool comp(node u, node v)
{
	return u.y > v.y;
}

int main()
{
	priority_queue<int,vector<int>,greater<int> > S;
	int n;
	long long ans = 0, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].x >> a[i].y;
	}
	sort(a, a + n, comp);
	for (int i = 0; i < n; i++)
	{
		S.push(a[i].x);
		sum += a[i].x;
		while (S.size() > a[i].y)
		{
			sum -= S.top();
			S.pop();
		}
		ans = max(ans, sum);
	}
	cout << ans;
	return 0;
}
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
	int x, y;
	bool operator<(const node &v)const
	{
		return x>v.x;
	}
}a[100008];

bool comp(node u, node v)
{
	return u.y > v.y;
}

int main()
{
	priority_queue<node> S;
	int n;
	long long ans = 0, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].x >> a[i].y;
	}
	sort(a, a + n, comp);
	for (int i = 0; i < n; i++)
	{
		S.push(a[i]);
		sum += a[i].x;
		while (S.size() > a[i].y)
		{
			sum -= S.top().x;
			S.pop();
		}
		ans = max(ans, sum);
	}
	cout << ans;
	return 0;
}
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
	int x, y;
}a[100008];

bool comp(node u, node v)
{
	return u.y > v.y;
}

struct cmp1
{
	bool operator()(const node& u, const node& v)const
	{
		return u.x > v.x;
	}
};

int main()
{
	priority_queue<node,vector<node>,cmp1> S;
	int n;
	long long ans = 0, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].x >> a[i].y;
	}
	sort(a, a + n, comp);
	for (int i = 0; i < n; i++)
	{
		S.push(a[i]);
		sum += a[i].x;
		while (S.size() > a[i].y)
		{
			sum -= S.top().x;
			S.pop();
		}
		ans = max(ans, sum);
	}
	cout << ans;
	return 0;
}
  • 第一个程序用multiset容器,默认从小到大排序。
  • 第二个程序用priority_queue,其默认为大根堆,这里通过priority_queue<int,vector,greater > S改为小根堆。默认的大根堆参数为priority_queue<int,vector,less > S.另外这里的数据类型是基本数据类型。
  • 第三个程序的数据类型是自定义的结构体,可以采用程序中的方法定义小根堆(重载<)。
  • 第四个程序是将定义小根堆的方法写在了结构体外面( 重载() )。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值