12.16总结

· · 个人记录

比赛情况

::cute-table{tuack}

题目 分数 补题时间
T709037 补胎 50 0.66h
T709058 最小化数组中的最大值 30 0.04h
T710139 浮点数二分-1 0 0.15h
T711190 浮点数二分-2 ^ 0.49h

T709037 补胎

题意简化

给定两个正整数 n 和 m,以及一个数组 a,求 max(a_i\times x_i^2) 的最小值,其中 \sum x_i=m 且 x_i 为非负整数。

题解

1.暴力

  • 枚举从1到n的所有x,计算其满足条件的值,找到第一个满足条件的x并输出。 ::::info[暴力代码]{open}
    #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;
    }

    ::::warning[时间复杂度]{open}

  • 时间复杂度:O(n \sqrt{max(a_i) \times m})。
  • 由于题目数据范围为1\le n\le 10^5,1\le a_i\le 100,1\le m\le 10^6,暴力解法会超时。
  • 因此考虑二分解法。
    2.二分答案
  • 设定二分搜索的范围为[1, n],对于每个中间值mid,计算其满足条件的值,如果满足条件则更新答案并缩小右边界,否则扩大左边界。最终输出答案。 ::::success[二分代码]{open}
    #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;
    }

    ::::: ::::success[时间复杂度]{open}

  • 时间复杂度:O(n \log (max(a_i) \times m))。

T709058 最小化数组中的最大值

题意简化

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

题解

1.模拟

  • 每次选取最大值并进行操作。 ::::info[模拟代码]{open}
    #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;
    }

    :::: ::::warning[时间复杂度]{open}

  • 时间复杂度:O(n \times m),其中 m 是操作次数。
  • 由于题目数据范围为 2 \leq n \leq 10^5,0 \leq nums[i] \leq 10^9,模拟解法会超时。
    2.二分答案
  • 先对数组进行排序,然后使用二分搜索来找到满足条件的最大值。在check函数中,计算每个位置上可以达到的最大值,并判断是否满足条件。最终输出二分搜索的结果。 ::::success[二分代码]{open}
    #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;
    }

    :::: ::::success[时间复杂度]{open}

  • 时间复杂度:O(n \log (max(a_i) \times m))。

T710139 浮点数二分-1

题意简化

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

题解

1.模拟

  • 每次从 1 开始枚举 x,计算 x^x 的位数,直到找到满足条件的 x 并输出。 ::::info[模拟代码]{open}
    #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;
    }

    :::: ::::warning[时间复杂度]{open}

  • 时间复杂度:O(x),其中 x 是满足条件的最小正整数。
  • 由于题目数据范围为 1 \leq n \leq 2 \times 10^9,而在C++中,long long的极限值为 2^{63}-1,而 x^x 的增长速度极快,因此模拟解法会超时。

2.二分答案

  • 二分搜索 x 的值,计算 x^x 的位数,直到找到满足条件的 x 并输出。 ::::error[二分答案]{open} 由于 x^x 的值可能非常大,直接计算会导致溢出,所以此方法不可行。 ::::

3.数学推导+二分答案

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

::::info[对数运算]{open}
1.对数运算的定义:

2.对数运算的性质:

::::success[二分代码]{open}

#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;
}

:::: ::::success[时间复杂度]{open}

T711190 浮点数二分-2

题意简化

  • 给定一个大水缸,水温为 T,体积为 C 升。另有 n 杯水,每杯水有温度 t_i 和体积 c_i 升。要求将大水缸的水倒入 n 杯水中,使得 n 杯水的温度相同,求可行的最高温度,保留 4 位小数。
  • 注意:一杯温度为 t_1 单位,体积为 c_1 升的水与另一杯温度为 t_2 单位,体积为 c_2 升的水混合后温度变为 \frac{t_1\times c_1+t_2\times c_2}{c_1+c_2},体积变为 c_1+c_2。

题解

1.暴力

  • 每次从 1 开始枚举温度 x,计算需要倒入每个水杯的水量,直到找到满足条件的 x 并输出。 ::::info[暴力代码]{open}
    #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;
    }

    :::: ::::warning[时间复杂度]{open}

  • 时间复杂度:O(n \times m),其中 m 是操作次数。
  • 由于题目数据范围为 1 \leq n \leq 10^5,模拟解法会超时。
    2.浮点数二分答案
  • 题目可以分三种情况讨论:
    1.当 x < max(t_i) 并且 x > min(t_i) 时,大缸子将水加入到比温度比 T 高的杯子之中后,水温仍然比 T 大。
    2.当 x \leq min(t_i) 时,其他的杯子最多只能达到最小值。
    3.当 x \geq max(t_i) 时,其他的杯子最多只能达到最大值。
  • 首先考虑第一种情况。
    • 由于水温 T 介于 max(t_i) 和 min(t_i) 之间,所以无法通过倒水使得所有杯子的温度相同,因此输出 Impossible。 :::warning[注意]{open}
    • 如果 max(t_i) = min(t_i),则所有杯子的温度相同,直接输出 Possible 和 min(t_i)。
  • 接下来考虑第二种情况。
    • 首先计算需要将每个杯水的温度降低到 min(t_i) 所需的水量 sum。
    • 如果 sum <= C * (min(t_i) - T),则输出 Possible 和 min(t_i)。
    • 否则输出 Impossible。
  • 最后考虑第三种情况。
    • 使用二分搜索来找到满足条件的最高温度 x。
    • 在 check 函数中,计算每个杯子需要的水量,并判断是否满足条件。
    • 最终输出二分搜索的结果。

::::success[二分代码]{open}

#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;
}

:::: ::::success[时间复杂度]{open}