POJ3104 Drying

  这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分.

  设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自然晾干,

  此题自然就转化为了求把让所有衣服都干(烘干+晾干)所用的最小时间,

    当c(X)为true时,ub=mid,尽量减小区间,

    当C(x)为false时,表示时间为x时不成立,必须增加时间,令lb=mid+1

    循环终止条件为lb=ub,此时任意输出一个值就ok了

    此题有个坑:把衣服放入烘干机中时,每件衣服每分钟脱水量为k,在外面晾干时,每分钟脱水量为1,算烘干时间消耗时,如果假设该衣服在x时间内固定减小1的话,那么在烘干机里面就每分钟固定减小k-1,

    若第i件衣服的水量为a[i],当a[i]>x时,该件衣服所需烘干机时为(a[i]-x)/(k-1)向上取整,即(a[i]-x+k-2)/(k-1),由于k-1可能为0,所以必须单独判断一下,当k=1时,烘干与晾干效率一样,若a[i]>x,就不满足题意

/*
* Created: 2016年03月31日 22时25分12秒 星期四
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; int n,k,maxn;
int a[];
bool C(int x){ //考虑外面每分钟减少1,放入烘干机其实每分钟减少k-1
int num=x; //剩余的烘干时间,
bool flag=true;
rep(i,,n){
if(a[i]>x){
if(k==){ //不判断可能会re
flag=false;break;
}
num=num-(a[i]-x+k-)/(k-);
if(num<){
flag=false;break;}
}
}
return flag;
}
int main()
{
while(in(n)!=EOF){
maxn=;
rep(i,,n){
in(a[i]);
maxn=max(maxn,a[i]);
}
in(k);
int lb,ub,mid;
lb=,ub=maxn;
while(ub>lb){
mid=(ub+lb)/;
if(C(mid)) ub=mid;
else lb=mid+;
}
out(ub);
}
return ;
}

POJ3104 Drying(二分查找)的更多相关文章

  1. POj3104 Drying(二分)

    Drying Time Limit: 2000MS Memory Limit: 65536K Description It is very hard to wash and especially to ...

  2. POJ3104 Drying —— 二分

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  3. POj-3104 Drying 二分+贪心

    题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 ...

  4. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  5. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  6. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  7. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  8. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  9. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

随机推荐

  1. Python Socket Programming

    本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...

  2. MVC + LigerUI 做后台管理还真是清爽

    LigerUI是基于Jquery,轻量级UI框架.具体可以看官方演示 http://www.ligerui.com/ 我的简单后台 模拟Winodw桌面效果,挺不错呢.最喜欢的还是他的,下拉列表绑定G ...

  3. 转:perror和strerror的区别

    概述: perror和strerror都是C语言提供的库函数,用于获取与erno相关的错误信息,区别不大,用法也简单.最大的区别在于perror向stderr输出结果,而 strerror向stdou ...

  4. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  5. Android中几种常用的话框

    1.普通对话框: Builder alert=new AlertDialog.Builder(MainActivity.this); alert.setTitle("提示"); a ...

  6. GUID 的优缺点 uniqueidentifier

    1) 优点 同 IDENTITY 列相比,uniqueidentifier 列可以通过 NewID() 函数提前得知新增加的行 ID,为应用程序的后续处理提供了很大方便. 便于数据库移植,其它数据库中 ...

  7. sicily 4433 DAG?

    题意:输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). 解法:还是深搜 #include<iostream> #include<memory ...

  8. MyEclipse8.6下的svn插件安装

    myeclipse8.6的svn插件安装 下载site-1.6.18.zip 在myeclipse8.6的MyEclipse8.6的安装目录D:/install/MyEclipse8.6/Genuit ...

  9. Storm实现单词计数

    package com.mengyao.storm; import java.io.File; import java.io.IOException; import java.util.Collect ...

  10. ERP售前顾问形象寓意

    几个做销售的朋友一起去X山旅游,一群爱侃的人凑在一起,一顿云山雾罩的神吹是少不了的.突然看到一栋平房前面伸出一个大大的幌子:铁嘴铜牙.下面一行小字:不灵不要钱(管理专家:提供专业解决方案). 销售都是 ...