[luogu1843]奶牛晒衣服

标准的二分答案。

首先考虑暴力做法,即枚举最终答案ans,对于第一个可行的ans一定是最优解(在ans时间内可以烘干在ans+t(t>=0)的时间内也就一定可以烘干)由于N<=500000的数据范围绝对会TLE,因此需要优化。

接下来考虑优化,由于之前提到的性质,设f(i)表示在i时间内有无可能烘干,那么f数组必定满足**单调性**。那么我们可以二分最终答案ans,对于每一个ans对其进行暴力check:对于每一件衣服,如果它能在ans天内烘干(湿度<=ans*a),那么就不必使用烘干机,接下来处理必须使用烘干机的情况,当i号衣服不能被直接烘干时,我们需要用((clothes[i]-a*mid)%b==0?(clothes[i]-a*mid)/b:(clothes[i]-a*mid)/b+1)烘干机进行烘干(实际写代码的过程中我更推荐if-else),最后只需看烘干机使用总数是否小于等于mid即可。

下面给出参考代码:

 1 #include<iostream>
 2 #define int long long
 3 using namespace std;
 4 int n,a,b,clothes[600006];
 5 bool check(int mid)
 6 {
 7     int tot=0;
 8     for(int i=1;i<=n;i++)
 9     {
10         if(clothes[i]<=mid*a)continue;
11         else 
12         {
13             
14             if((clothes[i]-mid*a)%b==0)tot+=(clothes[i]-mid*a)/b;
15             else tot+=((clothes[i]-mid*a)/b+1);
16         }
17     }
18     return tot<=mid;
19 }
20 signed main()
21 {
22     ios::sync_with_stdio(0);
23     cin>>n>>a>>b;
24     for(int i=1;i<=n;i++)cin>>clothes[i];
25     int l=0,r=1e9,mid;
26     while(l<=r)
27     {
28         mid=(l+r)/2;
29         if(check(mid))r=mid-1;
30         else l=mid+1;
31     }
32     cout<<l<<endl;
33     return 0;
34 } 
View Code
原文地址:https://www.cnblogs.com/szmssf/p/11182302.html