第十五次训练课总结

· · 题解

T1

解题思路:

  1. 我们需要找到数组中最长的连续相同元素的子数组的长度。

  2. (1) 如果当前元素与前一个元素相同,增加当前长度l,并更新ans。

    (2) 如果不同,重置当前长度l为1。

  3. 输出结果:最终ans即为最长平台的长度。

代码:

#include<bits/stdc++.h>
using namespace std;
int a[1010];
int main(){
    int n;
    cin >> n;
    int l = 1; // 当前连续相同元素的长度,初始为1
    int ans = 0; // 记录最长连续相同元素的长度
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        if(a[i] == a[i - 1] && i != 1){ // 如果当前元素与前一个相同
            l++; // 当前长度增加
            ans = max(ans, l); // 更新最大长度
        }
        else if(a[i] != a[i - 1]){ // 如果不同
            l = 1; // 重置当前长度为1
        }
    }
    cout << ans; // 输出结果
    return 0;
}

T2

解题思路: 1.给定棋盘上的一个点,输出该点所在行、列、主对角线和副对角线上的所有点。

  1. (1) 固定行号,列号从1到n。

    (2) 固定列号,行号从1到n。

    (3) 行号减列号等于给定点的行号减列号。

    (4) 行号加列号等于给定点的行号加列号。

  2. 按照指定格式输出坐标。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, a, b;
    cin >> n >> a >> b;
    // 输出行
    for(int i = 1; i <= n; i++){
        cout << "(" << a << "," << i << ")" << " ";
    }
    cout << endl;
    // 输出列
    for(int i = 1; i <= n; i++){
        cout << "(" << i << "," << b << ")" << " ";
    }
    cout << endl;
    // 输出主对角线
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i - j == a - b){
                cout << "(" << i << "," << j << ")" << " ";
            }
        }
    }
    cout << endl;
    // 输出副对角线
    for(int i = n; i >= 1; i--){
        for(int j = 1; j <= n; j++){
            if(i + j == a + b){
                cout << "(" << i << "," << j << ")" << " ";
            }
        }
    }
    return 0;
}

T3

解题思路:

1.根据输入的前缀匹配字符串,输出键盘上可能的字符。

  1. (1) 读取所有可能的字符串和前缀。

    (2) 找到所有匹配前缀的字符串的下一字符。

    (3) 根据字符是否存在,输出*或字符。

  2. 按照给定的键盘布局输出。

代码:

#include<bits/stdc++.h>
using namespace std;
string a[55];
char ch[40];
int idx = 1;
char cs[5][9] = {{}, {' ', '*', '*', '*', 'A', 'B', 'C', 'D', 'E'},
                      {' ', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'},
                      {' ', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'},
                      {' ', 'V', 'W', 'X', 'Y', 'Z', '*', '*', '*'}};
int main(){
    string s;
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    cin >> s;
    int len = s.size();
    // 匹配前缀并收集下一个字符
    for(int i = 1; i <= n; i++){
        string str = a[i].substr(0, len);
        if(str == s){
            ch[idx++] = a[i][len];
        }
    }
    // 输出键盘
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= 8; j++){
            if((i == 1 && j <= 3) || (i == 4 && j >= 6)){
                cout << "*";
            } else {
                bool f = false;
                for(int k = 1; k < idx; k++){
                    if(ch[k] == cs[i][j]){
                        cout << ch[k];
                        f = true;
                        break;
                    }
                }
                if(!f){
                   cout << "*";
                }
            }
        }
        cout << endl;
    }
    return 0;
}

T4

解题思路:

  1. 统计所有满足特定条件的子串数量。

  2. (1) 使用双指针统计满足条件的子串量。

    (2) 记录前缀中满足条件的字符数量。

  3. 最终统计的数量即为答案。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long k;
    cin >> k;
    string s;
    cin >> s;
    char a, b;
    cin >> a >> b;
    long long ans = 0;
    int l = 0, r = k - 1, len = s.size();
    long long cnt = 0;
    // 双指针统计
    while(r < len){
        if(s[l] == a){
            cnt++;
        }
        if(s[r] == b){
            ans += cnt;
        }
        l++;
        r++;
    }
    cout << ans;
    return 0;
}

T5

解题思路:

  1. 统计数组中未被切割标记分割的连续段数量。

  2. (1) 标记所有需要切割的元素。

    (2) 遍历数组,统计未被切割的连续段。

  3. 连续段的数量即为答案。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10;
int vis[N];
int a[N];
int b[N];
int main(){
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> b[i];
        vis[b[i]] = 1; // 标记切割点
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[a[i]] && vis[a[i - 1]]){
            ans++; // 统计连续段
        }
    }
    cout << ans;
    return 0;
}

T6

解题思路: 1.通过二分查找确定最大可能的卡牌套数。

  1. (1) 在可能的范围内查找最大套数。

    (2) 检查当前套数是否可行。

  2. 最大套数即为答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
int a[200010], b[200010];
bool check(int x){
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(a[i] < x){
            if(b[i] < x - a[i]){
                return false;
            }
            ans += x - a[i];
        }
    }
    return ans <= m;
}
signed main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
    }
    int l = 0, r = 1e9;
    while(l  + 1 < r){
        int mid = (l + r) / 2;
        if(check(mid)){
            l = mid;
        }else{
            r = mid;
        }
    }
    cout << l;
    return 0;
}

T7

解题思路:

  1. 统计所有满足条件的子序列数量,可以通过预处理前缀和后缀优化计算。

  2. (1) 统计每个位置前后的特定字符数量。

    (2) 遍历字符串,统计满足条件的子序数量。

  3. 最终统计的数量即为答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int pre[N], nex[N];
char ss[N];
signed main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> ss[i];
    // 预处理前缀N的数量
    for(int i = 1; i <= n; i++){
        pre[i] = pre[i - 1] + (ss[i] == 'N');
    }
    // 预处理后缀I的数量
    for(int i = n; i >= 1; i--){
        nex[i] = nex[i + 1] + (ss[i] == 'I');
    }
    int ans = 0;
    // 统计所有O的位置的贡献
    for(int i = 1; i <= n; i++){
        if(ss[i] == 'O'){
            ans += pre[i] * nex[i];
        }
    }
    // 考虑替换一个字符为O的最大增益
    int res = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        if(ss[i] == 'O') cnt += nex[i];
    }
    res = max(res, cnt);
    cnt = 0;
    for(int i = n; i >= 1; i--){
        if(ss[i] == 'O') cnt += pre[i];
    }
    res = max(res, cnt);
    for(int i = 1; i < n; i++){
        res = max(res, pre[i] * nex[i + 1]);
    }
    ans += res;
    cout << ans << endl;
    return 0;
}