跳到正文

个人记录

12.16总结

zrh1007363868约 18 分钟
原文
文章信息与版本

文章信息

发布
2025年12月18日
收录
2025年12月18日
更新
2026年12月2日
篇幅
约 18 分钟

版本

  1. v3当前
  2. v2
  3. v1

比赛情况

题目分数补题时间
T709037 补胎500.66h
T709058 最小化数组中的最大值300.04h
T710139 浮点数二分-100.15h
T711190 浮点数二分-20.49h

T709037 补胎

题意简化

给定两个正整数 nn 和 mm,以及一个数组 aa,求 max(ai×xi2)max(a_i\times x_i^2) 的最小值,其中 ∑xi=m\sum x_i=m 且 xix_i 为非负整数。

题解

1.暴力

  • 枚举从11到nn的所有xx,计算其满足条件的值,找到第一个满足条件的xx并输出。
暴力代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[200010];
bool check(long long x){
    long long cnt=0;
    for(int i=1;i<=n;i++){
        long long num=sqrt(x/a[i]);
        cnt+=num;
    }
    return cnt>=m;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    long long r=0;
    for(long long i=1;;i++){
        if(check(i)){
            r=i;
            break;
        }
    }
    cout<<r;
    return 0;
}
时间复杂度
  • 时间复杂度:O(nmax(ai)×m)O(n \sqrt{max(a_i) \times m})。
  • 由于题目数据范围为1≤n≤105,1≤ai≤100,1≤m≤1061\le n\le 10^5,1\le a_i\le 100,1\le m\le 10^6,暴力解法会超时。
  • 因此考虑二分解法。

2.二分答案

  • 设定二分搜索的范围为[1,n][1, n],对于每个中间值mid,计算其满足条件的值,如果满足条件则更新答案并缩小右边界,否则扩大左边界。最终输出答案。
二分代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[200010];
bool check(long long x){
    long long cnt=0;
    for(int i=1;i<=n;i++){
        long long num=sqrt(x/a[i]);
        cnt+=num;
    }
    return cnt>=m;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    long long r=1-1,l=1e15+1;
    while(l+1<r){
        long long mid=(l+r)/2;
        if(check(mid))r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}
时间复杂度
  • 时间复杂度:O(nlog⁡(max(ai)×m))O(n \log (max(a_i) \times m))。

T709058 最小化数组中的最大值

题意简化

给定一个长度为 nn 的数组 numsnums,每次可以选择一个下标 ii(2≤i≤n2 \leq i \leq n),将 nums[i]nums[i] 减 11,并将 nums[i−1]nums[i-1] 加 11。求经过任意次操作后,数组中的最大值的最小值。

题解

1.模拟

  • 每次选取最大值并进行操作。
模拟代码
C++
#include<bits/stdc++.h>
using namespace std;
int n,nums[100010];
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>nums[i];
    while(true){
        int mx=0,pos=0;
        for(int i=1;i<=n;i++){
            if(nums[i]>mx){
                mx=nums[i];
                pos=i;
            }
        }
        if(pos==1) break;
        nums[pos]--;
        nums[pos-1]++;
    }
    int ans=0;
    for(int i=1;i<=n;i++) ans=max(ans,nums[i]);
    cout<<ans;
    return 0;
}
时间复杂度
  • 时间复杂度:O(n×m)O(n \times m),其中 mm 是操作次数。
  • 由于题目数据范围为 2≤n≤1052 \leq n \leq 10^5,0≤nums[i]≤1090 \leq nums[i] \leq 10^9,模拟解法会超时。

2.二分答案

  • 先对数组进行排序,然后使用二分搜索来找到满足条件的最大值。在check函数中,计算每个位置上可以达到的最大值,并判断是否满足条件。最终输出二分搜索的结果。
二分代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n,a[100010],b[100010];
bool check(long long x){
    for(long long i=1;i<=n;i++){
        b[i]=a[i];
    }
    for(long long i=n;i>=2;i--){
        if(b[i]>x){
            b[i-1]+=(b[i]-x);
        }
    }
    return b[1]<=x;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i];
    }
    long long l=0,r=1e9;
    while(l+1<r){
        long long mid=(l+r)/2;
        if(check(mid)) r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}
时间复杂度
  • 时间复杂度:O(nlog⁡(max(ai)×m))O(n \log (max(a_i) \times m))。

