题解:P15008 [UOI 2019 II Stage] 狼人杀

· · 题解

题目传送门

~一道简单的二分题~

思路

::::info[简化一下题意] 给定最少需要的总单词数 n,每位测试员公共频道发 a个单词,向其他每位测试员私人频道发 b 个单词,求最少雇佣的测试员数量,使得总单词数大于等于 n。 ::::

可以设雇佣 x 位测试员,总单词数由两部分组成:

得出以下式子:

f_x = a\times x + b\times x\times \left(x-1\right)

由于函数 f 单调递增(a\ge 0、b\ge 0 且 a+b>0),因此最小满足 f_x\ge n 的 x 可以二分。直接对需要雇佣的测试员人数二分答案。

::::warning 不开 long long 见祖宗。 ::::

代码

#include<bits/stdc++.h>
#define lcm(x,y) x/__gcd(x,y)*y
#define lb(x) (x&-x)
#define str to_string
#define endl '\n'
#define endr '\r'
#define emp_ '\0'
using namespace std;
#define int long long
using ll=long long;
namespace Fast{
    namespace Fread{const int SZ=1<<20;char buf[SZ],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SZ,stdin);if(S==TT)return endl;}return *S++;}}
    namespace Fwrite{const int SZ=1<<20;char buf[SZ],*S=buf,*TT=buf+SZ;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
    using namespace Fread;
    using namespace Fwrite;
    #define getchar Fread::getchar
    #define putchar Fwrite::putchar
    namespace FastIO{
        struct R{
            template<typename T>
            R&operator>>(T&from){from=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')from=(from<<3)+(from<<1)+(c^48),c=getchar();from*=f;return*this;}
            R&operator>>(double&from){from=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(long double&from){from=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(__float128&from){from=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
            R&operator>>(char&c){c=getchar();while(c==' '||c==endl||c==endr)c=getchar();return*this;}
            R&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c==endl||c==endr)c=getchar();while(c!=' '&&c!=endl&&c!=endr)str[len++]=c,c=getchar();str[len]=emp_;return*this;}
            R(){}
        }cin;
        struct W{
            const int S=6;
            typedef int M;
            template<typename T>
            W&operator<<(T from){if(from==0){putchar('0');return*this;}if(from<0)putchar('-'),from=-from;static short sta[40];short top=0;while(from>0)sta[++top]=from%10,from/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(long double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(__float128 from){if(from<0)putchar('-'),from=-from;M _=from;from-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
            W&operator<<(char c){putchar(c);return*this;}
            W&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
            W&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
            W(){}
        }cout;
    }
    int max(register const int e,register const int b){register const int from(e),to(b);return(from>to?from:to);}
    int min(register const int e,register const int b){register const int from(e),to(b);return(from>to?to:from);}
}
using namespace Fast;
#define cin FastIO::cin
#define cout FastIO::cout
const double EPS=1e-6,PAI=acos(-1.0);
const int MAX=5e5+5,mod=1e9+7,MOD=998244353;
int n,a,b;
bool check(int mid){ // 判断是否满足
    int p=a*mid,q=0;
    if(p>=n)return true;
    if(b){
        if(mid&&b>n/mid)q=n;
        else{
            int t=b*mid;
            if((mid-1)&&t>n/(mid-1))q=n; // 无需精确计算
            else q=t*(mid-1);
        }
    }
    return(p+q)>=n;
}
void solve(){
    cin>>n>>a>>b;
    int l=1,r;
    if(!b){ // b=0时,处理整除误差
        r=(n/a)+2;
        if(a>=n)r=1;
    }else{ 
        r=sqrt(1.0*n/b)+2;
        r=max(r,2);
    }
    int ans=r;
    while(l<=r){
        int mid=l+(r-l)/2; // 避免溢出
        if(check(mid)){
            ans=mid;
            r=mid-1;
        }else l=mid+1;
    }
    cout<<ans<<endl;
}
signed main(){
    int t=1;
//  cin>>t;
    while(t--)solve();
    return 0;
}

AC 记录