52 lines
1.5 KiB
C++
52 lines
1.5 KiB
C++
#include <iostream>
|
||
#include <vector>
|
||
#include <algorithm> // 必须引入排序
|
||
|
||
using namespace std;
|
||
|
||
// 定义排序规则:按结束时间从小到大排
|
||
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
|
||
return a.second < b.second;
|
||
}
|
||
|
||
int main(){
|
||
int luogugrass = 0; // 定义必要变量
|
||
int n, m;
|
||
cin >> n >> m;
|
||
vector<vector<pair<int, int>>> arr(n);
|
||
for(int i = 0; i < n; i++){
|
||
int num;
|
||
cin >> num;
|
||
arr[i].resize(num);
|
||
for(int j = 0; j < num; j++){
|
||
cin >> arr[i][j].first >> arr[i][j].second;
|
||
}
|
||
// --- 核心改动 1:对每一层课程按结束时间排序 ---
|
||
sort(arr[i].begin(), arr[i].end(), cmp);
|
||
}
|
||
|
||
int end = 0; // 修改:初始结束时间应为 0
|
||
bool isfind = true;
|
||
for(int i = 0; i < n; i++){
|
||
bool flag = false;
|
||
// 由于已经排序,第一个满足 first > end 的就是最早结束的班级
|
||
for(int j = 0; j < arr[i].size(); j++){
|
||
if(arr[i][j].first > end) {
|
||
end = arr[i][j].second;
|
||
flag = true;
|
||
break;
|
||
}
|
||
}
|
||
if(flag == false) {
|
||
isfind = false;
|
||
break; // --- 核心改动 2:一旦某门课修不了,直接跳出 ---
|
||
}
|
||
}
|
||
|
||
// --- 核心改动 3:最后判断是否超过了总天数 m ---
|
||
if(isfind && end <= m) cout << end << endl;
|
||
else cout << -1 << endl;
|
||
|
||
return 0;
|
||
}
|