二分答案模版题

题目描述:

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

题意:

n件衣服,每件衣服都有一定的含水量,每分钟每件衣服的含水量会减少1,你有一个吹风机,每分钟只能对一个衣服使用吹风机,且这分钟过完了,衣服的含水量会减少k,问你最少要多少分钟能让所有衣服的含水量非正

思路:

二分的入门题,去二分所需的分钟

二分最重要的就是check函数怎么写,这个题的话,我们计算x分钟时每个衣服需要吹几分钟的吹风机,然后加起来和k比较即可

坑点:

使用吹风机的时候总共让一件衣服的水少k,这个k包含了一分钟内水自然蒸发掉的那个1

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
using namespace std;
#define inf 0x3f3f3f3f
#define MAX 200000 + 50
#define endl '\n'
#define seed 13331
#define mod 1000000007
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
typedef  long long ll ;
//不开longlong见祖宗!
//inline __int128 read(){__int128 x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-'){f = -1;}ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
//inline void print(__int128 x){if(x < 0){putchar('-');x = -x;}if(x > 9){print(x / 10);}putchar(x % 10 + '0');}
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}
inline void write(int x){if (x < 0) {x = ~x + 1; putchar('-');}if (x > 9){write(x / 10);}putchar(x % 10 + '0');}

ll n, k;
ll tr[MAX];

bool judge(ll t){
    ll sum = 0;
    for(int i = 1; i <= n; ++i){
        if(tr[i] <= t)continue;
        else {
            sum += (tr[i] - t + k - 2) / (k - 1);
        }
    }
    if(sum > t)return false;
    return true;
}

int main(){
    io;
    n = IntRead();
    ll l = 1, r = 0;
    for(int i = 1; i <= n; ++i){
        tr[i] = IntRead();
        r = max(r, tr[i]);
    }
    k = IntRead();
    if(k == 1){
        cout<<r<<endl;
    }
    else {
        r = inf;
        while (l <= r) {
            ll mid = (l + r) / 2;
            if(judge(mid))r = mid - 1;
            else l = mid + 1;
        }
        cout<<l<<endl;
    }
    return 0;
}