【码蹄集】— 码蹄杯 2023省赛第一场

MC0201

a + b a+b a+b

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    int a,b; cin >> a >> b;
    cout << a + b << endl;
} 

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

MC0202

暴力枚举

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    int l,r; char x; cin >> l >> x >> r;
    int sum = 0;
    for(int i = l;i <= r;i ++){
        if(i % 2 || i % 3) continue;
        sum += i;
    }
    cout << sum << endl;
} 

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

MC0203

map更新分数和最后一次操作的位置即可

然后找最小分数 如果最小分数相同就看更新的位置

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    cin >> n;
    map<string,pair<int,int>> mp;
    for(int i = 0;i < n;i ++){
        string s; cin >> s;
        int x; cin >> x;
        mp[s] = {x + mp[s].first,i};
    }
    int mn = 1e18,pos = 1e18;
    string ans;
    for(auto x : mp){
        if(x.second.first < mn) mn = x.second.first,ans = x.first,pos = x.second.second;
        else if(x.second.first == mn && x.second.second < pos) mn = x.second.first,ans = x.first,pos = x.second.second;
    }
    cout << ans << endl;
    cout << mn + 1000 << endl;
} 

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

MC0204

求最长连续不重复子序列长度

双指针即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e6 + 7;
int n;
int c[N];

void solve(){
    cin >> n;
    for(int i = 0;i < n;i ++) cin >> c[i];
    map<int,int> mp;
    int mx = 1;
    for(int i = 0,j = 0;i < n;i ++){
        mp[c[i]] ++;
        while(j < i && mp[c[i]] > 1) mp[c[j ++]] --;
        mx = max(i - j + 1,mx);
    }
    cout << mx << endl;
} 

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

MC0205

找最大字符

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    char ans = *max_element(s.begin(),s.end());
    cout << ans << endl;
} 

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

MC0206

只会有两种情况:

ABABAB...

BABABA...

看两种情况最低处理操作即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    int n = s.size();
    int ans1 = 0,ans2 = 0;
    for(int i = 0;i < n;i ++){
        if(i % 2){
            if(s[i] == 'A') ans1 ++;
            else ans2 ++;
        } else{
            if(s[i] == 'B') ans1 ++;
            else ans2 ++;
        }
    }
    cout << min(ans1,ans2) << endl;
} 

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

MC0207

单向建图 dfs从1开始跑一遍看是否能到n即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 2e5 + 7;
vector<int> g[N];
int n,m;
int flag;
int st[N];

void dfs(int u){
    if(flag || u == n){
        flag = 1;
        return;
    }
    st[u] = 1;
    for(auto t : g[u]){
        if(st[t]) continue;
        dfs(t);
    }
}

void solve(){
    cin >> n >> m;
    for(int i = 0;i < m;i ++){
        int u,v; cin >> u >> v;
        g[u].push_back(v);
    }
    dfs(1);
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
} 

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

MC0208

看是否是10的倍数 不是就*2

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    cin >> n;
    if(n % 10) cout << 2 * n << endl;
    else cout << 0 << endl;
} 

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

MC0209

分长度奇偶即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    string s; cin >> s;
    n = s.size();
    if(n & 1) cout << "123456" << endl;
    else{
        reverse(s.begin() + n / 2,s.end());
        cout << s << endl;
    }
} 

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

MC0210

暴力判断即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    string s; cin >> s;
    n = s.size();
    for(int i = n / 3;i >= 1;i --){
        string s1 = s.substr(0,i);
        string s2 = s.substr(n - i,i);
        if(s1 == s2){
            string s3 = s.substr(i,n - 2 * i);
            if(s3.find(s1) != s3.npos){
                cout << s1 << endl;
                return;
            }
        }
    }
    cout << "No" << endl;
} 

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

MC0211

模拟

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    int pos = s.size() - 1;
    for(int i = pos;i >= 0;i --){
        if(s[i] != '0'){
            pos = i;
            break;
        }
    }
    if(s.size() == 1) cout << s << endl;
    else{
        cout << s[0];
        if(pos != 0) cout << ".";
        for(int i = 1;i <= pos;i ++) cout << s[i];
        cout << "E" << s.size() - 1 << endl;
    }
} 

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

MC0212

code:

cpp 复制代码

MC0213

按相同边长分类讨论

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    map<int,int> mp;
    for(int i = 0;i < 5;i ++){
        int x; cin >> x;
        mp[x] ++;
    }
    if(mp.size() == 1) cout << "Square" << endl;
    else if(mp.size() == 2){
        if(mp.begin()->second == 4 || mp.begin()->second == 1) cout << "Square" << endl;
        else cout << "Rectangle" << endl;
    } else if(mp.size() == 3){
        int cnt = 0;
        for(auto x : mp) if(x.second == 2) cnt ++;
        if(cnt == 2) cout << "Rectangle" << endl;
        else cout << "No" << endl;
    }
    else cout << "No" << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}
相关推荐
im_AMBER14 分钟前
算法笔记 05
笔记·算法·哈希算法
夏鹏今天学习了吗20 分钟前
【LeetCode热题100(46/100)】从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
吃着火锅x唱着歌20 分钟前
LeetCode 2389.和有限的最长子序列
算法·leetcode·职场和发展
嶔某33 分钟前
二叉树的前中后序遍历(迭代)
算法
WWZZ20251 小时前
快速上手大模型:机器学习2(一元线性回归、代价函数、梯度下降法)
人工智能·算法·机器学习·计算机视觉·机器人·大模型·slam
孤狼灬笑1 小时前
深度学习经典分类(算法分析与案例)
rnn·深度学习·算法·cnn·生成模型·fnn
dragoooon341 小时前
[优选算法专题四.前缀和——NO.26二维前缀和]
算法
苏小瀚2 小时前
算法---位运算
java·算法
Code小翊2 小时前
归并排序基础理解
数据结构·算法·排序算法
2401_841495642 小时前
【数据结构】基于Floyd算法的最短路径求解
java·数据结构·c++·python·算法··floyd