poj 3104 Drying 二分搜索--查找最小yes值
来源:互联网 发布:i5处理器编程够用么 编辑:程序博客网 时间:2024/06/01 10:16
Description
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.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #132 3 95sample input #232 3 65
Sample Output
sample output #13sample output #22
Source
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
LL a[100005],b[100005];
LL n,k;
int ok(LL mid)
{
LL cnt=0;
for(int i=1;i<=n;i++)
{
if(a[i]<mid)
continue; //!!这个地方很容易错啊,假如没有这个判断的话的当a[i]<《mid时,cnt是会减少的。
cnt+=(a[i]-mid+k-2)/(k-1);//向上取整,或者ceil
}
return cnt<=mid?1:0;
}
int main()
{
while(~scanf("%lld",&n))
{
LL l=0,r=0,mid;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>r)
r=a[i];
}
scanf("%lld",&k);
if(k==1)
{
printf("%d\n",r);
continue;
} //因为后面用到了/(k-1),所以需要特别判断下k==1的情况
while(l<r)
{
mid=l+(r-l)/2;
if(ok(mid))
r=mid;
else
l=mid+1;
}
printf("%lld\n",r);
}
return 0;
}
- poj 3104 Drying 二分搜索--查找最小yes值
- POJ 3104 Drying 二分搜索
- POJ 3104 - Drying(二分搜索)
- poj 3104 Drying(二分查找)
- poj 3104 Drying (二分搜索答案)
- POJ - 3104 Drying (二分搜索)
- POJ 3104 Drying (二分搜索)
- POJ 3104 Drying 二分
- POJ--3104--Drying【二分】
- POJ 3104 Drying 二分
- poj 3104 Drying (二分)
- POJ 3104 Drying (二分)
- POJ 3104 Drying (二分)
- POJ 3104- Drying-二分
- POJ 3104 Drying(二分)
- POJ - 3104 Drying(二分)
- POJ 3104 Drying【二分】
- poj 3104 Drying 【二分查找(最小化最大值)】
- poj 2139 Six Degrees of Cowvin Bacon 最短路bellman 多源最短路径 (一次AC)
- Codeforces Round #329 (Div. 2) A. 2Char 字符串+暴力
- poj 3259 Wormholes 最短路bellman 题意转化很重要
- Spring配置之RMI
- poj 2456 Aggressive cows 整数二分写法 模板题
- poj 3104 Drying 二分搜索--查找最小yes值
- poj 3111 K Best 二分搜索 最大化平均值
- POj 3258 River Hopscotch 二分搜索 最大化最小值
- poj 2674 Linear world 弹性碰撞 升级的蚂蚁
- poj 2785 4 Values whose Sum is 0
- Codeforces Round #324 (Div. 2) A. Olesya and Rodion 构造数字 思维题
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya 思维题 数论
- Poj 3977 Subset 折半枚举 超大背包
- poj 2549 Sumsets 折半枚举