从大佬那里看的解法 转载一下
第1行:线段的数量N(2 <= N <= 50000)。 第2 - N + 1行:每行2个数,线段的起点和终点。(0 <= s , e <= 10^9)
输出最长重复区间的长度。
5 1 5 2 4 2 8 3 7 7 9
4
从这道题就看出自己到底有多菜 操
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
int beginn;
int endd;
} t[110000];
bool cmp(node a,node b)
{
if(a.beginn!=b.beginn)
return a.beginn<b.beginn;
return a.endd<b.endd;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1; i<=n; i++)
scanf("%d%d",&t[i].beginn,&t[i].endd);
sort(t+1,t+n+1,cmp);
int temp=t[1].endd;
int maxx=0;
for(i=2; i<=n; i++)
{
if(t[i].beginn<temp)
{
if(t[i].endd<=temp)
{
maxx=max(maxx,t[i].endd-t[i].beginn);
}
else if(t[i].endd>temp)
{
maxx=max(maxx,temp-t[i].beginn);
temp=t[i].endd;
}
}
else
temp=t[i].endd;
}
printf("%d\n",maxx);
}
return 0;
}