P1843 奶牛晒衣服

题目分析:

  • 数据范围:

  • 为了避免踩坑,用long long型
  • 求解弄干所有衣服的时间
  • 二分答案区间[l,r] (l = 1,r = inf)

注意:

  • 踩坑了,int改半天,w[i] - a * x里面乘以x可能爆int,用long long啊

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>

using namespace std;

#define inf 0x3f3f3f3f
#define eps 1e-6
#define db double
#define ll long long
#define  mm(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define pb push_back
#define el endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define fgx cerr<<"-------------------------"<<endl
#define shutio ios::sync_with_stdio(false),cin.tie(0)
#define  mk make_pair
#define lowbit(x) (x) & (-x)
#define fi first
#define se second

const int N = 1e6 + 10;

ll n,a,b;
ll w[N];
ll l,r;

bool check(ll x){
    ll tot = 0;
    for(int i = 0; i < n; i ++ ){
        ll t = w[i] - a * x;
        if(t > 0){
            if(t % b == 0) tot += t/b;
            else tot += t/b + 1; 
        }
    }
    return tot<=x;    
}

int main() {
    shutio;
    cin >> n >> a >> b;
    ll l = 1,r = inf;    
    for(int i = 0; i < n; i ++ ){
        cin >> w[i];
    }
    while(l < r){
        ll mid = (l + r) >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    cout<<l;                
    return 0;
}