B4405 [语言月赛 202509] 你这题至少 *3000 题解

· · 题解

本题考察数组、循环结构。

我们使用数组来解决这个问题。设置 \texttt{bool} 类型数组 b,其中 b_i 表示是否有题适合位置 i

对于输入的每个数 x,我们令 b_xb_{x+1} 都为 1 即可。虽然 x=m 时会超出去,但是不影响我们的算法正确性,因为我们之后不会统计到这道不存在的题目。

之后我们扫描一遍数组 b1m 的位置,统计一下有多少位置 b_i=0,并输出这些位置即可。

特别地,如果没有任何位置 b_i=0,输出 Accepted

const int N=1e5+10;
int n,m;
bool bol[N];
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    while(n--){
        int x;
        cin>>x;
        bol[x]=bol[x+1]=1;
    }
    int c=0;
    for(int i=1;i<=m;i++) if(!bol[i]) c++;
    if(!c) cout<<"Accepted\n";
    else{
        cout<<c<<'\n';
        for(int i=1;i<=m;i++) if(!bol[i]) cout<<i<<' ';
    }
}