Educational Codeforces Round 97 (Rated for Div. 2)

Educational Codeforces Round 97 (Rated for Div. 2)

传送门

C题

题意:每分钟拿一个菜,每个菜都有最优拿的时间,安排拿的方案,使每个菜拿的时间距离最优时间之和最小。

题解:先排序,然后考虑暴力,枚举每个菜拿的时间,排序后在后面的菜的时间,不低于前

面的时间,用dfs暴力搜索,dfs带两个参数,p为第几个菜,lim为前q个菜的最晚时间,最后加上记忆化即可AC。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int t,n,a[207];
int dp[207][407];
int dfs(int p,int lim){
    if(p==n+1)return 0;
    if(dp[p][lim]!=-1){
        return dp[p][lim];
    }
    int minn=1e9+7;
    for(int i=lim+1;i<=n*2;i++){
        minn=min(minn,dfs(p+1,i)+abs(a[p]-i));
    }
    return dp[p][lim]=minn;
}
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        printf("%d
",dfs(1,0));
    }
}

D题

题意:给一个树的bfs的序,每个点的子结点在序列必升序,求树的最短深度;

题解:写个bfs,queue存深度,每次将一段上升的序列的深度加入queue,ans记录bfs过程最大深度即可。

#include<iostream>
#include<queue>
using namespace std;
queue<int>ho;
int t,n,a[200007],task,ans;
void bfs(){
    ho.push(0);
    while(!ho.empty()){
        int deep=ho.front();
        ho.pop();
        ans=max(ans,deep);
        int last=0;
        while(task<=n){
            if(a[task]>last){
                ho.push(deep+1);
                last=a[task];
                task++;
            }
            else{
                break;
            }
        }
    }
}
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        ans=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        task=2;
        bfs();
        printf("%d
",ans);
    }
}
     
原文地址:https://www.cnblogs.com/whitelily/p/13888678.html