winter week2 day5

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

A

DFS搜索

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    int n;
    cin>>n;
    string s;
    cin>>s;
    int a=0,b=0;
    for(int i=0;i<s.size();++i){
        if(a==0&&s[i]=='d')a++;
        else if(a==1&&s[i]=='f')a++;
        else if(a==2&&s[i]=='s')a++;
        if(b==0&&s[i]=='D')b++;
        else if(b==1&&s[i]=='F')b++;
        else if(b==2&&s[i]=='S')b++;
    }
    if(b==3)cout<<1<<' ';
    else cout<<0<<' ';
    if(a==3)cout<<1<<'\n';
    else cout<<0<<'\n';
}


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

 


B

关鸡

思路:注意每种情况

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    int n,ans=3;
    cin>>n;

    map<PII,int>mp;
    int l=2,r=2;
    for(int i=0;i<n;++i){
        int a,b;
        cin>>a>>b;
        mp[{a,b}]=1;
    }
    auto P=[&mp](int a,int b){
        int x=2;
        if(a==1){
            if(mp.count({2,b-1})){
                x=min(x,0ll);
//                cout<<"*1*";
            }
            else if(mp.count({2,b})){
                x=min(x,0ll);
//                cout<<"*2*";
            }
            else if(mp.count({2,b+1})){
                x=min(x,0ll);
//                cout<<"*3*";
            }
            else {
                x=min(x,1ll);
//                cout<<"*4*";
            }
//            cout<<'\n';
        }else{
            if(mp.count({1,b-1}))x=min(x,0ll);
            else if(mp.count({1,b}))x=min(x,0ll);
            else if(mp.count({1,b+1}))x=min(x,0ll);
            else x=min(x,1ll);
        }
//        cout<<"*"<<x<<'\n';
        return x;
    };
    for(auto [u,v]:mp){
        int a=u.first,b=u.second;
        if(b<0)l=min(l,P(a,b));
        if(b>0)r=min(r,P(a,b));
    }
    if(mp.count({2,0})){
        if(l==2)l=1;
        else if(r==2)r=1;
    }
//    cout<<l<<' '<<r<<'\n';
    ans=min(ans,l+r);
    int cnt=0;
    if(mp.count({1, -1}))cnt++;
    if(mp.count({1,1}))cnt++;
    if(mp.count({2,0}))cnt++;
    ans=min(ans,3-cnt);
    cout<<ans<<'\n';
}

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

 


C

按闹分配

思路:二分位置

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    int n,Q,tc;
    cin>>n>>Q>>tc;
    vector<int>ve(n+1),pre(n+5);
    for(int i=1;i<=n;++i)cin>>ve[i];
    sort(ve.begin()+1,ve.end());
    for(int i=1;i<=n;++i){
        ve[i]+=ve[i-1];
        pre[i]=pre[i-1]+ve[i];
    }
    for(int q=0;q<Q;++q){
        int m;
        cin>>m;
        auto check=[n,tc,m](int x){
            int c=n-x+1;
            return c*tc<=m;
        };
        int l=1,r=n+1,x;
        while(l<=r){
            int mid=l+r>>1;
            if(check(mid))r=mid-1,x=mid;
            else l=mid+1;
        }
        cout<<ve[x-1]+tc<<'\n';
    }
}


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

 


E

本题又主要考察了贪心

思路:暴力

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    int n,m;
    cin>>n>>m;
    int ans=n;
    vector<int>ve(n+1);
    vector<PII>g;
    for(int i=1;i<=n;++i)cin>>ve[i];
    for(int i=0;i<m;++i){
        int u,v;
        cin>>u>>v;
        if(u==1||v==1){
            ve[1]+=3;
        }else{
            g.push_back({u,v});
        }
    }
    int nn=g.size();

    for(int i=0;i<pow(3,nn);++i){
        vector<int>f=ve;
        int ii=i,res=1;
        for(int j=0;j<nn;++j){
            if(ii%3==1){
                f[g[j].first]+=3;
            }else if(ii%3==2){
                f[g[j].second]+=3;
            }else{
                f[g[j].first]+=1;
                f[g[j].second]+=1;
            }
            ii/=3;
        }
        for(int k=2;k<=n;++k){
            if(f[k]>f[1])res++;
        }
        ans=min(ans,res);
    }
    cout<<ans<<'\n';
}


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

 


F

