阅读程序(第三题):最多不重叠区间选择
阅读下面程序,完成后面的判断题和单选题。判断题正确填 A,错误填 B。
#include <algorithm>
#include <cstdio>
struct Seg {
int l, r;
};
Seg a[100005];
bool cmp(const Seg &x, const Seg &y) {
return x.r < y.r;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", &a[i].l, &a[i].r);
std::sort(a + 1, a + n + 1, cmp);
int cnt = 0, last = -1000000000;
for (int i = 1; i <= n; ++i) {
if (a[i].l >= last) {
++cnt;
last = a[i].r;
}
}
printf("%d\n", cnt);
return 0;
} 子题 判断题
判断题:该程序求解的是“从给定的若干时间段中选出最多若干个两两不重叠的时间段”(此处允许一个时间段的结束时刻等于另一个时间段的开始时刻)。( )
子题 判断题
判断题:将第 11 行的 `x.r < y.r` 改为 `x.r > y.r` 后,程序的输出结果一定不变。( )
子题 判断题
判断题:若 n ≥ 1,且所有时间段的左端点 l 均不小于 0,则程序输出的 cnt 一定大于等于 1。( )
子题 判断题
判断题:将第 22 行的 `a[i].l >= last` 改为 `a[i].l > last` 后,程序输出的结果可能变小。( )
子题 单选题
当输入为 "3 / 1 10 / 2 3 / 4 5"(第一行为 n = 3,随后 3 行每行一个时间段)时,输出为( )。
子题 单选题
当输入为 "4 / 1 2 / 2 3 / 3 4 / 1 4" 时,输出为( )。
子题 单选题
该程序的时间复杂度为( )。
正在加载题目数据…
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析