题目链接
题目详情
You are given n one-dimensional segments (each segment is denoted by two integers — its endpoints).
Let's define the function as the number of segments covering point x (a segment covers the point x if , where l is the left endpoint and r is the right endpoint of the segment).
An integer point x is called ideal if it belongs to more segments than any other integer point, i.e. is true for any other integer point y.
You are given an integer k. Your task is to determine whether it is possible to remove some (possibly zero) segments, so that the given point k becomes ideal.
Input
The first line contains one integer — the number of test cases.
The first line of each test case contains two integers and .
Then nn lines follow, i-th line of them contains two integers and — the endpoints of the i-th segment.
Output
For each test case, print YES if it is possible to remove some (possibly zero) segments, so that the given point kk becomes ideal, otherwise print NO.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
input
4
4 3
1 3
7 9
2 5
3 6
2 9
1 4
3 7
1 3
2 4
3 5
1 4
6 7
5 5
output
YES
NO
NO
YES
题目大意及解题思路
大意:
给定n个一维线段(每个线段由两个整数表示-其端点)。让我们定义函数f(x)表示x被线段覆盖, 一个整数点 x 被称为理想的,如果它比任何其他整数点属于更多的段,即f(y)<f(x)对于任何其他整定点y都是真的。你得到了一个整数k。确定是否有可能删除一些(可能为零)线段,使给定点k成为理想的。输出对于每个测试用例,如果可以删除一些(可能为零)段,则打印YES,从而使给定点kk变得理想,否则打印NO。
解题思路:
首先我们排除不覆盖的线段,然后找到覆盖k的最小区间,如果这个区间的左端点L和右端点R相等,那么就符合条件(这时L=R=k)。
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, k, L = -1, R = 0x3f3f3f3f;
cin >> n >> k;
while (n--)
{
int l, r;
cin >> l >> r;
if (l <= k && r >= k)
{
L = max(L, l);
R = min(R, r);
}
}
if (L == R)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}