T710139 浮点数二分-1

题意简化

求使得 xxx^x 达到或超过 nn 位数字的最小正整数 xx。

题解

1.模拟

  • 每次从 11 开始枚举 xx,计算 xxx^x 的位数,直到找到满足条件的 xx 并输出。
模拟代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n;
long long f(long long x){
    long long res=0;
    while(x){
        res++;
        x/=10;
    }
    return res;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(long long i=1;;i++){
        long long len=f(pow(i,i));
        if(len>=n){
            cout<<i<<'\n';
            break;
        }
    }
    return 0;
}
时间复杂度
  • 时间复杂度:O(x)O(x),其中 xx 是满足条件的最小正整数。
  • 由于题目数据范围为 1≤n≤2×1091 \leq n \leq 2 \times 10^9,而在C++中,long long的极限值为 263−12^{63}-1,而 xxx^x 的增长速度极快,因此模拟解法会超时。

2.二分答案

  • 二分搜索 xx 的值,计算 xxx^x 的位数,直到找到满足条件的 xx 并输出。
二分答案

由于 xxx^x 的值可能非常大,直接计算会导致溢出,所以此方法不可行。

3.数学推导+二分答案

  • 首先,我们需要了解一下对数运算。
对数运算

1.对数运算的定义:

  • 如果 ax=Na^x=N,那么 xx 就是以 aa 为底 NN 的对数,记作 x=log⁡aNx=\log_a N。
  • 所以,log⁡aN\log_a N 的含义是:要将 aa 乘以多少次才能得到 NN。

2.对数运算的性质:

  • 性质一:log⁡a(M×N)=log⁡aM+log⁡aN\log_a (M \times N)=\log_a M + \log_a N。
    证明
    • 设 log⁡aM=x\log_a M = x,log⁡aN=y\log_a N = y。
    • 根据对数的定义,我们可以知道 ax=Ma^x = M,ay=Na^y = N。
    • 那么,MN=ax×ay=ax+yMN = a^x \times a^y = a^{x+y}。
    • 因此,log⁡a(MN)=x+y=log⁡aM+log⁡aN\log_a (MN) = x + y = \log_a M + \log_a N。
    • 证毕。
  • 性质二:log⁡a=1\log_a = 1。
    证明
    • 根据对数的定义,log⁡aa=x\log_a a = x,那么 ax=aa^x = a。
    • 显然,x=1x=1 满足这个等式。
    • 因此,log⁡aa=1\log_a a = 1。
    • 证毕。
  • 性质三:log⁡a(Mx)=x×log⁡aM\log_a (M^x) = x \times \log_a M。
    证明
    • 设 log⁡aM=y\log_a M = y。
    • 根据对数的定义,我们可以知道 ay=Ma^y = M。
    • 那么,Mx=(ay)x=axyM^x = (a^y)^x = a^{xy}。
    • 因此,log⁡a(Mx)=xy=x×log⁡aM\log_a (M^x) = xy = x \times \log_a M。
    • 证毕。

上述性质是解决本题的关键

  • 根据对数的性质,我们可以将 xxx^x 的位数表示为 ⌊log⁡10(xx)⌋+1\lfloor \log_{10} (x^x) \rfloor + 1,即 ⌊x×log⁡10x⌋+1\lfloor x \times \log_{10} x \rfloor + 1。
  • 因此,我们可以二分搜索 xx 的值,计算 ⌊x×log⁡10x⌋+1\lfloor x \times \log_{10} x \rfloor + 1,直到找到满足条件的 xx 并输出。
二分代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    int l=0-1,r=2e9+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(mid*log10(mid)+1>=n) r=mid; // 注意这里的 +1 是因为位数是从 1 开始计数的
        else l=mid;
    }
    cout<<r;
    return 0;
}
时间复杂度
  • 时间复杂度:O(log⁡n)O(\log n)。

T711190 浮点数二分-2

题意简化

  • 给定一个大水缸,水温为 TT,体积为 CC 升。另有 nn 杯水,每杯水有温度 tit_i 和体积 cic_i 升。要求将大水缸的水倒入 nn 杯水中,使得 nn 杯水的温度相同,求可行的最高温度,保留 4 位小数。
  • 注意:一杯温度为 t1t_1 单位,体积为 c1c_1 升的水与另一杯温度为 t2t_2 单位,体积为 c2c_2 升的水混合后温度变为 t1×c1+t2×c2c1+c2\frac{t_1\times c_1+t_2\times c_2}{c_1+c_2},体积变为 c1+c2c_1+c_2。