鸡数题!

思路:由条件可知,要用m个数异或出2n-1,并且任意两个数按位与后为0,以二进制形式看,就是m个数中1的个数和为n。问题就可以看成n个位置的1分配到m个数里,由于每个位置都只有一个,那么分配后的m个数的最高位都是不一样的,也就保证了递增的条件。

这个时候就直接用8种球盒关系中的(0,1,0),n个不同的球分到r个相同的盒子且不存在空盒子,也叫第二类Stirling数

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e5+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
int fact[N],infact[N];
int ksm(int a,int k,int p){
    int res=1;
    while(k){
        if(k&1)res=res*a%p;
        k>>=1;
        a=a*a%p;
    }
    return res;
}
void init(){
    fact[0] = infact[0] = 1;
    for (int i = 1 ; i < N; ++i) {
        fact[i] = fact[i - 1] * i % mod;
        infact[i] = infact[i - 1] * ksm(i, mod - 2, mod) % mod;
    }
    //C(a,b)=fact[a]*infact[a-b]%mod*infact[b]%mod;
}
int C(int a,int b){
    return fact[a]*infact[a-b]%mod*infact[b]%mod;
}
void solve() {
    int n,m;
    cin>>n>>m;
    if(m>n){
        cout<<0;
        return ;
    }
    init();
    int ans=0;
    for(int i=0;i<=m;++i){
        if(i%2){
            ans=(ans-C(m,i)*ksm(m-i,n,mod)%mod+mod)%mod;
        }else{
            ans=(ans+C(m,i)*ksm(m-i,n,mod)%mod)%mod;
        }
    }
    cout<<ans*infact[m]%mod;
}


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

 


G

why买外卖

思路:枚举所有a

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    int n ,m;
    cin>>n>>m;
    map<int,int>mp;
    for(int i=0;i<n;++i){
        int a,b;
        cin>>a>>b;
        mp[a]+=b;
    }
    int now=0,ans=m;
    for(auto [a,b]:mp){
        now+=b;
        if(m>=a-now)ans=max(ans,m+now);
    }
    cout<<ans<<'\n';
}


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

 


H

01背包,但是bit

思路:重量由所选物品按位或而得,那就从二进制看重量。

对于m的每一位1,把1变成0,那么1后面的二进制可以任意取,保证前面的二进制不会有新的1出现的情况下的重量都是可取的

其实就是枚举了m的二进制上的所有选取情况

统计最大的答案

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e5+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};

void solve() {
    int n,m;
    cin>>n>>m;
    vector<int>v(n),w(n);
    for(int i=0;i<n;++i)cin>>v[i]>>w[i];
    int ans=0;
    for(int i=0;i<n;++i)
        if((w[i]|m)<=m)ans+=v[i];
    for(int i=m;i>0;i-=i&-i){
        int s=0;
        for(int j=0;j<n;++j){
            if((w[j]|(i-1))<=(i-1))s+=v[j];
        }
        ans=max(ans,s);
    }
    cout<<ans<<'\n';
}


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

 

 


I

It's bertrand paradox. Again!

思路:n为1e5,可以从概率方面看,bit是圆心概率一定,buaa是半径概率一定。若圆心概率一定的话,可以求出在一片区域内圆心出现的期望次数,用实际出现次数与期望次数比较即可判断是bit还是buaa

查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};

void solve() {
    int k=20;
    int a=k*k*10;
    int cnt=0;
    int n;
    cin>>n;
    for(int i=0;i<n;++i){
        int x,y,r;
        cin>>x>>y>>r;
        if(max(abs(x),abs(y))<k)cnt++;
    }
    if(cnt<=a)cout<<"bit-noob";
    else cout<<"buaa-noob";
}


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

 


L

要有光

思路:阴影的面积是梯形,推边的式子

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



void solve() {
    double c,d,h,w;
    cin>>c>>d>>h>>w;
    double x=h*(x+c)/d;
    double hh;
    if(d>h&&x<c)hh=x;
    else hh=c;
    double b=2*w*(c+hh)/c,a=2*w;
    double ans=(a+b)*hh/2;
    cout<<fixed<<setprecision(9)<<ans<<'\n';
}


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

 


M

牛客老粉才知道的秘密

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=100+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-12;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};



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


signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
posted @ 2024-02-23 19:51  bible_w  阅读(6)  评论(0编辑  收藏  举报