总结

· · 个人记录

总结

比赛编号:\color{black}{\textsf{292561}}

题目编号 题目类型 题目分数 提交状态
\color{green}{\texttt{B4333}} \color{black}{\texttt{模拟}}+\color{black}{\texttt{字符串}} 100 \color{green}{\textsf{AC}}
\color{red}{\texttt{P12870}} \color{black}{\texttt{贪心}} 90 \color{red}{\textsf{UNAC}}
\color{red}{\texttt{P12249}} \color{black}{\texttt{贪心}} 5 \color{red}{\textsf{UNAC}}
\color{red}{\texttt{P12612}} \color{black}{\texttt{双指针}} 0 \color{red}{\textsf{WA}}
\color{#454545}{\texttt{P12257}} \color{black}{\texttt{双指针}} 0 \color{white}{\textsf{未交}}
\color{#454545}{\texttt{P13521}} \color{black}{\texttt{双指针}} 0 \color{white}{\textsf{未交}}

T1

题目逻辑:

在一个字符串中找到最长且每个字符相等的子串\\ 所以可以建一个map,定义为map<char,int>~~\\ 然后再标记当前记得字符~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 如果记得字符和当前字符不同就将上一个字符连\\ 续的数量与map中的数取最大值~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 最终遍历char(97\sim122)求出map中的最大值~~~~~\\ 最后输出最大值即可~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 5*(1e5+5);
string s;//字符串
map<char,int> num;//每一个字符都最大值
char bj;//标记上一个字符
int n;//连续数
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> s;
    bj=s[0];n=1;
    for(int i = 1;i<s.size();i++){//逐位遍历
        if(s[i]!=bj){//不同
            num[bj]=max(num[bj],n);//比大小
            n=1;//标记
            bj=s[i];//更新
        }else n++;//增加连续数
    }
    int maxn=0;//统计最大值
    for(int i = 97;i<=122;i++){
        maxn=max(maxn,num[char(i)]);
    }
    cout << maxn;
    return 0;
}

T2

错误代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 5*(1e5+5);
int n;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    cout << 1;
    if(n==1) return 0; 
    while(n){
        cout << 0;
        n/=10;
    }
    return 0;
}

题目逻辑:

输入一个数n,并将n转为字符串s,然后创建一个\\ 变量ans,遍历总1到|s|-1,每次将ans\times10~~~~~~~~\\ 如果ans\ge n,那么输出n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 否则输出ans\times10~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

正确代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 5*(1e5+5);
int n;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    string s=  to_string(n);//转字符串
    ll ans = 1;
    for(int i = 1;i<=s.size()-1;i++){//遍历
        ans*=10;
    }
    if(ans>=n) cout << n;//输出
    else cout << ans*10;
    return 0;
}

T3

错误代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 3*(1e4+4);
int a[M],b[M];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;cin >> t;
    while(t--){
        int ans=0;
        int n,m;
        cin >> m >> n;
        for(int i = 1;i<=m;i++) cin >> a[i];
        for(int i = 1;i<=n;i++) cin >> b[i];
        for(int i = 1;i<=n;i++){
            if(a[b[i]]==0&&a[b[i]+1]==0){
                ans++;
            }else if(a[b[i]+1]!=0){
                a[b[i]+1]--;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

正确思路:

给定t组数据,每组数据定义:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 2个变量n和m,由m个元素组成的序列A,由n个元素~~\\ 组成的序列B~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 定义变量ans~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 循环n次,如果A_{B_i}和A_{B_i+1}都为0,那么ans+1~~~~~~~~~~~~~\\ 如果A_{B_i}=0但A_{B_i+1}\ge1,那么A_{B_i+1}-1~~~~~~~~~~~~~~~~~~~~~\\ 如果A_{B_i+1}=0但A_{B_i}\ge1,那么A_{B_{i}}-1~~~~~~~~~~~~~~~~~~~~~~~~\\ 最后输出ans~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

正确代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 3*(1e4+4);
int a[M];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;cin >> t;
    while(t--){//每组数据
        int ans=0;
        int n,m;
        cin >> n >> m;
        for(int i = 1;i<=m;i++) cin >> a[i];//录入
        for(int i = 1;i<=n;i++){
            int x;cin >> x;//代替b[i]
            if(a[x+1]){//判断并更改元素
                a[x+1]--;
            }else if(a[x]){
                a[x]--;
            }else ans++;
        }
        cout << ans << endl;//输出
    }
    return 0;
}

T4

错误代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+5,M = 5*(1e5+5);
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << 4;
    return 0;
}//实在不会就骗分了

正确思路:

给定n个字符,可以改变其中一个,求出在此情况下最\\ 大的S子序列~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 并作标记统计,再作s1和s2变量作为上一个S序列与~~~\\ 和这一个S序列~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ 且每次比ans和s1+s2+1,最后输出ans~~~~~~~~~~~~~~~~~~~~~

正确代码:

#include<bits/stdc++.h>
using namespace std;
int s1,s2,ans;
bool p,p1;
int main(){
    int n;
    cin >> n;
    for(int i = 1;i<=n;i++){
        char a;
        cin >> a;
        if(a=='S'){
            if(p)s2++;
            else s2 = 1,p = 1;
            ans = max(ans,s1+s2+1);
        }
        else{
            p1 = 1;
            if(p) {
                p = 0;
                s1 = s2;
            }
            else s1 = s2 = 0;
        }
    }
    if(!ans)ans = 1;
    if(!p1)ans = n-1;
    cout << ans;
    return 0;
}

T5

正确思路:

给定3个变量n~k~c和一个由n个元素组成的序列A~~~~~~~\\ 再定义SUM序列,其中SUM_i=SUM_{i-1}+A_i~~~~~~~~~~~\\ 再定义KSUM,其中KSUM_i=SUM_i-SUM_{i-k}~~~~\\ 最后循环c次,每次循环至sum[l]\le i且l\le(n-k)~~~~~\\ 循环中每次循环将ans设为ans和KSUM_{l+k}并k+1~~\\ 输出ans~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

正确代码:

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=5005;
long long n,k,c,a[maxn],l;
long long sum[maxn],ksum[maxn],ans;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> k >> c;
    for(int i = 1;i<=n;i++){
        cin >> a[i];//录入
    }
    sort(a+1,a+n+1);//排序
    for(int i = 1;i<=n;i++){
        sum[i]=sum[i-1]+a[i];//定义sum[i]
    }
    for(int i = k;i<=n;i++){
        ksum[i] = sum[i]-sum[i-k];//定义ksum[i]
    }
    for(int i = 1;i<=c;i++){
        while(sum[l]<=i&&l<=n-k){
            ans = max(ans,ksum[l+k]);//求出ans
            l++; 
        }
        cout << ans << '\n'; //输出,换行时用\n防止TLE
    }
    return 0;
}

T6

题目思路:

给定n和由n组成的序列A,求:最多能组成多少个B\\ 序列,B序列要求:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\ \frac{B的最大值}{2}=B的最小值~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

正确代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,a[100005];
int main(){
    ll ans =0;
    cin >> n;
    for(int i = 1;i<= n;i ++) cin >> a[i];//录入
    sort(a+1,a+1+n);//排序
    for(int i  =n/2,j=n;i>=1;i--){//双指针求答案
        if(a[j]>=a[i]*2){
            ans++;//统计答案
            j--;
        }
    }
    cout << ans;//输出
    return 0;
}

字数:7556字