第二届蓝桥杯模拟赛 题解

第二届蓝桥杯模拟赛

A 人肉优化

动态规划 dp 记得开long long

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
int dp[37];

void solve() {
    int x;
    cin >> x;
    dp[0]=0,dp[1]=1,dp[2]=2;
    for (int i = 3; i <= 35; ++i) {
        dp[i]=dp[i-1]*3+dp[i-2]*2+dp[i-3];
    }
    cout << dp[x] << endl;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t=1;
    //cin >> t;
    while (t--) {
        solve();
    }

}

H 拔河比赛(easy)

stack维护上一个名字

vector存储二队名字

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
void solve() {
    
    int n,m;
    cin >> n >> m;
    vector<string>s;
    stack<string>sk;
    string p;
    n+=m;
    while (n--) {
        cin >> p;
        if (p=="OUT!") {
            string l=sk.top();
            sk.pop();
            s.push_back(l);
        }else{
            sk.push(p);
        }
    }
    for(string g:s){
        cout << g << endl;
    }
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t=1;
    //cin >> t;
    while (t--) {
        solve();
    }

}

B 拔河比赛(hard)

set维护上一个名字 自定义排序

deque维护二队名字

#include <bits/stdc++.h>

#define int long long
using namespace std;

struct cmp {
    bool operator() (const pair<string, int>& l, const pair<string, int>& r) const {
        return l.second < r.second;
    }
};
signed main() {
    int n, m;
    cin >> n >> m;
    map<string,int>mp;
    string f;
    int q;
    for (int i = 0; i < n; ++i) {
        cin >> f >> q;
        mp[f]=q;
    }
    set<pair<string,int>,cmp>st;
    deque<string>de;
    string s;
    n+=m;
    while(n--){
        cin >> s;
        if (s!="min"&&s!="max") {
            st.insert({s,mp[s]});
        }else {
            string c,v;
            cin >> c >> v;
            if (s=="max"&&v=="front!"){
                de.push_front(st.rbegin()->first);  
                st.erase(prev(st.end()));            
            }else if(s=="min"&&v=="front!"){
                de.push_front(st.begin()->first);
                st.erase(st.begin());
            }else if(s=="max"&&v=="back!"){
                de.push_back(st.rbegin()->first);
                st.erase(prev(st.end()));            
            }else {
                de.push_back(st.begin()->first);
                st.erase(st.begin());
            }

        }
    }
    while(!de.empty()){
        cout << de.front() << endl;
        de.pop_front();
    }

}

D 吉利的 8(easy)

暴力

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
void solve() {

    int n;
    cin >> n;
    int cnt=0;
    vector<int>a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if ((a[i]+a[j])%8==0) {
                cnt++;
            }
        }
    }
    cout << cnt << endl;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t=1;
    //cin >> t;
    while (t--) {
        solve();
    }

}

F 寂寞双十一

只会dp做法,然后cpp代码mle了,看到其它语言内存多给一倍,换Golang过了

package main

import "fmt"

func main() {
	var A, B, C, X int
	fmt.Scan(&A, &B, &C, &X)

	n := 0
	dp := make([][]int, C+1)
	for i := range dp {
		dp[i] = make([]int, B+1)
	}

	dp[0][0] = X
	for i := 0; i <= C; i++ {
		if i > 0 {
			dp[i][0] = dp[i-1][0] - 10
		}
		if dp[i][0] < 0 {
			break
		}
		for j := 0; j <= B; j++ {
			if j > 0 {
				dp[i][j] = dp[i][j-1] - 5
			}
			if dp[i][j] < 0 {
				break
			}
			if dp[i][j] <= A {
				n++
			}
		}
	}

	if n == 0 {
		fmt.Println("Impossable")
	} else {
		fmt.Println(n)
	}
}

G 榫卯结构

找特征点,最开始看成上面四个和下面四个出现次数相等了...

用了三维数组来存(第一次用)

#include <bits/stdc++.h>

#define int long long
using namespace std;

signed main() {

    int n;
    cin >> n;
    int a=0,b=0,c=0,d=0;
    int q=0,w=0,e=0,r=0;
    char mp[n+1][3+1][3+1]; //第i个
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= n; ++j) {
            cin >> mp[j][i][1] >> mp[j][i][2] >> mp[j][i][3];
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (mp[i][1][1]=='.'&&mp[i][1][3]=='.') {
            a++;
            continue;
        }
        if (mp[i][1][3]=='.'&&mp[i][3][3]=='.') {
            b++;
            continue;
        }
        if (mp[i][3][1]=='.'&&mp[i][3][3]=='.'){
            c++;
            continue;
        }
        if (mp[i][1][1]=='.'&&mp[i][3][1]=='.') {
            d++;
            continue;
        }
        if (mp[i][3][1]=='#'&&mp[i][3][2]=='.'&&mp[i][3][3]=='#') {
            q++;
            continue;
        }
        if (mp[i][1][1]=='#'&&mp[i][2][1]=='.'&&mp[i][3][1]=='#'){
            w++;
            continue;
        }
        if (mp[i][1][1]=='#'&&mp[i][1][2]=='.'&&mp[i][1][3]=='#'){
            e++;
            continue;
        }
        if (mp[i][1][3]=='#'&&mp[i][2][3]=='.'&&mp[i][3][3]=='#') {
            r++;
            continue;
        }
    }
    if (a-q==0&&b-w==0&&c-e==0&&d-r==0)cout << "Yes\n";
    else cout << "No\n";
}

I 超级快排

排序的过

为啥我加快读还tle了,不加还过了

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10;
const int mod = 1e9 + 7;
int a[N];
int hashb[N];
int readInt() {
    int x = 0;
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar());
    for (; c >= '0' && c <= '9'; c = getchar()) {
        x = x * 10 + c - '0';
    }
    return x;
}

void solve() {
    int cnt = 0;
    map<int,int>mp;
    int n,m;
    cin >> n >> m;
    int k;
    for (int i = 1; i <= n; i++) {
        cin >> k;
        mp[k]++;
    }
    hashb[0]=0;
    for (auto nmsl : mp) {
        for (int i = 1; i <= nmsl.second; i++)
            a[++cnt] = nmsl.first;
    }
    for (int i = 1; i <= n; i++) {
        hashb[i] = (hashb[i - 1] * 114514 + a[i]) % mod;
    }
    cout << hashb[n] << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

J 回文字符串

选就行

#include<bits/stdc++.h>

using namespace std;
using ll=long long;
void solve() {
    
    string s;
    cin >> s;
    int n=s.size();
    for (int i = 0; i <= n/2; ++i) {
        s[i]=s[n-1-i]=min(s[i],s[n-1-i]);
    }
    cout << s << endl;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t=1;
    //cin >> t;
    while (t--) {
        solve();
    }

}
全部评论
F题直接暴力不就行了,还要什么dp
点赞 回复 分享
发布于 03-27 20:37 四川

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
GGrain:没事,本硕985也不发面试笔试😖
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务