答案:<pre class="brush:cpp;toolbar:false">//参考答案1
#include <bits/stdc++.h>
using namespace std;
//#define PPYDEBUG
struct lesson{
int no; //课程编号
int a; //课程开始a天
int b; //课程结束b天
};
int max_chongtu(lesson x, lesson y){
int r = min(x.b, y.b) - max(x.a, y.a) + 1;
if(r < 0){
r = 0;
}
#ifdef PPYDEBUG
printf("%d = x[%d,%d] - y[%d,%d]\n", r, x.a, x.b, y.a, y.b);
#endif
return r;
}
int main() {
#ifdef LOCAL
freopen("202209_3_1.in", "r", stdin);
#endif
int n;
scanf("%d", &n);
lesson le[1010];
for(int i = 0; i < n; i++){
le[i].no = i + 1;
scanf("%d%d", &le[i].a, &le[i].b);
}
int r = 0;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
r = max(r, max_chongtu(le[i], le[j]));
}
}
printf("%d", r);
return 0;
}</pre><pre class="brush:cpp;toolbar:false">//参考答案2
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct project {
int start;
int end;
project(int a,int b):start(a),end(b){}
bool operator <(const project A)const {
if (start == A.start)
return end < A.end;
else
return start < A.start;
}
};
int main() {
int n;
cin >> n;
vector<project>alls;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
alls.push_back(project(a, b));
}
sort(alls.begin(), alls.end());
int result = 0;
for (int i = 0; i < n; i++) {
if (alls[i].end - alls[i].start < result)//剪枝
continue;
for (int j = i + 1; j < n; j++) {
if (alls[j].start > alls[i].end)
break;
int t = min(alls[i].end, alls[j].end) - alls[j].start + 1;
result = max(t, result);
}
}
cout << result << endl;
return 0;
}</pre><p><br/></p>
这道题暂未补解析。