POJ3104 Drying —— 二分
题目链接:http://poj.org/problem?id=3104
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18378 | Accepted: 4637 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, k, a[MAXN]; bool test(int mid)
{
int time = , left = mid;
for(int i = n; i>=; i--)
{
a[i] -= time; // a[i]的值已经发生改变了!!!!!! 应该另开一个数组用于操作,以保证原始数值不被修改
if(a[i]<=left) continue;
int t = (a[i]-left)/(k-);
if((a[i]-left)%(k-)) t++;
time += t;
left -= t;
if(left<) return false;
}
return true;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]);
scanf("%d", &k);
sort(a+, a++n);
if(k==)
{
printf("%d\n", a[n]);
continue;
} int l = , r = a[n];
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, k, a[MAXN];
int tmp[MAXN]; bool test(int mid) //模拟过程
{
//time为当前时间, left为剩余时间, time与left之和恒为mid
int time = , left = mid;
memcpy(tmp, a, sizeof(tmp)); //!!!!!!
for(int i = n; i>=; i--)
{
tmp[i] -= time; //过了多长时间, 就风干了多少水。
if(tmp[i]<=left) continue; //如果剩下的时间可以让这件衣服完全风干, 则跳过;否则下一步
int t = (tmp[i]-left)/(k-); //t为使得这件衣服可以风干的最少烘干次数
if((tmp[i]-left)%(k-)) t++; //向上取整
time += t; //更新当前时间和剩余时间
left -= t;
if(left<) return false; //如果超额,则不能满足条件
}
return true;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]);
scanf("%d", &k);
sort(a+, a++n);
if(k==) //当k为1时, 烘干机没用了, 全部自己风干。
{
printf("%d\n", a[n]);
continue;
} int l = , r = a[n];
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, k, a[MAXN]; bool test(LL limit)
{
int sum = ;
for(int i = n; i>=; i--)
{
if(a[i]<=limit) continue; //直接风干
int t = (a[i]-limit)/(k-); //t为使得这件衣服可以风干的最少烘干次数
if((a[i]-limit)%(k-)) t++; //向上取整
sum += t; //时间累加
if(sum>limit) return false; //超过时间限制, 退出
}
return true; //满足条件
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]);
scanf("%d", &k);
sort(a+, a++n);
if(k==) //当k为1时, 烘干机没用了, 全部自己风干。
{
printf("%d\n", a[n]);
continue;
} int l = , r = a[n];
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}
POJ3104 Drying —— 二分的更多相关文章
- POJ3104 Drying(二分查找)
POJ3104 Drying 这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分. 设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自 ...
- POj3104 Drying(二分)
Drying Time Limit: 2000MS Memory Limit: 65536K Description It is very hard to wash and especially to ...
- POj-3104 Drying 二分+贪心
题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 ...
- poj3104 Drying(二分最大化最小值 好题)
https://vjudge.net/problem/POJ-3104 一开始思路不对,一直在想怎么贪心,或者套优先队列.. 其实是用二分法.感觉二分法求最值很常用啊,稍微有点思路的二分就是先推出公式 ...
- Drying poj3104(二分)
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7916 Accepted: 2006 Descriptio ...
- POJ 3104 Drying(二分答案)
题目链接:http://poj.org/problem?id=3104 ...
- POJ3104 Drying 2017-05-09 23:33 41人阅读 评论(0) 收藏
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15604 Accepted: 3976 Descripti ...
- POJ3104 Drying
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13703 Accepted: 3527 Description It i ...
- POJ 3104 Drying [二分 有坑点 好题]
传送门 表示又是神题一道 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9327 Accepted: 23 ...
随机推荐
- STL学习笔记(五) 算法
条款30:确保目标区间足够大 条款31:了解各种与排序有关的选择 //使用unaryPred划分输入序列,使得unaryPred为真的元素放在序列开头 partition(beg, end, unar ...
- DP的序--Codeforces626F. Group Projects
$n \leq 200$个数,$ \leq 500$,$K \leq 1000$代价内的数字分组有多少?一个分组的代价是分成的每个小组的总代价:一个小组的代价是极差. 问的极差那就从极入手嘛.一个小组 ...
- Scrapy学习-9-FromRequest
用FromRequest模拟登陆知乎网站 实例 默认登陆成功以后的请求都会带上cookie # -*- coding: utf-8 -*- import re import json import d ...
- tmux基本操作
安装和移除: // 安装 sudo apt-get install tmux // 移除 sudo apt-get remove tmux 常用命令: tmux [new -s 会话名 -n 窗口名] ...
- malloc动态分配多维数组
下面试自己写的三个测试程序,如果看懂了基本上动态分配多维数组就没什么问题啦:重点 1:深刻理解多维数组的概念,多维数组在内存中的分配情况,基本上动态分配也没什么问题的.然后还要注意一点的就是,释放是分 ...
- ceph工作原理和安装
一.概述 Ceph是一个分布式存储系统,诞生于2004年,最早致力于开发下一代高性能分布式文件系统的项目.随着云计算的发展,ceph乘上了OpenStack的春风,进而成为了开源社区受关注较高的项目之 ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1
Despite the fact that Cypress is an application that runs natively on your machine, you can install ...
- HDU 4422 The Little Girl who Picks Mushrooms(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4422 Problem Description It's yet another festival s ...
- Win7 VNC远程连接Centos桌面
一,安装Linux桌面: yum -y groupinstall Desktop yum -y groupinstall "X Window System" yum -y grou ...
- 王立平--GUI与GUILayout的差别
GUI.Button (new Rect(0,0,5,5,"哈哈"); GUILayout.Button ("heheh"); 1.以上代码都是现实一个butt ...