Drying
来源:互联网 发布:钢雨篷荷载计算软件 编辑:程序博客网 时间:2024/06/06 13:03
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.
<b>sample input #1</b>32 3 95<b>sample input #2</b>32 3 65
<b>sample output #1</b>3<b>sample output #2</b>2
题意:晾衣服,给n件衣服,和没件衣服的水量,如果自然风干,每件每分钟水量-1,还可以用烘干机,一次一件每分钟水量-k,问最少能几分钟全干。
题意了解之后,说一下我开始的想法。这么多衣服,要用烘干机烘哪件好呢?当然水最多的。怎么找呢?快速排序啊。这样就得了一轮,1.快排,2.最多的-k,3.其他-1;(中间为了降低时间还会把<=0的去掉不算了,只对>0的操作)。OK,n轮下来,都<=0了,结果就是n。简单的想法,简单的思路,理论上完全可行,就是TLE而已嘛。
然后想了想最近刚学到优先队列,一个百度 优先队列时间复杂度,比快排低?来一遍,主要就是两个优先队列来回倒,到一来回time++,某元素<=0了去掉,队列空了得出结果。也是很遗憾的TLE了。但是这俩的想法是很棒的,我会把代码贴后面。其实超时的原因,想想也可以理解,就算只有一件衣服,可水量1e9,只给一个2的烘干机,得多少轮才出来。
下面说正确思路了:用二分法,left=0,right=max{ p[i] }(表示水量最多的衣服直接晾干的时间,即最大的时间了),然后用mid逼近结果。假设mid为结果的话,有下列情况:
1、水量<=mid的衣服,直接晾干就可以了,不需要浪费烘干机。
2、水量>mid的衣服,有两部分时间:x 用烘干机的时间,那么用烘掉的水量就是x*k;y 风干的时间。且mid=x+y。
那么还有一个式子:p[i] <= x*k + y;联立解得 x>=(p[i] - mid)/(k-1)。注意可能不是整数的,这里向上取整。
代码如下:
//二分 AC
#include<iostream>#include<stdio.h>using namespace std;typedef long long ll;int N,K;int p[100010];int judge(int x){ int sum=0; for(int i=1;i<=N;i++) { if(p[i]>x) sum+=(p[i]-x+K-2)/(K-1); if(sum>x) return 0; } return 1;}int main(){ int i; while(scanf("%d",&N)!=EOF) { int maxx=0,ans=0; for(int i=1;i<=N;i++) { scanf("%d",&p[i]); if(maxx<p[i]) maxx=p[i]; } scanf("%d",&K); if(K==1) { printf("%d\n",maxx); continue; } int left=0,right=maxx,mid; while(left<=right) { mid=(left+right)/2; if(judge(mid)) { right=mid-1; ans=mid; } else left=mid+1; } printf("%d\n",ans); } return 0;}
//两个TLE的方法
#if 0 //快速排序 超时#include<iostream>#include<algorithm>#include<stdio.h>using namespace std;int cmp(const int &a,const int &b){ return a>b;}int main(){ int N,K; int p[100010]; while(~scanf("%d",&N) && N) { for(int i=0;i<N;i++) { scanf("%d",&p[i]); //cin>>p[i]; } scanf("%d",&K); int time=0; int M=N; while(1) { sort(p,p+M,cmp); for(int i=0;i<N;i++) { if(p[i]<=0) { M=i; break; } } if(M==0) break; time++; p[0]-=K; for(int i=1;i<M;i++) p[i]--; } printf("%d\n",time); //cout<<time<<endl; } return 0;}#endif // 1#if 0 //两个优先队列来回倒腾 超时#include<iostream>#include<queue>#include<stdio.h>using namespace std;int main(){ int N,K,x; while(~scanf("%d",&N) && N) { priority_queue<int>q; priority_queue<int>temp; for(int i=0;i<N;i++) { scanf("%d",&x); q.push(x); //入队列q } scanf("%d",&K); int time=0; while(1) { time++; x=q.top(); //q首元素x q.pop(); x-=K; if(x>0) temp.push(x); while(!q.empty()) { x=q.top(); q.pop(); x-=1; if(x>0) //大于0 才入temp temp.push(x); } if(temp.empty()) break; while(!temp.empty()) { x=temp.top(); temp.pop(); q.push(x); } } printf("%d\n",time); } return 0;}#endif // 1
- Drying
- Drying
- Drying
- Drying
- POJ3104 Drying
- POJ Drying
- POJ3104 Drying
- POJ3104--Drying
- POJ3104-Drying
- POJ3104 Drying
- POJ3104-Drying
- Drying POJ
- Drying POJ
- Drying POJ
- POJ 3104 Drying 二分
- 二分法 POJ-3104 Drying
- POJ--3104--Drying【二分】
- POJ 3104 Drying
- NOIP 2009 提高组 复赛 trade 最优贸易
- 个人初学bootstrap并搭建后台界面代码
- 数据结构-散列
- 比较器
- python中的生产者与消费者模式
- Drying
- 网易2018校招编程题集合3
- POJ
- miscellaneous_7
- 前端面向对象
- python相关函数
- PhpBoot: 一款用于快速开发 RESTful 接口的PHP框架
- 功夫小子实践开发-游戏设置功能的实现
- 常用SQL语句总结