原理

看到这个题,最直接的解法肯定应该枚举晾干所用时间time,然后判断当前的time是否足够衣服完全晾干。
判断方法为:
设当前晾干所用时间为time,晾干第i件衣服要用烘干机xi分钟,自然晾干时间为time-xi,则烘干第i件衣服需要满足:A(time-xi)+xi(A+B)>=clothes[i],解得:xi>=ceil((clothes[i]-A*time)/B)(ceil为取上限)
判断sum(x)是否<=time即可判断time时间是否可以晾干所有衣服

  1. bool isok(int time){
  2. int col=0;
  3. for(int i=0;i<n;i++){
  4. if(clothes[i]-A*time<=0)//注意这里要特判一下(大家可以思考一下为什么)
  5. continue;
  6. if((clothes[i]-A*time)%B==0){
  7. col+=(clothes[i]-A*time)/B;
  8. }else{
  9. col+=(clothes[i]-A*time)/B+1;
  10. }
  11. }
  12. return col<=time;
  13. }

但枚举time会发现超时了,我们这是应该想到:枚举->二分
这样我们就不用从小到大一个一个地枚举,直接将时间复杂度降低为O(log2n)

完整代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,A,B,clothe;
  4. vector<int>clothes;
  5. bool isok(int time){
  6. int col=0;
  7. for(int i=0;i<n;i++){
  8. if(clothes[i]-A*time<=0)
  9. continue;
  10. if((clothes[i]-A*time)%B==0){
  11. col+=(clothes[i]-A*time)/B;
  12. }else{
  13. col+=(clothes[i]-A*time)/B+1;
  14. }
  15. }
  16. return col<=time;
  17. }
  18. int main(){
  19. int max_clothe=0;
  20. cin>>n>>A>>B;
  21. for(int i=0;i<n;i++){
  22. cin>>clothe;
  23. clothes.push_back(clothe);
  24. max_clothe=max(max_clothe,clothe);//更新最大衣服水量
  25. }
  26. //将最大衣服水量作为二分的最大值
  27. int l=1,r=max_clothe,mid,ans;
  28. while(l<=r){
  29. mid=(l+r)/2;
  30. if(isok(mid))
  31. ans=mid,r=mid-1;
  32. else
  33. l=mid+1;
  34. }
  35. cout<<ans<<endl;
  36. //system("pause");
  37. return 0;
  38. }
点赞(0)
 

9.9 分

3 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论