260408 OI 总结

· · 个人记录

OI 总结

Problem A B C D
Scores 0pts 40pts 0pts 10pts

第一题没看懂题目,都不知道最小排名和最大排名是怎么算的;

第二题推了个假解,第三题没推出公式;

第四题我已经把最大值,第二大值,第三大值求出来了,但当时只想得个 75 分,就写了个枚举三个点的,不知道怎么的炸了。

由此看来,我的代码能力、数学和逻辑都要加强。。。

错题回顾

A

题意

有 m 个考生和 n 道题,给出一位考生每道题的得分(0 \sim 1 的任意实数)排名,求他最后的总得分排名最小和最大是多少。

思路

因为得分是 0 \sim 1 的任意实数,所以我们可以任意分配得分。

首先我们考虑最小名次,即最高名次。只有全赢了那位考生的考生,排名才会稳稳地在那位考生之上,所以那位考生的最高名次就是全赢了他的考生数量加一。

然后我们考虑最大名次,即最低名次。只有全输了那位考生的考生,排名才会稳稳地在那位考生之下(咋感觉那么怪嘞),所以那位考生的最高名次就是 m 减全输了他的考生数量。

时空复杂度

Time complexity: O(n)

Mem complexity: O(n)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

int const N=1e4+10;
int n,m,an[N],sum,tot; 

void solve(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>an[i];
        sum+=m-an[i];
        tot+=an[i]-1;
    }
    cout<<m-min(m-1,sum)<<"\n"<<1+min(m-1,tot);
    return;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--) solve();
    return 0;
}

B

题意

给定两个空集合 A 和 B,以及 n 次询问。每次询问,先在 A 和 B 中各放进去一个数,然后让你求将 A,B 中的数两两配对,使得其中最大数对的和最小。

思路

首先贪心地,每次选一个集合的较大值和另一个集合的较小值配对是最优的。

又因为 A,B 中的数最大就 100,所以,我们用一个桶记录在 A,B 中每个数出现的个数,用双指针枚举较大值和较小值,加起来取最大值,然后减去次数,继续移动指针,最后输出答案即可。

时空复杂度

Time complexity: O(n)

Mem complexity: O(n)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

int const N=1e5+10;
int n,an[N],bn[N];

void solve(){
    cin>>n;
    while(n--){
        int a,b;
        cin>>a>>b;
        an[a]++,bn[b]++;
        int l=0,r=101;
        int ta=0,tb=0,ans=0;
        while(l<=100 && r>=1){
            if(!ta){
                l++,ta=an[l];
                continue;
            }
            if(!tb){
                r--,tb=bn[r];
                continue;
            }
            ans=max(ans,l+r);
            ta--,tb--;
        }
        cout<<ans<<"\n";
    }
    return;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--) solve();
    return 0;
}

C

题意

有 n 块石板围成一个圈,现在要给这些石板染色。

有 m 种颜料,要求相邻两块石板的颜色必须不同,求有多少种染法。

思路

我们先考虑 n 块石板排成一排,那么第一块能染 m 种颜色,第二块就只能染 m-1 种颜色,第二块也只能染 m-1 种颜色。。。总共 m(m-1)^{n-1} 种方案,记作 f(n)。

但是,n 块石板围成了一个圈,也就是说第一块石板和最后一块石板连在了一起,那我们就把它们看成一个点,于是这种情况带来的不合法的方案数就为 f(n-1)。

所以最终方案数就是 f(n)=m(m-1)^{n-1}-f(n-1),化简后为 f(n)=(m-1)^n+(-1)^n(m-1),用快速幂计算即可。

注意取模!!!

时空复杂度

Time complexity: O(\log n)

Mem complexity: O(1)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

ll const MOD=1000000007;

ll qpow(ll a,ll b){
    ll ret=1;
    while(b){
        if(b&1) ret=ret*a%MOD;
        a=a*a%MOD,b>>=1; 
    }
    return ret;
}

