二分(two of drying)

http://poj.org/problem?id=3104

题意:有n件含水a[i]的衣服,两种风干方式,1、自然风干每次减少1滴水。2、散热器风干每次减少k滴水。

解法:二分时间,判断时间是否符合。

假设此时判断mid时间是否可以风干所有衣服。考虑每一件衣服a[i] , 自然风干a[i] - mid + t , t为散热器风干。

只需判断散热器风干的总和是否大于mid,如果大于,则不符。

有 t * k = a[i] - mid + t .可求出t。t = (a[i] - mid) / (k - 1).向上取整。特判下k==1时,取最大,否则会RE。

//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
typedef long long ll ;
#define int ll
#define mod 1000000007
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//ll lcm(ll a , ll b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define cin(x) scanf("%lld" , &x);
using namespace std;
const int N = 1e7+9;
const int maxn = 1e5+9;
const double esp = 1e-2;
int n ;
int a[maxn];
int k ;

bool check(int x){
    int ans = 0 ;
    rep(i , 1 , n){
        if((a[i] - x) > 0){
            int t = (a[i] - x) / (k - 1);
            ans += (a[i] - x) % (k - 1) == 0 ? t : t + 1;
        }
    }
    if(ans > x) return false;
    else return true;
}

void solve(){
    int ma = -1;
    cin >> n ;
    rep(i , 1 , n){
        scanf("%lld" , &a[i]);
        ma = max(ma , a[i]);
    }
    scanf("%lld" , &k);
    if(k == 1){
        cout << ma << endl;
        return ;
    }
    int l = 0 , r = 1e9;
    while(r > l){
        int mid = (r + l) >> 1;
        if(!check(mid)){
            l = mid + 1;
        }else{
            r = mid ;
        }
    }
    cout << l << endl;
}

signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0); cout.tie(0);
    //int t ;
    //cin(t);
    //while(t--){
    //while(~scanf("%lld%lf" , &n , &a))
        solve();
    //}
}

 

原文链接: https://www.cnblogs.com/nonames/p/12463497.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    二分(two of drying)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/372628

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月3日 上午11:20
下一篇 2023年3月3日 上午11:21

相关推荐