题目描述
某个杂志的主编想要找出最多的卓越科学家在世的年代。现在他的手上有这些科学家的出生与去世的年份(byear,eyear);如果某两个科学家的年份有交叉(10年以上,即一个的eyear-另一个的byear>=10),认为两人是“同在”,科学家同在最多的时代,称为“科学盛世”(一个科学家,只要跟另一个同在即可,而不是跟所有的都同在),请找出这个盛世的开始年和结束年。
输入要求
第一行输入一个整型n<1000,表示有多少个科学家
后面n行,每行2个整型,分别表示每个科学家的byear和eyear
输出要求
输出2个整型,表示“科学盛世”的开始年和结束年;如果有多于1个盛世(人数相同),输出最前面一个;
输入样例
4
1234 1264
1260 1370
1900 1949
1919 2020
输出样例
1900 2020
来源
NBU OJ
#include "algorithm"
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#define debug
int main() {
#ifdef debug
freopen("in.txt", "r", stdin);
#endif
int n;
std::cin >> n;
std::pair<int , int > scientist[1010];
for (int i = 0; i < n; i++)
{
std::cin >> scientist[i].first >> scientist[i].second;
}
std::sort(scientist, scientist + n);
#ifdef debug
for (int i = 0; i < n; i++)
{
std::cout << scientist[i].first << " " << scientist[i].second << std::endl;
}
#endif
int start=scientist[0].first, end=scientist[0].second;
int maxStart = start, maxEnd = end;
int numSci = 1, maxSci = 1;
for (int i = 1; i < n; i++)
{
//^废弃^前科学家去世日期比后一位晚十年 即黄金时代延续
// if(scientist[i-1].second-scientist[i].first>=10){
//科学家出生日期比当前早十年 即黄金时代延续
if (end - scientist[i].first >= 10) {
numSci++; //当前时代科学家数
end = scientist[i].second > end
? scientist[i].second : end; //当前时代末尾延长
//若当前时代科学家数大于最高 替换掉盛世
if(numSci>maxSci){
maxSci = numSci;
maxStart = start;
maxEnd = end;
}
} else { //黄金时代断开
start = scientist[i].first;
end = scientist[i].second;
numSci = 1;
}
}
std::cout << maxStart << " " << maxEnd << std::endl;
}