void solve(){
    ll n,m;
    cin>>n>>m;
    if(n==1){
        cout<<m<<"\n";
        return ;
    }
    cout<<(qpow(m-1,n)+(n&1?-1:1)*(m-1)+MOD)%MOD<<"\n";
    return;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T=1;
    cin>>T;
    while(T--) solve();
    return 0;
}

D

题意

给定一个 n 个点 m 条边的无向图,让你找出 4 个互不相同点 a,b,c,d,满足 a \to b 的最短路加b \to c 的最短路加c \to d 的最短路最大。

思路

我们可以枚举中间两个点 b,c,那么 a 就是离 b 最远的点,d 就是离 c 最远的点,预处理 BFS 维护一下,把这三段加起来取最大值即可。

但是,万一离 b 最远的点是 c 呢?那我们只能维护一个第二大值,如果 b 最远的点是 c,那么就用第二大值来选 a,c,d 同理。

但是,万一选好 a 后,离 c 最远的点是 a 呢?那只能又维护一个第三大值,如果 c 最远的点是 a,那么就用第三大值来选 d。

这样就万无一失了。

时空复杂度

Time complexity: O(n \times (n+m))

Mem complexity: O(n^2)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

int const N=5e3+10;
int n,m,dp[N][N],mx[N],mid[N],cmx[N],cid[N],tmx[N],tid[N],fmx[N],fid[N],ans;
vector<int> g[N];

void bfs(int st){
    queue<int> q;
    for(int i=1;i<=n;i++) dp[st][i]=1e9,mx[i]=cmx[i]=tmx[i]=fmx[i]=-1e9;
    q.push(st),dp[st][st]=0;
    while(q.size()){
        int u=q.front();q.pop();
        if(dp[st][u]>mx[u]){
            fmx[st]=tmx[st],fid[st]=tid[st];
            tmx[st]=cmx[st],tid[st]=cid[st];
            cmx[st]=mx[st],cid[st]=mid[st];
            mx[st]=dp[st][u],mid[st]=u;
        }
        else if(dp[st][u]>cmx[u]){
            fmx[st]=tmx[st],fid[st]=tid[st];
            tmx[st]=cmx[st],tid[st]=cid[st];
            cmx[st]=dp[st][u],cid[st]=u;
        }
        else if(dp[st][u]>tmx[u]){
            fmx[st]=tmx[st],fid[st]=tid[st];
            tmx[st]=dp[st][u],tid[st]=u;
        }
        else if(dp[st][u]>fmx[u]) fmx[st]=dp[st][u],fid[st]=u;
        for(int v:g[u]){
            if(dp[st][v]>dp[st][u]+1){
                dp[st][v]=dp[st][u]+1;
                q.push(v);
            }
        }
    }
    return ;
}

void solve(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1;i<=n;i++) bfs(i);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j) continue;
            int dis=dp[i][j],id;
            if(mid[i]!=i && mid[i]!=j && mx[i]>0) id=mid[i],dis+=mx[i];
            else if(cid[i]!=i && cid[i]!=j && cmx[i]>0) id=cid[i],dis+=cmx[i];
            else if(tmx[i]>0) id=tid[i],dis+=tmx[i];
            else continue;
            if(mid[i]!=i && mid[i]!=j && mid[i]!=id && mx[i]>0) id=mid[i],dis+=mx[i];
            else if(cid[i]!=i && cid[i]!=j && cid[i]!=id && cmx[i]>0) id=cid[i],dis+=cmx[i];
            else if(tid[i]!=i && tid[i]!=j && tid[i]!=id && tmx[i]>0) id=tid[i],dis+=tmx[i];
            else if(fmx[i]>0) id=fid[i],dis+=fmx[i];
            else continue;
            ans=max(ans,dis);
        }
    }
    cout<<ans;
    return;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--) solve();
    return 0;
}