poj 3104

/*
    题意:
        有n个衣服,每个衣服都有一个数值a[i],代表它的含水量。
        你要把所有衣服晾干,有两种方法:
            1.自然晾晒,每秒少1水
            2.风干机,每秒少k水,不足k则变为0,但是同一时间只可以风干一件衣服。

        求把所有衣服风干的最短时间。

    我们可以枚举时间mid。
    判断是否可以满足在mid时间内让所有的都变为0,如果可以,那么最终答案肯定<=mid,否则>mid。
    判断是否可以让mid满足:
        对于每个衣服,假如a[i]<=mid,那么自然风干就可以了,否则需要使用机器。
        我们可以使用贪心的策略来使用机器,对于衣服,可以使用机器t秒,然后自然风干,让总时间<=mid就可以了。

        t*k + mid - t >= a[i]
        有t >= (a[i] - mid)(k-1)

        k=1则之间输出a[i]最大的就可以了
        k!=1,t = ceil(....)

        把每个衣服的t加起来,就是风干机需要使用的总时间,如果不大于mid,那么满足条件,否则不行。
*/
#include <iostream>
#include <cstdio>
#include <cmath>

#define range(i,a,b) for (int i=a;i<=b;i++)

using namespace std;

const int maxn = 100000;

int a[maxn+1];
int n,k;

bool check(int val)
{
    int sum(0);
    range(i,1,n)
        if (a[i] <= val)
        {
            continue;
        }
        else
        {
            sum += ceil((double)(a[i]-val) / (k-1));
            if (sum > val)
                return 0;
        }
    return sum <= val;
}

int main()
{
    scanf("%d",&n);

    int L(0),R(0);

    range(i,1,n)
    {
        scanf("%d",&a[i]);
        R = R < a[i] ? a[i] : R;
    }
    scanf("%d",&k);

    if (k == 1)
    {
        cout<<R<<endl;
        return 0;
    }

    range(c,1,100)
    {
        int mid = (L+R) >> 1;
        if (check(mid))
        {
            R = mid;
        }
        else
        {
            L = mid;
        }
    }

    if (check(L))
        cout<<L<<endl;
    else
        cout<<L+1<<endl;

    return 0;
}
时间: 2025-01-05 19:20:42

poj 3104的相关文章

poj 3104 Drying (二分)

/*设某次二分出的一个值是mid: 1.对于一件ai值小于等于mid的衣服,直接晾干即可: 2.对于一件ai值大于mid值的衣服,最少的用时是用机器一段时间, 晾干一段时间,设这两段时间分别是x1和x2, 那么有mid=x1+x2,ai<=k*x1+x2,解得x1>=(ai-mid)/(k-1) , 所以对(ai-mid)/(k-1)向上取整就是该件衣服的最少用时.*/ # include <stdio.h> # include <string.h> # include

POJ 3104 Drying(二分答案)

题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11128   Accepted: 2865 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afra

POJ 3104 Drying

Drying Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 310464-bit integer IO format: %lld      Java class name: Main It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart g

POJ 3104 Drying [二分 有坑点 好题]

传送门 表示又是神题一道 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 proces

poj 3104(二分)

题意:有n个衣服要烘干,每件衣服都有含水量ai,每分钟衣服含水量都可以减少1,用烘干机每分钟含水量减少k,烘干机每次只能放入一件衣物,那么问最少几分钟可以让所有衣服含水量为0. 题解:先把含水量从大到小排序,二分出时间x,然后如果a[i] <= x,就不用烘干机,否则a[i] - 已过去时间 - k * 已使用烘干机次数 <= x - 已过去时间 - 已使用烘干机次数, 从而计算出这件衣服应该用最少几次烘干机(向上取整),然后已过去时间+使用烘干机次数如果大于x,返回false. #inclu

POJ - 3104 Drying 二分 + 贪心

题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 解题思路:贪心的话,每分钟都要使用到烘干机. 枚举时间,如果湿度小于等于时间的话,就不用考虑了,在枚举时间内肯定会干的 如果湿度大于枚举时间的话,就要考虑一下了,该衣服要在给定时间内湿度变为零的话就要满足该式子,设已经过了cnt分钟了,当前这件衣服的湿度为num[i],枚举的时间为mid,那么 (

poj 3104 Drying(二分搜索之最大化最小值)

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

【POJ 3104】Drying

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

POJ - 3104 :Drying

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 thi