描述
题解
先进行排序,然后互相比较,留最长。然而单纯地互相比较会在最后几组数据超时,所以需要进行一个小小的优化,L[j].y > L[i].y
时,直接跳出i
与后续的j
的比较。如此,AC之……
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 5e4 + 5;
struct line
{
int x, y;
} L[MAXN];
bool cmp(line a, line b)
{
return a.x < b.x;
}
int main(int argc, const char * argv[])
{
freopen("input.txt", "r", stdin);
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
scanf("%d %d", &L[i].x, &L[i].y);
}
sort(L, L + N, cmp);
int len = 0;
for (int i = 0; i < N - 1; i++)
{
for (int j = i + 1; j < N; j++)
{
int A = L[i].x > L[j].x ? L[i].x : L[j].x;
int B = L[i].y > L[j].y ? L[j].y : L[i].y;
len = len > B - A ? len : B - A;
if (L[j].y > L[i].y)
{
break;
}
}
}
cout << len << '\n';
return 0;
}