请求DEBUG
查看原帖
请求DEBUG
922691
Hisy楼主2024/7/15 09:29
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
priority_queue<int,vector<int>,greater<int>> q1;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q2,q3;
pair<int,int> a[50005];
ll m;
int n,k;
int mp[50005];
inline bool compare(pair<int,int> x,pair<int,int> y){
	return x.second<y.second;
}
int main(){
	scanf("%d %d %lld",&n,&k,&m);
	for(int i=1;i<=n;++i){
		scanf("%d %d",&a[i].first,&a[i].second);
	}
	sort(a+1,a+1+n,compare);
	ll sum=0;
	for(int i=1;i<=k;++i){
		sum+=a[i].second;
		if(sum>m){
			printf("%d",i-1);
			return 0;
		}
		q1.push(a[i].first-a[i].second);
	}
	if(k==n){
		printf("%d",n);
		return 0;
	}
	for(int i=k+1;i<=n;++i){
		q2.push(make_pair(a[i].second,i));
		q3.push(make_pair(a[i].first,i));
	}
	int ans=k;
	for(int i=k+1;i<=n;++i){
		while(mp[q2.top().second]){
			q2.pop();
		}
		while(mp[q3.top().second]){
			q3.pop();
		}
		int t1=q2.top().second;
		int t2=q3.top().second;
		int t3=q2.top().first+q1.top();
		int t4=q3.top().first;
		if(t3<t4){
			sum+=t3;
			q1.pop();
			q2.pop();
			q1.push(a[t1].first-a[t1].second);
		}else{
			sum+=t4;
			q3.pop();
			mp[t2]=1;
		}
		++ans;
		if(sum>m){
			printf("%d",ans-1);
			return 0;
		}
	}
	printf("%d",n);
	return 0;
}
2024/7/15 09:29
加载中...