POJ3104 Drying [二分]

题目不是很难

大体思路:

题意:烘干机,给出一堆衣服的水分a[i],在不加烘干机情况下自动每一分钟减少1水分,每分钟可以变改衣服(i)到烘干机中,每分钟减少k水分,求最少需要多少时间。

题解:第一时间就想到使用二分枚据答案+验证这种思路,不过这题还是有些陷阱需要注意。

1. 验证答案时,如果 a[i] <= mid,让它自然烘干即可 ; 如果a[i] > mid,那么烘干这件衣服可以分成两段时间:使用烘干机时间x1 + 自然烘干时间x2,那么可以列出等式:mid = x1 + x2; a[i] <= kx1+x2;于是得x1 >= (a[i] -mid)/(k-1);即得使用烘干机的最少时间x1

2.注意当k==1时,k-1 == 0,需要特殊处理,直接打出ans = maxV

3.注意当求left+right时,结果可能超出范围,正确的方法应该是left + (right - left)*0.5;

犯了一个很2的错误,ceil(int/int),应该是ceil(int*1.0/int)

再次验证我的二分写法没错,哇哈哈哈

标准的

while l<r

l=mid+1;

r=mid;

mid=l+(r-l)/2;

代码如下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

long long num[111111];
int main()
{
	//cout<<"here"<<endl;
	//cout<<ceil(3.4)<<endl;
	//cout<<ceil(4)<<endl;
	//cout<<"end"<<endl;
	long long n,k;
	long long maxn=0;
	while(scanf("%lld",&n)!=EOF)
	{
			for(long long i=1;i<=n;i++)
			{
				scanf("%lld",&num[i]);
				maxn=max(maxn,num[i]);
			}
			sort(num+1,num+1+n);
			scanf("%lld",&k);
			if(k==1)
			{
				printf("%lld\n",maxn);
				continue;
			}
			long long l=1,r=maxn,mid=(r+l)/2;
			long long ans=0;
			while(l<r)
			{
				//mid=(l+r)/2;
				long long sum=0;
				for(long long i=1;i<=n;i++)
				{
					if(num[i]>mid)
						sum+=ceil((num[i]-mid)*1.0/(k-1));
				}
				if(sum>mid)
					l=mid+1;
				else if(sum<=mid)
				{
					r=mid;
				}
				mid=(l+r)/2;
			}
			printf("%lld\n",mid);
	}
}

POJ3104 Drying [二分]

时间: 2024-09-30 19:32:51

POJ3104 Drying [二分]的相关文章

POJ3104 Drying(二分查找)

POJ3104 Drying /* * Created: 2016年03月31日 22时25分12秒 星期四 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #in

POJ3104:Drying(二分)

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

poj 3104 Drying (二分)

/*设某次二分出的一个值是mid: 1.对于一件ai值小于等于mid的衣服,直接晾干即可: 2.对于一件ai值大于mid值的衣服,最少的用时是用机器一段时间, 晾干一段时间,设这两段时间分别是x1和x2, 那么有mid=x1+x2,ai<=k*x1+x2,解得x1>=(ai-mid)/(k-1) , 所以对(ai-mid)/(k-1)向上取整就是该件衣服的最少用时.*/ # include <stdio.h> # include <string.h> # include

POJ 3104 Drying [二分 有坑点 好题]

传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 2364 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 proces

POJ 3104 Drying(二分答案)

题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11128   Accepted: 2865 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afra

POJ 3104 Drying (二分+精度)

题目链接:click here~~ [题目大意]: 题意:有一些衣服,每件衣服有一定水量,有一个烘干机,每次能够烘一件衣服,每分钟能够烘掉k单位水. 每件衣服没分钟能够自己主动蒸发掉一单位水, 用烘干机烘衣服时不蒸发.问最少须要多少时间能烘干全部的衣服. [解题思路]: 题目数据较大.常规方法肯定会TE.首先能够想到二分枚举答案.枚举时间mid值,(一般二分的题目,题目叫你求什么,就二分什么就能够了) 那么相应两种方法 1:自然风干.2:吹风机吹干 若一件衣服的水量小于mid,则自然风干就可以,

POJ - 3104 Drying 二分 + 贪心

题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 解题思路:贪心的话,每分钟都要使用到烘干机. 枚举时间,如果湿度小于等于时间的话,就不用考虑了,在枚举时间内肯定会干的 如果湿度大于枚举时间的话,就要考虑一下了,该衣服要在给定时间内湿度变为零的话就要满足该式子,设已经过了cnt分钟了,当前这件衣服的湿度为num[i],枚举的时间为mid,那么 (

POJ 3104 Drying(二分)

题目描述: Drying Time Limit: 2000MS Memory Limit: 65536K 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. B

POJ 3104 Drying(二分

Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22163   Accepted: 5611 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