传送门

表示又是神题一道

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 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

Source

Northeastern Europe 2005, Northern Subregion

本来想贪心的,结果越写越乱,老老实实二分。。。不过这题有其精妙之处,wa的人先看样例

送点test data给WA的朋友们……

Posted by MIRKING at 2008-07-31 13:39:06 on Problem 3104 and last updated at 2008-07-31 13:40:02


test data:
2
8
9
2
result:6
7
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
2
result:875000000

Re:为什么二分时 枚举的时间l,r,mid不用__int64就wa了

Posted by wangyaoxuan at 2010-10-14 11:30:23 on Problem 3104 
In Reply To:为什么二分时 枚举的时间l,r,mid不用__int64就wa了 Posted by:wangyaoxuan at 2010-10-14 11:22:39

表示理解了,因为过程中求得的时间值t会超过int,后来的wa们慢慢体会....

Posted by lgq1205 at 2009-12-03 12:54:33 on Problem 3104


二分枚举时间,判断可行性,然后求解即可.

注意判断可行性的过程:

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

如果你WA了,看一下是否有变量要设为64位int,如果RE了,看一下除零。。。

Posted by 2745972075 at 2014-07-21 15:47:54 on Problem 3104

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 10
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-9
//#define inf 0x3fffffffff
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
ll ans;
ll a[N];
ll k; void ini()
{
ans=;
int i;
for(i=;i<=n;i++){
scanf("%I64d",&a[i]);
}
scanf("%I64d",&k);
sort(a+,a++n);
} int ok(ll m)
{
int i;
ll le=m;
ll re;
for(i=;i<=n;i++){
re=a[i]-m;
if(re<=) continue;
le-=(re+k-)/(k-);
}
if(le>=) return ;
return ;
} void solve()
{
ll l=;
ll r=a[n];
ll m;
//printf(" %d\n",a[1]);
if(k==){
ans=a[n];return;
}
while(l<r)
{
m=l+(r-l)/;
if(ok(m)==){
r=m;
}
else{
l=m+;
}
}
ans=l;
} void out()
{
printf("%I64d\n",ans);
} int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
} return ;
}

最新文章

  1. form表单取消按钮自动提交
  2. kxbdSuperMarquee.js滚动的神器-推荐
  3. MongoDB由于目标计算机积极拒绝,无法连接
  4. oracle计算两行差值
  5. Python实现LR(逻辑回归)
  6. Nginx实现七层负载均衡配置指导
  7. .net ajax式上传文件
  8. 当使用javac编译源文件时,如何查找import导入的类
  9. AtCoder Grand Contest 013
  10. name属性作用+使用$.post()取代name属性在提交表单信息中的作用
  11. 【Spark篇】---Spark资源调度和任务调度
  12. vue-cli卸载旧版,再重新安装后还显示的是旧的版本
  13. JS验证码邮件
  14. quartz执行两遍问题
  15. 【MySQL】【4】数据库时间与实际时间相差8小时
  16. java单例类的几种实现
  17. [转载]MySQL面试题
  18. [Elite 2008 Dec USACO]Jigsaw Puzzles
  19. LWIP network interface 网卡 初始化 以 STM32 为例子 后面会有 用 2G 或者4G 模块 用 PPP拨号的 形式 虚拟出网卡 所以先以 这个为 前提
  20. Web服务器进程连接数和请求连接数

热门文章

  1. C#中Lock关键字的使用
  2. CPP-基础:windows api 多线程---互斥量、信号量、临界值、事件区别
  3. 二分查找 &amp;&amp; 三分查找
  4. idea快速生成实体类Entity
  5. javascript(函数式编程思考) ---&gt; Map-Filter-quicksort-Collatz序列-Flodl-Flodr
  6. PAT 乙级 1027
  7. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card
  8. 微信小程序登录对接Django后端实现JWT方式验证登录
  9. (转)Duplicate Symbol链接错的原因总结和解决方法
  10. Ubuntu桌面主题设置以及优化