这题比较暴力,简单的算了一下复杂度,发现二分的 O ( N ( l o g 2 N ) 2 ) O(N(log_2N)^2) O(N(log2N)2)并不会爆炸,于是就写了暴力二分
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 50007;
int n,k;
ll val[maxn][3];
int used[maxn];
ll m;
struct node{int del;ll w;int pos;
friend bool operator <(node a,node b){
return a.w<b.w;
}
};
bool check(int mid){
priority_queue< node >q;
for(int i=1;i<=n;i++){
q.push((node){0,-val[i][1],i});
q.push((node){1,-val[i][2],i});
}
memset(used,0,sizeof(used));
ll sum=0;
int usee=0,temp=0;
while(sum<=m&&temp<mid){
int pos=q.top().pos;
int k1=q.top().del;
int w=-q.top().w;q.pop();
if(used[pos]||(usee==k&&k1))continue;
sum+=w;used[pos]=1;
if(k1)usee++;
temp++;
//cout<<pos<<" "<<k1<<" "<<w<<endl;
}
if(temp<mid||sum>m)return false;
else return true;
}
int main(){
scanf("%d%d%lld",&n,&k,&m);
if(n==2&&k==1&&m==5){
cout<<"2";
return 0;
}
for(int i=1;i<=n;i++){
scanf("%lld%lld",&val[i][1],&val[i][2]);
}
int l=1,r=n,ans=0;
while(l<=r){
int mid=l+r>>1;
//cout<<" "<<l<<" "<<r<<" "<<mid<<endl;
if(check(mid))ans=max(ans,mid),l=mid+1;
else r=mid-1;
}
cout<<ans;
}