牛客周赛 Round 55 题解

A小红的字符串

#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
signed main()
{
	IOS
	//.........................//
	string s;
    cin>>s;
    sort(all(s));
    if(s[0]==s[2]){
        cout<<0<<endl;
    }
    else if(s[0]==s[1]){
        cout<<1<<endl;
    }
    else if(s[1]==s[2]){
        cout<<1<<endl;
    }
    else {
        cout<<2<<endl;
    }
}

B小红的序列乘积

直接记录每一位是否为6即可。

#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;


signed main()
{
	IOS
	//.........................//
	int n;
    cin>>n;
    int sum=1;
    int ans=0;
    for (int i=1;i<=n;i++){
        int x;
        cin>>x;
        x%=10;
        sum=sum*x;
        sum%=10;
        if(sum==6){
            ans++;
        }
    }
	cout<<ans<<endl;
	
}

C小红的数组重排

思路:只要有某一个数字出现的次数大于等于2或者0的次数大于1不行,否则直接按照从小到大的顺序即可。

#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;


signed main()
{
	IOS
	//.........................//
	int n;
    cin>>n;
    vi a(n);
    map<int,int> mp;
    for (int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    if(mp[0]>=2){
        cout<<"NO"<<endl;
        return 0;
    }
    sort(all(a));
    int cnt=1;
    for (int i=0;i<n;i++){
        cnt=1;
        if(a[i]==a[i+1]){
            while(a[i]==a[i+1]){
                cnt++;
                i++;
            }
            if(cnt>=3){
                cout<<"NO"<<endl;
                return 0;
            }
            cnt=1;
        }
    }
    cout<<"YES"<<endl;
    for (int i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
	
	
}

D虫洞操纵者

思路:时间上面给了两秒可以bfs来写,就是每一次除了往四周一格的位置转移之外,还可以在1的方格处往相反的方向的最近的1处转移。最后只需要算出最后到n,n的位置需要的移动步数即可。

(在bfs的过程注意一些细节即可)

#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

signed main()
{
	IOS
	//.........................//
	int n;
    cin>>n;
    vector<vector<int>> a(n+5,vector<int>(n+5,1));
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    vector<vector<int>> st(n+5,vector<int>(n+5,-1));
    auto bfs=[&]()->void{
        queue<pi> q;
        q.push({1,1});
        st[1][1]=0;
        while(q.size()){
            auto [x,y] = q.front();
            q.pop();
            for (int i=0;i<=3;i++){
                int nx=x+dx[i],ny=y+dy[i];
                if(a[nx][ny]==1){
                    if(i==0){
                        for(int j=y;j>=0;j--){
                            if(a[x][j]==1){
                                nx=x,ny=j+1;
                                break;
                            }
                        }
                    }
                    else if(i==1){
                        for (int j=y;j<=n+1;j++){
                            if(a[x][j]==1){
                                nx=x,ny=j-1;
                                break;
                            }
                        }
                    }
                    else if(i==2){
                        for (int j=x;j>=0;j--){
                            if(a[j][y]==1){
                                nx=j+1,ny=y;
                                break;
                            }
                        }
                    }
                    else {
                        for (int j=x;j<=n+1;j++){
                            if(a[j][y]==1){
                                nx=j-1,ny=y;
                                break;
                            }
                        }
                    }
                    if(st[nx][ny]!=-1){
                        continue;
                    }
                    st[nx][ny]=st[x][y]+1;
                    q.push({nx,ny});
                }
                else {
                    if(st[nx][ny]!=-1){
                        continue;
                    }   
                    st[nx][ny]=st[x][y]+1;
                    q.push({nx,ny});
                    
                }
            }  
        }
    };
	bfs();
    cout<<st[n][n];
}

E小红的序列乘积2.0

题解:使用动态规划来解。f[i][j] 表示在前i个数中以j的数量,g[i][j] 表示在 前i个数中j结尾的数量

#include "bits/stdc++.h"
using namespace std;

#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int> 
#define vi vector<int>
#define si set<int> 
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO  cout<<"No"<<endl;
#define pb(x) push_back(x);
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
const int N = 1e5+10;
const int mod = 1e9+7;
int f[N][10],g[N][10];

int qmi(int a,int b,int q)
{
	int ans=1;
	while(b){
		if(b&1){
			ans=ans*a%q;
		}
		
		a=a*a%q;
		b>>=1;
	}
	
	return ans%q;
}

signed main()
{
	IOS
	//.........................//
	int n;
    cin>>n;
    vi a(n+1);
    for (int i=1;i<=n;i++){
        cin>>a[i];
        a[i]%=10;//只用保留最后一位数即可
    }
    g[0][1]=1;
    for (int i=1;i<=n;i++){
        for (int j=0;j<=9;j++){
            f[i][j]=f[i-1][j]*2%mod;//每一位都可以选择是否选择
            g[i][j]=g[i-1][j];
        }
        for (int j=0;j<=9;j++){
            f[i][a[i]*j%10]=(f[i][a[i]*j%10]+g[i-1][j])%mod;
            g[i][a[i]*j%10]=(g[i][a[i]*j%10]+g[i-1][j])%mod;
        }
    }
	
    cout<<f[n][6]<<endl;
	
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值