7 条题解
-
0
参考VAC,给出rust实现。
use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); let mut lines = stdin.lock().lines().map(|line| line.unwrap()); let m: i32 = lines.next().unwrap().parse().unwrap(); for _ in 0..m { let n: usize = lines.next().unwrap().parse().unwrap(); let mut activities = Vec::new(); // 读取每个活动的起始时间和结束时间 for _ in 0..n { let line = lines.next().unwrap(); let split: Vec<i32> = line .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); activities.push(split); } // 按照结束时间升序排序 activities.sort_by(|a, b| a[1].cmp(&b[1])); let mut act_num = 1; // 已安排的活动数量 let mut pointer = 0; // 指向上一个活动的索引 // 从第二个活动开始,检查是否与前一个活动时间冲突 for i in 1..n { if activities[i][0] > activities[pointer][1] { // 没有时间冲突,可以安排该活动 act_num += 1; pointer = i; } } println!("{}", act_num); // 输出结果 } }
信息
- ID
- 124
- 时间
- 3000ms
- 内存
- 128MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 1626
- 已通过
- 205
- 上传者