P3611 [USACO17JAN] Cow Dance Show S题解
题目描述
题目传送门这道题简化过来就是有\(n\)个奶牛要跳舞,每次可以让\(k\)个奶牛跳,跳完一个换下一个跳,每个奶牛都有一个跳的时间,求在满足一定时间内\(k\)的最小值
题目分析
这道题我们可以发现可以用二分答案求k,然后\(check\)函数里判断满不满足要求,我们可以让跳的时间越久的时间的奶牛先跳,所以我们就可以使用优先队列即可
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int n,t,l,r,a[N];
bool check(int k){
priority_queue < int,vector<int>,less<int> > q;
for(int i=1;i<=k;i++) q.push(a[i]);
for(int i=k+1;i<=n;i++){
int to=q.top();
q.pop();
q.push(to+a[i]);
}
int time;
while(!q.empty()){
time=q.top();
q.pop();
}
if(time>t) return 0;
else return 1;
}
int main(){
cin>>n>>t;
for(int i=1;i<=n;i++) cin>>a[i];
l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)) r=mid-1;
else l=mid+1;
}
cout<<l;
return 0;
}

浙公网安备 33010602011771号