poj 3104 二分
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12568 | Accepted: 3243 |
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 #1
3
2 3 9
5 sample input #2
3
2 3 6
5
Sample Output
sample output #1
3 sample output #2
2
题目大意:有n件衣服需要晾干,每件含水量ai.每件衣服每分钟自然干1单位的水.每分钟可以对其中任意一件使用吹风机,其可以减少k的水.求晾干所有衣服的最少时间.
思路分析:数据量很大,如果贪心做肯定会超时,而且这道题目是被归到二分专题中的,我自然而然的就选择用二分做了,二分算法,弱表才刚刚起步,在没有学之前,我觉
得二分就是在一个有序的数列中进行查找的时候会遇到,但随着学习的深入,渐渐感觉到了自己原来认识的浅薄,二分,不仅仅是查找!二分,相当于给了很多问题求解的另一种
思路,就是枚举,寻找最优解,通过二分枚举,可以最快的确定答案。这种题目的一般思路:
1.看数据范围和题目要求(最小最大),找到题眼,选用二分法。
2.确定二分边界,比如本题所要确定的边界就是需要的时间,很显然最小为1,最大为a[n-1](sort排序后).
3.开始二分逼近,寻找正确答案,此时的重点是check函数的构造,比如本题,给你一个时间t,你如何判断这些衣服在t时间内能否晾干,这就是check函数你要解决的问题。
比如本题,对于任何一个t,遍历数组a,如果a[i]<t,继续就行,若a[i]>t,则有下列方程,设烘干时间x1,自由蒸发时间x2,a[i]<=k*x1+x2,t=x1+x2.
可以求得x1>=(a[i]-t)/(k-1),向上取整即可,向上取整有技巧,你可以原式+1,然后分子减一,即(a[i]-k+k-2)/(k-1).什么时候不满足呢?自然是需要烘干的时间
>t时。至此check函数构造完毕。
tip:k==1时需单独处理。
弱重复使用字母导致报错。。。。心塞。。调试了半天
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int maxn=100000+100;
int a[maxn];
int n,k;
bool check(int t)
{
int sum=0;
if(k==1&&a[n-1]>t) return false;
for(int i=0;i<n;i++)
{
if(a[i]<=t) continue;
else
{
int b=(a[i]-t+k-2)/(k-1);
sum+=b;
if(sum>t) return false;
}
}
return true;
}
int main()
{
int ans;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
scanf("%d",&k);
int l=1,r=a[n-1];
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
}
}
poj 3104 二分的更多相关文章
- Drying POJ - 3104 二分 最优
题意:有N件湿的衣服,一台烘干机.每件衣服有一个湿度值.每秒会减一,如果用烘干机,每秒会减k.问最少多久可以晒完. 题解:二分.首先时间越长越容易晒完. 其次判定函数可以这样给出:对于答案 X,每一个 ...
- POJ 3104 Drying [二分 有坑点 好题]
传送门 表示又是神题一道 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9327 Accepted: 23 ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- POJ 3104 Drying(二分答案)
题目链接:http://poj.org/problem?id=3104 ...
- POJ 3104 Drying 二分
http://poj.org/problem?id=3104 题目大意: 有n件衣服,每件有ai的水,自然风干每分钟少1,而烘干每分钟少k.求所有弄干的最短时间. 思路: 注意烘干时候没有自然风干. ...
- POJ 3104 Drying(二分答案)
[题目链接] http://poj.org/problem?id=3104 [题目大意] 给出n件需要干燥的衣服,烘干机能够每秒干燥k水分, 不在烘干的衣服本身每秒能干燥1水分 求出最少需要干燥的时间 ...
- poj 3104 Drying(二分查找)
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ 3104 Drying (经典)【二分答案】
<题目链接> 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服没分钟可以自动蒸发掉一滴水,用烘干机烘衣服时不蒸发.问最少需要多少 ...
- poj 3104 dring 二分
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7684 Accepted: 1967 Descriptio ...
随机推荐
- 网络流——增广路算法(dinic)模板 [BeiJing2006]狼抓兔子
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- jsonp跨域请求学习笔记
前言 ajax,用苍白的话赞扬:很好. 我们可以使用ajax实现异步获取数据,减少服务器运算时间,大大地改善用户体验:我们可以使用ajax实现小系统组合大系统:我们还可以使用ajax实现前端的优化.( ...
- 整型(int)转时间格式字符串及页面long型转时间格式字符串
1,如果是封装的整型的话需要在后台进行处理再返回页面 处理过程是这样的 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm ...
- QT 串口通信 数据16进制发送
在QT中进行串口通信时,很多情况要用到发送16进制的数据.从网上找来了一段代码测试能用: static QByteArray QString2Hex(QString str) { QByteArray ...
- Ant构建与部署Java项目---入门
原文地址:http://tech.it168.com/j/2007-11-09/200711091344781.shtml Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建 ...
- Android 之 Window、WindowManager 与窗口管理
其实在android中真正展示给用户的是window和view,activity在android中所其的作用主要是处理一些逻辑问题,比如生命周期的管理.建立窗口等.在android中,窗口的管理还是比 ...
- cf B. Levko and Permutation
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...
- 安装AD15有问题多数是因为旧版本AD软件没有卸载干净,清理方法详解
论坛中总会看到有些朋友安装AD14.x,AD15.x后,使用不正常,多数情况是因为旧版本的AD软件没有卸载干净,安装新版本AD软件后,就会有问题.卸载和清理AD旧版本软件的方法如下(此方法只能解决卸载 ...
- Qt编译慢吗?
1. “用Qt写的程序编译比MFC慢”的说法是错误的绝对错误,单位代码行数编译Qt远比MFC快得多,因为Qt库的头文件设计非常好,尽量都使用了前置声明,避免了头文件嵌套,几乎所有类都使用了公有类和私有 ...
- 8.1 Optimization Overview
8.1 Optimization Overview 8.1 Optimization Overview 8.2 Optimizing SQL Statements 8.3 Optimization a ...