POJ3104(二分搜索)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13057 | Accepted: 3358 |
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 kwater, 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 #1
3
2 3 9
5 sample input #2
3
2 3 6
5
Sample Output
sample output #1
3 sample output #2
2
思路:二分minute。设每个clothes所需y次烘干,那么就有minute-y次自然干。minute-y+y*k>=x[i]。解y>=(x[i]-minute)/(k-1)。注意k等于1的情况。
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=;
int n,k;
int x[MAXN];
bool test(int t)
{
int s=;
for(int i=;i<n;i++)
{
if(x[i]<=t) continue;
s+=(int)ceil((x[i]-t)*1.0/(k-));
if(s>t) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int mx=;
for(int i=;i<n;i++)
{
scanf("%d",&x[i]);
mx=max(mx,x[i]);
}
scanf("%d",&k);
if(k==)
{
printf("%d\n",mx);
}
else
{
int l=;
int r=0x3f3f3f3f;
while(r-l>)
{
int mid=(l+r)>>;
if(test(mid)) r=mid;
else l=mid;
}
printf("%d\n",r);
}
}
return ;
}
POJ3104(二分搜索)的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)
题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k 分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- HDU 2852 KiKi's K-Number(树状数组+二分搜索)
题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...
- nyoj914Yougth的最大化(二分搜索 + 贪心)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...
- POJ3104 Drying(二分查找)
POJ3104 Drying 这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分. 设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自 ...
随机推荐
- php数组函数-array_reduce()
array_reduce()函数发送数组中的值到用户自定义函数,并返回一个字符串. 注:如果数组是空的或则初始化值未传递,该函数返回NULL array_reduce(array,myfunction ...
- iOS 学习之 UITabBarController
- (IBAction)btnClick:(id)sender { UITabBarController *tabBarCtrl = [[[UITabBarController alloc] init ...
- java web数据库连接封装-simple
package cn.cslg.bm.web.util; import java.sql.Connection; import java.sql.DriverManager; import org.a ...
- C++中随机数的生成
1.随机数由生成器和分布器结合产生 生成器generator:能够产生离散的等可能分布数值 分布器distributions: 能够把generator产生的均匀分布值映射到其他常见分布,如均匀分布u ...
- 【bzoj5055】膜法师(离散化+树状数组)
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=5055 这道题……不得不说,从标题到题面都能看出一股浓浓的膜法气息……苟…… 题意就是统计顺序 ...
- 【codevs1002】搭桥(prim)
题目描述: 这是道题题意有点迷(或者是我语文不好),但其实实际上求的就是图中连通块的个数,然后在连通块与连通块之间连边建图跑最小生成树.但是……这个图可能是不连通的……求桥的数量和总长 于是我立刻想到 ...
- EF Code-First 学习之旅
什么是Code-First 基本工作流: 写好应用程序的领域类和上下文类→配置领域类的额外映射→运行程序→Code-First API创建新的数据库或与现有数据库对应→添加种子数据到数据库中测试
- 到底EJB是什么
到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:" ...
- 【转】数据库获得当前时间getdate()
CONVERT(nvarchar(10),count_time,121): CONVERT为日期转换函数,一般就是在时间类型 (datetime,smalldatetime)与字符串类型(nchar, ...
- Codeforces 893E Counting Arrays:dp + 线性筛 + 分解质因数 + 组合数结论
题目链接:http://codeforces.com/problemset/problem/893/E 题意: 共q组数据(q <= 10^5),每组数据给定x,y(x,y <= 10^6 ...