A - 子集和问题

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;

int n,c;
int a[N];
int sum=0;
bool st[N];

void DFS(int x){
    if(sum>c) return ;
    if(x>n) return ;
    st[x]=1;
    sum+=a[x];
    if(sum==c){
        bool tag=0;
        for(int i=1;i<=x;i++){
            if(st[i]){
                if(!tag) tag=1;
                else cout<<" ";
                cout<<a[i];
            }
        }
        cout<<endl;
        exit(0);
    }else if(sum<c) DFS(x+1);
    st[x]=0;
    sum-=a[x];
    DFS(x+1);
}

inline void solve(){
    cin>>n>>c;
    int su=0;
    for(int i=1;i<=n;i++) cin>>a[i],su+=a[i];
    if(su<c){
        cout<<"No Solution!"<<endl;
        return ;
    }else if(su==c){
        bool tag=0;
        for(int i=1;i<=n;i++){
            if(!tag) tag=1;
            else cout<<" ";
            cout<<a[i];
        }
    }else{
        DFS(1);
        cout<<"No Solution!"<<endl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

B - 运动员最佳匹配问题

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
const int N = 25;
using namespace std;

int n;
int p[N][N];
int q[N][N];
int g[N][N];
int mg[N];
int maxn=INT_MIN;
int sum=0;
bool st[N];

void DFS(int x){
    //cout<<x<<endl;
    if(x>n){
        maxn=max(maxn,sum);
        return ;
    }
    int cnt=0;
    for(int i=x;i<=n;i++) cnt+=mg[i];
    if(sum+cnt<maxn) return ;
    for(int i=1;i<=n;i++){
        if(!st[i]){
            st[i]=1;
            sum+=g[x][i];
            DFS(x+1);
            sum-=g[x][i];
            st[i]=0;
        }
    }
}

inline void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>p[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>q[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            g[i][j]=p[i][j]*q[j][i];
            mg[i]=max(mg[i],g[i][j]);
        }
    }
    DFS(1);
    cout<<maxn<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

C - 工作分配问题

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
const int N = 25;
using namespace std;

int n;
int mp[N][N];
int minn=INT_MAX;
int sum=0;
bool st[N];

void DFS(int x){
    if(x>n){
        minn=min(minn,sum);
        return ;
    }
    for(int i=1;i<=n;i++){
        if(!st[i]){
            st[i]=1;
            sum+=mp[i][x];
            if(sum<=minn) DFS(x+1);
            st[i]=0;
            sum-=mp[i][x];
        }
    }
}

inline void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>mp[i][j];
        }
    }
    DFS(1);
    cout<<minn<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

D - 整数变换问题

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;

int n,m;
int tot=1;
bool st[N];
string s="";

bool DFS(int x,int num){
    if(x>tot) return 0;
    if(num==m) return 1;
    auto temp=num;
    for(int i=0;i<2;i++){
        if(!i) temp=num*3;
        else temp=num/2;
        if(DFS(x+1,temp)){
            if(!i) s+='f';
            else s+='g';
            return 1;
        }
    }
    return 0;
}

inline void solve(){
    cin>>n>>m;
    while(!DFS(0,n)) tot++;
    cout<<tot<<endl;
    cout<<s<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