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 ...
随机推荐
- 关于sqfa
有的时候定义状态,下次来到这个状态的时候,不应该就否决掉下次,因为下次的权值可能比这次更优..
- PHP+MySQL Smarty简单分页显示示例
一.分页程序的原理 分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page). 有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在m ...
- Android再学习-20141111-Android应用的七大件
Android应用的七大件 应用程序的四大组件: Android的四大组件,使用时需要在程序中注册. Activity: Activity是应用程序的一个界面,可以通过这个界面查看联系人.打电话或者玩 ...
- for_each 用法!
class MapTest:public CapTest{ private: set <string> MyTestContain; typedef pair <string,int ...
- IE6双倍margin间距解决方法
虽然现在很少有人在用ie6了,但作为前端人员,是不的不面对的问题. 在ie6中,使用float后的元素,在margin—left,这是你会发现距离和你想的不一样,其他的浏览器又是正常的,增大margi ...
- Jasper_crosstab_group _Error incrementing crosstab dataset
error detail: net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeExceptio ...
- 遗传算法matlab实现
我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下运用MATLAB实现遗传算法: clc clear %参数 a = 0 ; b = 4 ; e ...
- UltraEdit-32 恢复到初始默认配置
不小心把UltraEdit-32搞乱了,字体不管怎么设置,都回不去了..然后那些菜单也被弄得乱七糟八的....于是就想把这些设置都搞成默认的.重装UltraEdit-32.清理注册表.结果发现都没有用 ...
- Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)
Qt不像VC++的win32/MFC编程那样,提供了现成的sleep函数可供调用.Qt把sleep函数封装在QThread类中.子线程可以调用sleep函数.但是如果用户想在主线程实现延时功能,该怎么 ...
- 为什么memset的第二个参数不把int替换成char
memset是一个经常被用来初始化数组的函数,其定义如下: 1 void * memset ( void * ptr, int value, size_t num ); 它的效果大致是把以ptr为起始 ...