//#if 0
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <set>
#include <list>
#include <iostream>
using namespace std;
#define ll long long
#define ull unsigned long long
#define cls(x) memset(x,0,sizeof(x))
#define clslow(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
const int maxn=1e5+50;
priority_queue<ll ,vector<ll>,greater<ll> >q;
ll n,t,x;
int num[maxn];
int check(ll k,ll lim)
{
while(!q.empty())q.pop();
ll ans=0,tmp;
// cout<<"k= "<<k<<endl;
ll all=0;
int flag=(n-1)%(k-1);
if(flag>0){
flag++;
for(int i=1;i<=flag;i++) all+=num[i];
q.push(all);
}
for(int i=flag+1;i<=n;i++)q.push(num[i]);
while(q.size()>1){
ll len=q.size();
// cout<<" top = "<<q.top()<<endl;
for(int i=1;i<=k;i++){
tmp=q.top();
q.pop();
ans+=tmp;
}
all+=(ll)ans;
// cout<<all<<endl;
q.push(ans);
ans=0;
}
// cout<<" ans = "<<all<<endl;
if(all>lim)return 0;
return 1;
}
int main()
{
// freopen("in.txt","r",stdin);
int t0;
scanf("%d",&t0);
while(t0--)
{
while(!q.empty())q.pop();
scanf("%lld%lld",&n,&t);
for(int i=1;i<=n;i++){
scanf("%lld",&num[i]);
}
sort(num+1,num+1+n);
ll l=2,r=n;
while(l<r){
ll mid=(l+r)/2;
if(check(mid,t)==1){
r=mid;
// cout<<"mid == "<<mid<<endl;
}
else{
l=mid+1;
// cout<<mid<<endl;
}
}
printf("%lld\n",l);
}
return 0;
}
//#endif
hdu 5884(二分+优先队列)
最新推荐文章于 2023-01-10 21:58:11 发布