洛谷题解 | P9690 [GDCPC2023] Programming Contest

题面翻译

【题目描述】

广东省是全国较早一批将程序设计竞赛引入省内大学生竞赛体系的省份之一。 2003 2003 2003 年,中山大学承办了第一届广东省大学生程序设计竞赛。此后,华南农业大学、华南理工大学、华南师范大学等省内高校也先后承办了此赛事,除 2020 2020 2020 年因疫情停办外,每年一届。 2023 2023 2023 年,深圳技术大学将承办第二十届广东省大学生程序设计竞赛,让我们期待选手们出色的表现!

在另一个世界中,某程序设计竞赛自 y 1 y_1 y1 年起开始举办。除了 s 1 , s 2 , ⋯   , s n s_1, s_2, \cdots, s_n s1,s2,,sn n n n 年由于特殊原因无法举办之外,其他年份每年举办一次。

y 2 y_2 y2 年是该竞赛的第几次举办。

【输入格式】

有多组测试数据。第一行输入一个整数 T T T 1 ≤ T ≤ 20 1 \le T \le 20 1T20)表示测试数据组数。对于每组测试数据:

第一行输入一个整数 y 1 y_1 y1 1970 ≤ y 1 ≤ 9999 1970 \le y_1 \le 9999 1970y19999),表示该竞赛第一次举办的年份。

第二行首先输入一个整数 n n n 0 ≤ n ≤ 100 0 \le n \le 100 0n100)表示该竞赛停办的年份数,之后输入 n n n 个整数 s 1 , s 2 , ⋯   , s n s_1, s_2, \cdots, s_n s1,s2,,sn y 1 < s i ≤ 9999 y_1 < s_i \le 9999 y1<si9999)表示该竞赛的停办年份。停办年份按递增顺序给出,且没有重复的年份。

第三行输入一个整数 y 2 y_2 y2 y 1 ≤ y 2 ≤ 9999 y_1 \le y_2 \le 9999 y1y29999)。保证 y 2 y_2 y2 不是停办年份之一。

【输出格式】

每组数据输出一行一个整数,表示 y 2 y_2 y2 年是该竞赛的第几次举办。

【样例解释】

对于第一组样例数据,如题目描述中所述,答案为 20 20 20

对于第二组样例数据,由于 2003 2003 2003 就是该竞赛第 1 1 1 次举办的年份,因此答案为 1 1 1

对于第三组样例数据,由于竞赛从未停办,因此答案为 3456 − 2345 + 1 = 1112 3456 - 2345 + 1 = 1112 34562345+1=1112

对于第四组样例数据,该竞赛前 5 5 5 次举办的年份为 3000 3000 3000 3002 3002 3002 3005 3005 3005 3006 3006 3006 3007 3007 3007。因此答案为 5 5 5

题目描述

Guangdong Province is one of the earliest province in China which holds its own provincial collegiate programming contest. Sun Yat-sen University hosted the first Guangdong Collegiate Programming Contest in year 2003 2003 2003. After that, other universities in Guangdong, such as South China Agricultural University, South China University of Technology and South China Normal University, also hosted the contest. The contest is held once a year except for year 2020 2020 2020 due to the epidemic. In year 2023 2023 2023, Shenzhen Technology University will host the twentieth Guangdong Collegiate Programming Contest. We are looking forward to seeing participants’ outstanding performance!

In another world, a programming contest has been held once a year since year y 1 y_1 y1, except for the n n n years s 1 , s 2 , ⋯   , s n s_1, s_2, \cdots, s_n s1,s2,,sn when it was not held due to special reasons.

Calculate the number of times the competition has been held up to year y 2 y_2 y2 (inclusive).

输入格式

There are multiple test cases. The first line of the input contains an integer T T T ( 1 ≤ T ≤ 20 1 \le T \le 20 1T20) indicating the number of test cases. For each test case:

The first line contains an integer y 1 y_1 y1 ( 1970 ≤ y 1 ≤ 9999 1970 \le y_1 \le 9999 1970y19999) indicating the first year when the contest was held.

The second line first contains an integer n n n ( 0 ≤ n ≤ 100 0 \le n \le 100 0n100) indicating the number of years the contest was not held. Then n n n integers s 1 , s 2 , ⋯   , s n s_1, s_2, \cdots, s_n s1,s2,,sn ( y 1 < s i ≤ 9999 y_1 < s_i \le 9999 y1<si9999) follow, indicating the years when the contest was not held. These years are given in increasing order and have no duplicates.

The third line contains an integer y 2 y_2 y2 ( y 1 ≤ y 2 ≤ 9999 y_1 \le y_2 \le 9999 y1y29999). It’s guaranteed that y 2 y_2 y2 is not a year when the contest was not held.

输出格式

For each test case output one line containing one integer, indicating the number of times the competition has been held up to year y 2 y_2 y2 (inclusive).

样例 #1

样例输入 #1

4
2003
1 2020
2023
2003
1 2020
2003
2345
0
3456
3000
4 3001 3003 3004 3008
3007

样例输出 #1

20
1
1112
5

提示

For the first sample test case, as described in the problem description, the answer is 20 20 20.

For the second sample test case, because year 2003 2003 2003 is the 1 1 1-st year when the contest was held, the answer is 1 1 1.

For the third sample test case, because the contest was held every year, the answer is 3456 − 2345 + 1 = 1112 3456 - 2345 + 1 = 1112 34562345+1=1112.

For the fourth sample test case, the first 5 5 5 years when the contest was held is 3000 3000 3000, 3002 3002 3002, 3005 3005 3005, 3006 3006 3006 and 3007 3007 3007. So the answer is 5 5 5.

题目简化

计算在给定的区间内,未出现的数字的个数。

题目思路

首先,定义变量 Ty_1y_2n

然后,定义了一个布尔类型的数组 a。这个数组用来记录在区间内出现的数字。初始时,数组的所有元素都被设置为 false

接下来是一个循环,从 1 1 1T,用来处理每个测试用例。

在每个测试用例中,首先将数组 a 的所有元素重置为false,表示区间内的数字都未出现。然后,依次输入。

接下来,再次用一个循环从 1 1 1 n n n,读入 n n n 个数字,并将数组 a 对应位置的元素设置为 true,表示该数字在区间内出现过。

最后,继续读入,并定义变量 ans 0 0 0,用来记录未出现数字的个数。

再来一个循环,从 y_1y_2,遍历区间的每个年份。如果数组 a 对应位置的素为 false,表示该年份未出现在区间内的数字中,于是 ans + + + 1 1 1

最后,输出 ans

AC代码

#include<bits/stdc++.h>
using namespace std;
int T,y_1,y_2,n;
bool a[100001];
int main() {
    cin >> T;
    for (int i = 1; i <= T; i++) {
    	memset(a,0,sizeof(a));
        cin >> y_1 >> n;	
		for (int i = 1; i <= n; i++) {
	    	long long s;
        	cin >> s;
	    	a[s] = true;
		}
		cin >> y_2;
		int ans = 0;
		for (int i = y_1; i <= y_2; i++) {
			if (!a[i]) ans++;
		}
		cout << ans << endl;
    }
    return 0;
}

创作不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,如果喜欢我的文章,给个关注吧!

冰焰狼 | 文

如果本篇博客有任何错误,请批评指教,不胜感激 !

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值