NC50439

2 篇文章 0 订阅
2 篇文章 0 订阅

NC50439

题意

有n个士兵,每个士兵战斗力为v[i],添加这个士兵则上场的总士兵数量要小于s[i],求最高战斗力?
n(1≤n≤10^5)
v,s(1≤v≤10^9,1≤s≤n)

思路

贪心+优先队列或multiset
一开始想到01背包变形,如果按照这个思路去解,会发现随着s[i]的变化(即背包的容量变化)。
如果有会dp解的大佬麻烦发我一下代码,我学一下。
这道题如果人数k是已知的,直接贪心取s[i]>=k中的最大的k个人的战斗力即可。
问题在于k是不确定的,那我们考虑优化,枚举k(从s[i]中枚举,并非连续)。
现在考虑从大到小枚举还是从小到大枚举方便。
我们从大到小枚举,优先队列或multiset中放入被选中的士兵,不断维护即可。

p r i o r i t y _ q u e u e priority\_queue priority_queue

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e5 + 5;

struct node{
	ll x,y;
	bool operator < (const node &T){return y>T.y;}//s[i]大的放前面
}a[maxn];

priority_queue<ll,vector<ll>,greater<ll> >q;//默认优先队列是大的在前小的在后,即大根堆,这里要取小根堆,所以要用这种方式书写

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].y;
	}
	sort(a+1,a+1+n);
	ll ans=0,res=0;
	for(int i=1;i<=n;i++){
		ans+=a[i].x;
		q.push(a[i].x);
		while(q.size()>a[i].y){//维护选中的人数始终不超过限制人数
			ans-=q.top();
			q.pop();
		}
		res=max(res,ans);
	}
	cout<<res;
	return 0;
}

m u l t i s e t multiset multiset

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e5 + 5;

struct node{
	ll x,y;
	bool operator < (const node &T){return y>T.y;}
}a[maxn];

multiset<ll>s;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].y;
	}
	sort(a+1,a+1+n);
	ll ans=0,res=0;
	for(int i=1;i<=n;i++){
		ans+=a[i].x;
		s.insert(a[i].x);
		while(s.size()>a[i].y){
			ans-=*s.begin();
			s.erase(s.begin());
		}
		res=max(res,ans);
	}
	cout<<res;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值