P9690 [GDCPC2023] Programming Contest 题解

· · 题解

\Large\color{black}\textbf{P9690 [GDCPC2023] Programming Contest} \Large\textbf{题解}

\textbf{题目传送门}

\textbf{更好的阅读体验}

upd on 2023/10/5 9:54 将思路中提到的 ans-- 改为 LATEX 公式。

好水啊

\large\textbf{思路}

先计算出如果没有停办的答案,即 ans=y_2-y_1+1,再依次遍历停办的年份 a_i,如果 a_i<y_2,即在 y_2 之前停办,则 ans\leftarrow ans-1,最后输出答案即可。

\large\textbf{代码}
#include <bits/stdc++.h>
using namespace std;
#define int long long // DILL 是个好习惯,不过容易爆空间
const int N = 1e2 + 5; // 定义常量是个好习惯
int t, y1, y2, n, a[N];
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>t;
    while(t--) {
        cin>>y1>>n;
        for(int i = 1; i <= n; i++) cin>>a[i];
        cin>>y2;
        int ans = y2 - y1 + 1; // 计算初始答案
        for(int i = 1; i <= n; i++) 
            if(a[i] < y2) ans--; // 遍历判断是否在 y2 前停办
        cout<<ans<<endl;
    }
    return 0;
}