入队考核 || 代码公示——3.11 12点场

1001

#include<bits/stdc++.h>
using namespace std;
#define maxn 10010
int n;

int main()
{
    priority_queue<int,vector<int>,greater<int> > que;
    cin>>n;
    while(n--)
    {
        int x;
        cin>>x;
        que.push(x);
    }
    int ans=0;
    while(que.size()>1)
    {
        int a=que.top();
        que.pop();
        int b=que.top();
        que.pop();
        ans+=a+b;
        que.push(a+b);


    }
    cout<<ans<<endl;
    return 0;
}

1002

#include<bits/stdc++.h>
using namespace std;
#define maxn 110
int dp[maxn];
int n,m;
int w[maxn],val[maxn];
int main()
{
    cin>>m>>n;
    for(int i=0;i<n;i++)
    {
        cin>>w[i]>>val[i];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=w[i];j<=m;j++)
        {
            dp[j]=max(dp[j],dp[j-w[i]]+val[i]);
        }
    }
    cout<<"max="<<dp[m];
    return 0;
}

1003 。。没写出来,忘记字典树要怎么构建了。。想投机取巧用两个for循环看看能不能过,结果不出意料的超时了。

1004

#include<bits/stdc++.h>
using namespace std;
#define maxn 50
char maze[maxn][maxn];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int d[maxn][maxn];
int sum=0;
int n,m;
struct node
{
    int x;
    int y;

};
bool check(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m)
    return true;
    return false;
}
int bfs()
{
    node now,next;
    queue<node> que;
    memset(d,-1,sizeof(d));
    d[0][0]=0;

    now.x=now.y=0;
    maze[0][0]='#';
    que.push(now);
    
    while(!que.empty())
    {
        
        now=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(check(next.x,next.y)&&maze[next.x][next.y]=='.')
            {
                maze[next.x][next.y]='#';
                d[next.x][next.y]=d[now.x][now.y]+1;
                que.push(next);
                
                
            }
        }
      
    }

    
return d[n-1][m-1]+1;

}
int main()
{
    
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>maze[i][j];
        }
    }
    cout<<bfs()<<endl;
    return 0;
}

1005

#include<bits/stdc++.h>
using namespace std;
#define maxn 200010
int p[maxn],a[maxn];
int n,m;
int d[maxn];
int find(int x)
{
    if(p[x]!=x) p[x]=find(p[x]);
    return p[x];
}

void uni(int x,int y)
{
    int a=find(x);
    int b=find(y);
    if(a!=b)
    {
        p[a]=b;
        d[b]+=d[a];
    }

}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        p[i]=i;
        d[i]=1;
    }
    while(m--)
    {
        char op[2];
        
        scanf("%s",op);
        if(op[0]=='M')
        {
            int a,b;
            cin>>a>>b;
            uni(a,b);

        }
        else
        {
            int x;
            scanf("%d",&x);
            cout<<d[find(x)]<<endl;
            
        }
    }

    return 0;
}

1006

#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
int n,x,y,d;
int main()
{
    int res=INF;
    int a,b;//b da a xiao
    cin>>n>>x>>y;
    if(x>y)
    {
        a=y;
        b=x;

    }
    else
    {
        a=x;
        b=y;
    }
    d=n/b;
    for(int i=0;i<d;i++)
    {
        if((n-(i*b))%a==0) 
        {
            res=min(res,a);
        }
        else
        res=min(res,(n-(i*b))%a);


    }
    cout<<res;
    return 0;

}

最终结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

captainfly_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值