思路:二分K,查看当前K能否满足总时间不超过最大时间
ACcode:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e4+10;
int n,tmax,a[N];
bool check(int x) {
priority_queue < int, vector < int >, greater < int > > q;
int y=0,ans=0;
for(int i=1; i<=x; i++) q.push(a[i]);
for(int i=x+1; i<=n; i++) {
ans+=q.top()-y;
if(ans>tmax)return false;//小剪枝
y=q.top();
q.pop();
q.push(a[i]+y);
}
for(int i=1; i<=x; i++) {
ans+=q.top()-y;
if(ans>tmax)return false;//小剪枝
y=q.top();
q.pop();
}
return ans<=tmax;
}
void solve() {
cin>>n>>tmax;
for(int i=1; i<=n; i++) cin>>a[i];
int l=0,r=n+1;
while(l+1<r) {
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid;
}
cout<<r<<"\n";
}
signed main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
solve();
return 0;
}
注释:
over~