浅析 poj3104 Drying (二分)

题目链接:

http://poj.org/problem?id=3104

题面:

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件衣服需要晾干,每件衣服含水量 aia_i,每件衣服每分钟自然风干 1 单位的水,每分钟可以对其中任意一件衣服使用吹风机,其可以减少 k 单位的水,求晾干所有衣服的最少时间。

数据规模:

1n1000001ai1091k1091 \leq n \leq 100000 \\ 1 \leq a_i \leq 10^9 \\ 1 \leq k \leq 10^9

分析+代码:

二分枚举答案+检验;
注意二分条件判断
注意数据规模(输入输出建议用printf\scanf);
边界特例(k=1)会导致TLE(RE);

//#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;

int n;
int k;
int a[100010] = {};

/*
   
> 注意判断可行性的过程:
   > 
   > 1.先把所有衣服的含水量减去T
   > 2.然后把>=(k-1)的拿去烘干,可以理解为烘干时候每分钟掉(k-1)水,
   	这样所有的衣服都每分钟自然干掉1水了。
   	因为每分钟掉一滴水是肯定的了,因此,如果你去烘干它的话,
   	那么它就能再掉多k-1 + 1 == k,这样才是k滴水,然后就是计算总花费时间
   > 3.最后判断总花费时间时候小于T,若小于等于,则可行性,否则不可行.

在用 烘 干 机 的同时不会自然风干

*/

bool check(int mid) {
   ll cnt = 0;
   for (int i = 1; i <= n; i++) {
   	if (a[i] > mid) {
   		cnt += (a[i]-mid) / (k-1);  // 
   		if ((a[i]-mid) % (k-1)) {
   			cnt++;
   		}
   	}
   }
   
   if (cnt <= mid) return false;
   else return true;
   
}

int main () {
   
//	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
   
//	while (cin.peek() != EOF) {
//		cin >> n;
   while (scanf("%d", &n) != EOF) {
   	
   	int mx = 0;

   	for (int i = 1; i <= n; i++) {
//			cin >> a[i]; 
   		scanf("%d", &a[i]);
   		mx = max(mx, a[i]);
   		
   	} 
//		cin >> k;
   	scanf("%d", &k);
   	
   	if (k == 1) {  // 特例判断,防止时间越界
   		printf("%d\n", mx);
   		continue;
   	}
   	
   	int l = 1, r = mx;
   	
   	while (l <= r) {
   		
   		int mid = (l + r) >> 1;
   		
   		if (check(mid)) 
   			l = mid + 1;
   		else 
   			r = mid - 1;
   	}
   	
   	printf("%d\n", l);
   	
   }
   
   return 0;
}



全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 11:43
春招失败、父母离婚,好像我的人生一团糟,一年来压力大到常常崩溃。不知道能跟谁聊,朋友其实对我非常好,但是她无意中表达出来的家庭幸福都会刺痛到我……和ai聊天,我的未来在更高处,不在楼下,忍不住爆哭😭
youngfa:害,妹妹,我是一个研究生(很上进很想找到好工作的那种),但去年因为生病回家休养错过了秋招(当时对我的冲击也是非常大的),这学期返校来了也是把论文盲审交了后才开始找工作,现在也是一个offer没有,但我就没有像你一样把这个阶段性的事情绑定到人生上,人生不仅很长,也很广阔,先停下来,放松一下哦。不要被外部环境灌输的思维操控了,好好爱自己!
点赞 评论 收藏
分享
04-11 21:31
四川大学 Java
野猪不是猪🐗:(ja)va学弟这招太狠了
点赞 评论 收藏
分享
03-07 17:34
吉林大学 Java
野猪不是猪🐗:说说我的看法: 1. 分布式微服务不是必学的,先把mysql redis spring生态 juc jvm os 计网这些学的差不多,就能应对大部分常规八股。项目直接用单体项目也是可以的 2. 你的学历有优势,后续把外卖做个拓展换皮(或者去吃透一个不那么烂大街的项目),就能够收获不少面试。但重心建议放在八股算法上,项目不必追求高级或独特,但必须吃透,并且要提前准备一些话术,比如技术选型,为什么考虑用a而不是用b 3. 五六月份大厂暑期的难度会下降(因为大佬都选好offer开始释放了,很多甚至都入职了),所以心态要稳住,不要陷入内耗 加油
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务