HYOI 系列赛题解

· · 题解

Round 1

T1

求团结度和后乘 n 即可。

code:

#include<bits/stdc++.h>
using namespace std;
int cnt,n,r;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>r;
        cnt+=r;
    }
    cout<<cnt*n;
    return 0;
}

T2

对于前 50\% 的数据,按题意建图后 dfs 最大深度即可。

对于另外 20\% 的数据,树为满二叉树,\log_2 (x+1) 即为深度。

考虑完全二叉树的性质,节点尽可能向左填充,由于只求深度,所以可以当作满二叉树,深度为 {\lceil\log_2 (x+1)\rceil}。

code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long x;
    cin>>x;
    cout<<int(ceil(log2(x+1)));
    return 0;
}

T3

暴力模拟即可。

若数据开到 n,m\le 10^5,可考虑离散化+线段树+二分的 n\log^2 n 做法,不过出题人都不想写。

code:

#include<bits/stdc++.h>
using namespace std;
long long n,m,l,r,x,y,a[1005],op;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++){
        cin>>op;
        if(op==1){
            cin>>l>>r>>x;
            for(int j=l;j<=r;j++){
                a[j]+=x;
            }
        }
        else{
            cin>>l>>r>>x>>y;
            for(int j=l;j<=r;j++)
                if(a[j]>=x) a[j]-=y;
        }
    }
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    return 0;
}

T4

只有静态查询,几乎是前缀和模板。

记 s1_i=\sum_{j=1}^{i} a_j,s2_i=\sum_{j=1}^{i} a_j^2。

对于查询,输出 (s1_r-s1_{l-1}) 或 (s2_r-s2_{l-1}) 即可。

当然本题数据只有 10^5,ST 表,线段树,分块,莫队都可做。

code:

#include<bits/stdc++.h>
using namespace std;
long long n,m,r,s[100005],s2[100005],l,op;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>r;
        s[i]=s[i-1]+r;
        s2[i]=s2[i-1]+r*r;
    }
    for(int i=1;i<=m;i++){
        cin>>op>>l>>r;
        if(op==1) cout<<s[r]-s[l-1]<<'\n';
        else cout<<s2[r]-s2[l-1]<<'\n';
    }
    return 0;
}

Round 2

T1

按题意求出最大的 a_i\times b_i+V 即可。

code:

#include<bits/stdc++.h>
using namespace std;
int n,V;
int ans,a,b;
int main(){
    cin>>n>>V;
    for(int i=1;i<=n;i++) cin>>a>>b,ans=max(ans,a*b);
    cout<<ans+V;
    return 0;
}

T2

暴力模拟,双指针求最长连续段即可。

差分可以做到线性,不过这种题直接暴力显然更省脑子。

code:

#include<bits/stdc++.h>
using namespace std;
int n,x,y;
bool v[100005];
int ans,cnt;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
        for(int j=x;j<=y;j++) v[j]=1;
    }
    for(int i=0;i<=1000;i++){
        if(v[i]) cnt++;
        else{
            ans=max(ans,cnt);
            cnt=0;
        }
    }
    ans=max(ans,cnt);
    cout<<ans;
    return 0;
}

T3

发现 a_i 最小能填 1,因此 A 序列之和一定 \ge n,P<n 时无解。

由于要求字典序尽可能小,考虑前面全填 1,后面用一个较大数补。

code: ```cpp #include<bits/stdc++.h> using namespace std; long long n,P; int main(){ cin>>P>>n; if(P<n) cout<<"NO",exit(0); cout<<"YES\n"; P=P-n+1; for(int i=1;i<n;i++) cout<<"1 "; cout<<P*P; return 0; } ``` ### T4 $q\le 10^5$,只能预处理。 发现 $n\le 10^3$,可以暴力求出所有“正差”后排序。 时间复杂度为 $O((n^2 \log n^2)+q)$。 当 $n=10^3$ 时,$n^2\log n^2$ 约为 $2\times 10^7$,足以通过本题。 何况还跑不满。 code: ```cpp #include<bits/stdc++.h> using namespace std; int n,q,k,a[1005],b[500005],p; int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>q; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) b[++p]=a[j]-a[i]; sort(b+1,b+p+1); while(q--){ cin>>k; cout<<b[k]<<'\n'; } return 0; } ```