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的情况
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef long long ll; 12 using namespace std; 13 ll n, k, a[100010]; 14 int C(int x) 15 { 16 ll tmp=0; 17 for(int i = 0; i < n; i++){ 18 if(a[i] > x){ 19 tmp += int(ceil(1.0*(a[i]-x)/(k-1))); 20 } 21 } 22 return tmp<=x; 23 } 24 int main() 25 { 26 ll maxm = -INF; 27 scanf("%lld", &n); 28 for(int i = 0; i < n; i++){ 29 scanf("%lld", &a[i]); 30 maxm = max(maxm, a[i]); 31 } 32 scanf("%lld", &k); 33 if(k==1){ 34 printf("%lld ", maxm); 35 return 0; 36 } 37 ll lb=0, ub=maxm; 38 while(ub-lb>1){ 39 ll mid = (lb+ub)>>1; 40 if(C(mid)){ 41 ub = mid; 42 } 43 else lb = mid; 44 } 45 printf("%lld ", ub); 46 return 0; 47 }