Drying POJ - 3104 Best of two points

Question: There are N pieces of wet clothes and a dryer. Each garment has a humidity value. It will decrease by one every second, and if you use a dryer, it will decrease by k every second. Ask how long it will take to dry.

Solution: Two points. First of all, the longer it takes, the easier it will be to dry.

    Secondly, the decision function can be given as follows: for the answer X, every clothes with humidity greater than X should be dried. So you can directly count the total time the dryer is used, and return 0 if it is greater than X.

    Note the round-down details.

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
const int maxn = 1e5 + 5;
int a[maxn];
int n, k;
int l, r, mid;
bool check(int x) {
    int now = 0;//time for radiator
    rep(i, 1, n) {
        if (a[i] > x) {
            now += (a[i] - x - 1) / (k - 1) + 1;//k-1&&ceiling/consider integer 
            if (now > x)return 0;
        }

    }
    return 1;
}
void Run() {
    l = 0, r = 0;
    r = *max_element(a + 1, a + 1 + n);
    if (k == 1) { cout << r<<endl; }
    else {
        while (l <= r) {
            mid = l + r >>1;
            if (check(mid))r = mid - 1;
            else  l = mid + 1;

        }
        cout << l << endl;
    }
}

void smain() {


    cin >> n;
    rep(i, 1, n)cin >> a[i];
    cin >> k;

    Run();
    
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325289786&siteId=291194637