课程冲突
小 A 修了 n 门课程, 第 i 门课程是从第 ai 天一直上到第 bi 天。
定义两门课程的冲突程度为 : 有几天是这两门课程都要上的。
例如 a1=1,b1=3,a2=2,b2=4 时, 这两门课的冲突程度为 2。
现在你需要求的是这 n 门课中冲突程度最大的两门课的冲突程度。
时间限制:1000
内存限制:65536
输入
第一行一个正整数 n 表示课程数量。 接下来 n 行,每行两个正整数 ai,bi。 2 ≤ n≤ 1000, 1 ≤ ai ≤ bi ≤ 1000。
输出
输出一个整数表示最大的冲突程度
样例输入
3
1 3
2 4
5 5
样例输出
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;
}
#include <iostream>
#include <vector>
int calculate_conflicted_days(const std::pair<int, int> &p1, const std::pair<int, int> &p2)
{
if (p1.second < p2.first || p2.second < p1.first)
return 0;
else
return std::min(p1.second, p2.second) - std::max(p1.first, p2.first) + 1;
}
int main()
{
int n;
std::vector<std::pair<int, int>> abs;
std::cin >> n;
for (int i = 0; i < n; i++)
{
int a, b;
std::cin >> a >> b;
abs.push_back(std::make_pair(a, b));
}
int max = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
int days = calculate_conflicted_days(abs[i], abs[j]);
if (days > max)
max = days;
}
}
std::cout << max << '\n';
return 0;
}
解答:
#include<bits/stdc++.h>
using namespace std;
bool c;
int n,a,b,count;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b;
for(int j=a;j<=b;j++){
if(c[j])count++; //表示重复了
else c[j]=true; //没重复就存进布尔数组
}
}
cout<<count;
return 0;
}