hdu5884

#include <cstdio>

#include <queue>

#include <algorithm>

#include <string.h>
//#include <bits/stdc++.h>

using namespace std;

const int maxn=100000;

int t;

int a[maxn+10];

int sum[maxn+10];

int n,m;

bool judge(int mid){
    priority_queue<int,vector<int>,greater<int> > q;
    int cnt=0;
    int s=(n-1)%(mid-1);
    if(s){
        s++;
        int temp=sum[(n-1)%(mid-1)+1];
        q.push(temp);
        cnt+=temp;
    }
    for(int i=s+1;i<=n;i++){
        q.push(a[i]);
    }
    while(q.size()!=1){
         int temp=0;
         for(int i=1;i<=mid;i++){
            if(!q.empty()){
            temp+=q.top();
            q.pop();
            } else {
               break;
            }
        }
        q.push(temp);
        cnt+=temp;
    }
    return cnt<=m;
}

int solve(){
  int lb=2,ub=n;
  while(ub-lb>1){
       int mid=(ub+lb)>>1;
       if(judge(mid)) ub=mid;
       else lb=mid;
  }
  if(judge(lb)){
      return lb;
  } else return ub;
}

int main()
{
    scanf("%d",&t);
    while(t--){
       sum[0]=0;
       scanf("%d%d",&n,&m);
       if(n<=1){
         printf("1\n");
         continue;
       }

       for(int i=1;i<=n;i++){
           scanf("%d",&a[i]);
           sum[i]=sum[i-1]+a[i];
       }
       sort(a+1,a+1+n);
       for(int i=1;i<=n;i++){
           sum[i]=sum[i-1]+a[i];
       }
       //printf("%d\n",judge(3));
       int ans=solve();
       printf("%d\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2016-10-04 10:47  GeniusYang  阅读(131)  评论(0编辑  收藏  举报