POJ - 3104 :Drying

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件衣服,其含水量给出,不做处理单位时间可减少一含水量,有且仅有一个烘干机可以单位时间减少k含水量,求将所有衣服弄干的最短时间,用二分法。对于每一个中间值,枚举每个衣服,小于x则自然风干,大于x能用烘衣机一段时间再自然风干使其时间等于x。

#include<iostream>#include<string.h>#include<cmath>

using namespace std;

long long a[100005];

int main()
{	int n;	while(cin>>n)	{		long long max=-1;		memset(a,0,sizeof(a));		for(int i=0;i<n;i++)		{			cin>>a[i];			if(a[i]>max)				max=a[i];		}			long long  k;		cin>>k;		long long low=1,hign=max,mid;		if(k!=1)		{

while(hign-low>1)		{			mid=(low+hign)/2;			long long time=0;			for(int i=0;i<n;i++)			{				if(a[i]>mid)					time=time+(a[i]-mid +k-2)/ (k-1);			}			if(time>mid)				low=mid+1;				else					hign=mid; 		}

mid=(low+hign)/2;		cout<<mid<<endl;		}		else			cout<<max<<endl;	}

return 0;}
时间: 2024-08-03 22:06:22

POJ - 3104 :Drying的相关文章

【POJ 3104】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 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 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 1904:King&#39;s Quest【tarjan】

题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书379页二分图的压轴题,在图论刷的题还不多时思考过这题,现在想来也不难想 这题引人瞩目的一点便是预先给出了一个二分图的初始匹配 对每个点枚举后增广显然不怎么可行,那么还是图论问题的经典思考方式,点和边各表示什么 题目的输入天然的给出了一个图,但对这题好像没什么用处,于是开始思考把给出的初始匹配的每条边