poj3104 Drying(二分最大化最小值 好题)

时间:2021-09-14 11:32:26

https://vjudge.net/problem/POJ-3104

一开始思路不对,一直在想怎么贪心,或者套优先队列。。

其实是用二分法。感觉二分法求最值很常用啊,稍微有点思路的二分就是先推出公式:

对每件衣服:mid = x1(烘干时间)+x2(晾干时间);a[i] <= k*x1+x2;将1式带入2式得 x1>=(a[i]-mid)/(k-1)即每件衣服最少用时位x1向上取整。

注意这里k-1为分母,需要单独考虑k=1的情况

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll n, k, a[];
int C(int x)
{
ll tmp=;
for(int i = ; i < n; i++){
if(a[i] > x){
tmp += int(ceil(1.0*(a[i]-x)/(k-)));
}
}
return tmp<=x;
}
int main()
{
ll maxm = -INF;
scanf("%lld", &n);
for(int i = ; i < n; i++){
scanf("%lld", &a[i]);
maxm = max(maxm, a[i]);
}
scanf("%lld", &k);
if(k==){
printf("%lld\n", maxm);
return ;
}
ll lb=, ub=maxm;
while(ub-lb>){
ll mid = (lb+ub)>>;
if(C(mid)){
ub = mid;
}
else lb = mid;
}
printf("%lld\n", ub);
return ;
}