2024牛客寒假算法基础集训营1

文章介绍了两个C++程序,一个是使用DFS搜索技术处理字符串中的特定字符对,另一个是运用贪心策略解决题目。涉及到字符串操作、数组遍历和逻辑判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A        DFS搜索

方法一:找到所有‘dfs’ ‘DFS’的位置,一一比较

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    vector<int> a1,a2,b1,b2,c1,c2;
    int flag1=0,flag2=0;
    int n;
    cin>>n;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='d')
            a1.push_back(i);
        else if(s[i]=='f')
            b1.push_back(i);
        else if(s[i]=='s')
            c1.push_back(i);
        else if(s[i]=='D')
            a2.push_back(i);
        else if(s[i]=='F')
            b2.push_back(i);
        else if(s[i]=='S')
            c2.push_back(i);
    }
    for(int i=0;i<a1.size();i++)
    {
        for(int j=lower_bound(b1.begin(),b1.end(),a1[i])-b1.begin();j<b1.size();j++)
        {
            if(j==b1.size())
                break;
            else
            {
                int k=lower_bound(c1.begin(),c1.end(),b1[j])-c1.begin();
                if(k==c1.size())
                    break;
                else
                {
                    flag2=1;
                    break;
                }
            }
        }
    }
    for(int i=0;i<a2.size();i++)
    {
        for(int j=lower_bound(b2.begin(),b2.end(),a2[i])-b2.begin();j<b2.size();j++)
        {
            if(j==b2.size())
                break;
            else
            {
                int k=lower_bound(c2.begin(),c2.end(),b2[j])-c2.begin();
                if(k==c2.size())
                    break;
                else
                {
                    flag1=1;
                    break;
                }
            }
        }
    }
    cout<<flag1<<" "<<flag2<<endl;
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

方法二:

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    int n;
    string s;
    cin>>n>>s;
    string s1="dfs",s2="DFS";
    int p1=0;
    int p2=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='d' && p1==0)
            p1++;
        if(s[i]=='f' && p1==1)
            p1++;
        if(s[i]=='s' && p1==2)
            p1++;
        if(s[i]=='D' && p2==0)
            p2++;
        if(s[i]=='F' && p2==1)
            p2++;
        if(s[i]=='S' && p2==2)
            p2++;
    }
    int flag1=0,flag2=0;
    if(p1==3)
        flag1=1;
    if(p2==3)
        flag2=1;
    cout<<flag2<<" "<<flag1<<endl;

}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

B        关鸡

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    set<pair<ll,ll>> st;
    int n;
    cin>>n;
    int ans=3;
    int l=2,r=2;
    for(int i=1;i<=n;i++)
    {
        ll x,y;
        cin>>x>>y;
        if((x==1&&y==1) || (x==1&&y==-1) || (x==2&&y==0))
            ans--;
        st.insert({x,y});
        if(y>=0) r=1;
        if(y<=0) l=1;
    }
    for(auto p:st)
    {
        ll x=p.first;
        ll y=p.second;
        for(int i=-1;i<=1;i++)
        {
            if(st.count({x^3,y+i}))
            {
                if(y>0) r=0;
                if(y<0) l=0;
            }
        }
    }
    cout<<min(ans,l+r)<<endl;
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

C        按闹分配

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    ll n,q,touch,m;
    cin>>n>>q>>touch;
    vector<ll> t;
    for(ll i=1;i<=n;i++)
    {
        ll temp;
        cin>>temp;
        t.push_back(temp);
    }
    vector<ll> st;
    sort(t.begin(),t.end());
    st.push_back(0);
    for(ll i=0;i<=n-1;i++)
    {
        st.push_back(st.back()+t[i]);
    }
    for(ll i=1;i<=q;i++)
    {
        cin>>m;
        ll x=m/touch;
        ll pl=max(0ll,n-x);
        cout<<st[pl]+touch<<endl;
    }
}

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

E        本题又主要考察了贪心

诈骗,明明用dfs

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

int a[20];
int n,m;
int s1[20],s2[20];
int ans=1;
int res=1000;

void dfs(int step)
{
    if(step==m+1)
    {
        int op=a[1];
        int b[20];
        for(int i=1;i<=n;i++)
            b[i]=a[i];
        sort(b+1,b+n+1,greater<int>());
        int ch=b[1];
        if(ch==op)
        {
            ans=1;
        }
        else
        {
            for(int i=2;i<=n;i++)
            {
                ans++;
                if(b[i]==op)
                    break;
            }
        }
        res=min(res,ans);
        ans=1;
        return;
    }
    else
    {
        int n1=s1[step];
        int n2=s2[step];
        a[n1]+=3;
        dfs(step+1);//
        a[n1]-=3;
        a[n2]+=3;
        dfs(step+1);//
        a[n2]-=3;
        a[n1]+=1;
        a[n2]+=1;
        dfs(step+1);//
        a[n1]-=1;
        a[n2]-=1;
    }
    return;
}

void solve()
{
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    memset(a,0,sizeof(a));
    ans=1;
    res=1000;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=m;i++)
        cin>>s1[i]>>s2[i];
    dfs(1);
    cout<<res<<endl;
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

G        why买外卖

贪心

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

struct node
{
    ll a,b;
};

bool cmp(node x,node y)
{
    return x.a<y.a;
}
void solve()
{
    ll n,m;
    cin>>n>>m;
    vector<node> d;
    ll res=m;
    for(ll i=1;i<=n;i++)
    {
        node temp;
        cin>>temp.a>>temp.b;
        d.push_back(temp);
        res+=temp.b;
    }
    sort(d.begin(),d.end(),cmp);
    for(int i=d.size()-1;i>=0;i--)
    {
        if(res<d[i].a)
        {
            res-=d[i].b;
        }
        else
            break;
    }
    cout<<res<<endl;
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

I        It's bertrand paradox. Again!

模拟找规律

#include<bits/stdc++.h>
#define endl "\n"
#define pb push_back
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    double n;
    cin>>n;
    vector<double> xi,yi,ri;
    for(int i=1;i<=n;i++)
    {
        int x,y,r;
        cin>>x>>y>>r;
        xi.pb(x);
        yi.pb(y);
        ri.pb(r);
    }
    double sum=0.0;
    for(auto i:ri)
    {
        sum+=i;
    }
    sum/=n;
    if(sum<20.0)    cout<<"bit-noob"<<endl;
    else    cout<<"buaa-noob"<<endl;
    return 0;
}

L        要有光

初中数学题,非常简单

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    ll c,d,h,w;
    cin>>c>>d>>h>>w;
    cout<<w*3*c<<endl;
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

M        牛客老粉才知道的秘密

找规律,非常简单

#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int inf=INT_MAX;
const int mod=1e9+7;
const int maxn=1e5+7;

void solve()
{
    ll n;
    cin>>n;
    if(n%6==0)
    {
        cout<<n/6<<endl;
    }
    else
    {
        ll ans=2*(n/6);
        cout<<ans<<endl;
    }
}

signed main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值