题目描述
在一条数轴上,有 N 条线段,第 i 条线段的左端点是 si,右端点是 ei。如果线段有重叠(即使是端点重叠也算是重叠),则输出 “impossible”, 如果没有重叠则输出 “possible” 。
输入格式
多组测试数据。
第一行,一个整数 G ,表示有 G 组测试数据。1 <= G <= 10 。每组测试数据格式如下:
第一行,一个整数 N。 1 <= N <= 10。
接下来有 N 行,每行两个整数:si,ei ( 0 <= si,ei <= 1000000 )。
输出格式
共 G 行,每行一个字符串,不含双引号。
样例
输入数据 1
5
3
10 47
100 235
236 347
3
100 235
236 347
10 47
2
10 20
20 30
3
10 20
400000 600000
500000 700000
4
1 1000000
40 41
50 51
60 61
Copy
输出数据 1
possible
possible
impossible
impossible
impossible
#include<bits/stdc++.h>
using namespace std;
struct stu{
int x,y;
}a[100];
bool cmp(stu a,stu b){
return a.y<b.y;
}
int k,n;
int main(){
cin>>k;
while(k--){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y;
sort(a+1,a+n+1,cmp);
int l=1;
bool f=true;
for(int i=2;i<=n;i++)
{
if(a[l].y<a[i].x)l=i;
else{
f=false;
break;
}
}
if(f)cout<<"possible\n";
else cout<<"impossible\n";
}
return 0;
}