题解

1.暴力

  • 每次从 11 开始枚举温度 xx,计算需要倒入每个水杯的水量,直到找到满足条件的 xx 并输出。
暴力代码
C++
#include<bits/stdc++.h>
using namespace std;
long long n,T,C;
long long t[100010],c[100010];
bool check(double x){
    double need=0;
    for(int i=1;i<=n;i++){
        if(t[i]<x){
            need+=(x-t[i])*c[i]/(T - t[i]);
        }
    }
    return need<=C;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>T>>C;
    long long maxn=-1;
    for(int i=1;i<=n;i++){
        cin>>t[i]>>c[i];
        maxn=max(maxn,t[i]);
    }
    double r=1e4+1;
    for(double i=maxn;i>=1;i-=1e-4){
        if(check(i)){
            r=i;
            break;
        }
    }
    if(r==1e4+1){
        cout<<"Impossible\n";
    }
    else{
        cout<<"Possible\n";
        cout<<fixed<<setprecision(4)<<r<<'\n';
    }
    return 0;
}
时间复杂度
  • 时间复杂度:O(n×m)O(n \times m),其中 mm 是操作次数。
  • 由于题目数据范围为 1≤n≤1051 \leq n \leq 10^5,模拟解法会超时。

2.浮点数二分答案

  • 题目可以分三种情况讨论:
    1.当 x<max(ti)x < max(t_i) 并且 x>min(ti)x > min(t_i) 时,大缸子将水加入到比温度比 TT 高的杯子之中后,水温仍然比 TT 大。
    2.当 x≤min(ti)x \leq min(t_i) 时,其他的杯子最多只能达到最小值。
    3.当 x≥max(ti)x \geq max(t_i) 时,其他的杯子最多只能达到最大值。
  • 首先考虑第一种情况。
    • 由于水温 TT 介于 max(ti)max(t_i) 和 min(ti)min(t_i) 之间,所以无法通过倒水使得所有杯子的温度相同,因此输出 Impossible。
    注意
    • 如果 max(ti)=min(ti)max(t_i) = min(t_i),则所有杯子的温度相同,直接输出 Possible 和 min(ti)min(t_i)。
  • 接下来考虑第二种情况。
    • 首先计算需要将每个杯水的温度降低到 min(ti)min(t_i) 所需的水量 sum。
    • 如果 sum <= C * (min(t_i) - T),则输出 Possible 和 min(ti)min(t_i)。
    • 否则输出 Impossible。
  • 最后考虑第三种情况。
    • 使用二分搜索来找到满足条件的最高温度 xx。
    • 在 check 函数中,计算每个杯子需要的水量,并判断是否满足条件。
    • 最终输出二分搜索的结果。
二分代码
C++
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-5;
int n;
double t[100010],c[100010],T,C,minn=1e9,maxn=-1e9;
bool check(double mid){
    double sum=0.0;
    for(int i=1;i<=n;i++){
        sum+=mid*c[i]-t[i]*c[i];
    }
    if(sum<=C*(T-mid)) return 1;
    else return 0;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    cin>>T>>C;
    for(int i=1;i<=n;i++){
        cin>>t[i]>>c[i];
        minn=min(minn,t[i]);
        maxn=max(maxn,t[i]);
    }
    cout<<fixed<<setprecision(4);
    if(T<=maxn&&T>=minn){
        if(maxn==minn){
            cout<<"Possible\n";
            cout<<minn;
        }
        else cout<<"Impossible";
    }
    else if(T<=minn){
        double sum=0;
        for(int i=1;i<=n;i++){
            sum+=(t[i]*c[i])-(minn*c[i]);
        }
        if(sum<=C*(minn-T)){
            cout<<"Possible\n";
            cout<<minn;
        }
        else cout<<"Impossible";
    }
    else{
        double l=maxn,r=T;
        while(l+eps<r){
            double mid=(r+l)/2.0;
            if(check(mid)) l=mid;
            else r=mid;
        }
        cout<<"Possible\n";
        cout<<l;
    }
    return 0;
}
时间复杂度
  • 时间复杂度: O(nlog⁡(T−max(ti)eps))O(n \log (\frac{T - max(t_i)}{eps}))。
原文