任意门
链接:https://ac.nowcoder.com/acm/contest/11257/I
来源:牛客网
题目描述
There is a ring of numbers consisting of 11 to nn sequentially. For every number ii (1 \leq i \leq n - 1)(1≤i≤n−1), ii and i+1i+1 are adjacent to each other. Particularly, nn and 11 are adjacent. We use [l,r][l,r] to describe an interval on the ring. Formally, if l \leq rl≤r, then the interval [l,r][l,r] is equivalent to the set { l, l + 1, \ldots, r - 1, r }{l,l+1,…,r−1,r}. Otherwise, the interval [l,r][l,r] is equivalent to {l,l+1,\ldots,n,1,\ldots,r-1,r}{l,l+1,…,n,1,…,r−1,r}.
Yukikaze has mm non-intersecting intervals. She wants you to construct a set of intervals such that the intersection of them is the union of the mm intervals that Yukikaze has. The intersection of the intervals is the set of integers that the intervals have in common.
输入描述:
The first line of the input contains a single integer T\ (1 \leq T \leq 1000)T (1≤T≤1000), denoting the number of test cases.
The first line of each test case contains two integers n\ (3 \leq n \leq 1000)n (3≤n≤1000) and m\ (1 \leq m \leq n)m (1≤m≤n), denoting that the ring consists of numbers from 11 to nn.
Each of the next mm lines contains two integers l,r\ (1 \leq l, r \leq n)l,r (1≤l,r≤n), denoting an interval Yukikaze has. It’s guaranteed that the mm intervals won’t intersect with each other.
输出描述:
For each test case, if the answer doesn’t exist, output -1−1 in a line. Otherwise, output an integer kk indicating the number of intervals you construct in a line. Then output the kk intervals in kk lines. The number of intervals you used should never be less than one or greater than 20002000.
If there are multiple solutions, print any. Don’t print any extra spaces at the end of each line.
示例1
输入
2
3 1
2 2
4 2
1 1
3 3
输出
1
2 2
2
3 1
1 3
这一题最难受的就是翻译上面了,太多区间分不清他说的哪个区间是哪一个了,so来翻译一下把!
题目大意
有一串从1~n连续的数构成一个环,[l,r]所给的l,r无论大小呢都是顺时针来计算,现在我有m个不相交的区间,让你构造一些区间是我所有拥有的区间的并集,然后你所构造的这些区间的交集是你所拥有的区间的并集且为整数
思路
其实根据样例你看不出啥来的,就只好自己来模,然后你会发现无论怎么样都需要一个最小的到最大给包围起来,然后再找规律即可
代码
#include <bits/stdc++.h>
using namespace std;
const int N =1e4;
const int mod=998244353;
#define ll long long
struct node{
int l;int r;
}a[N];
bool cmp(node x,node y){
return x.l <y.l ;
}
int main() {
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
int l,r;
for(int i=0;i<m;i++){
cin>>a[i].l >>a[i].r ;
}
sort(a,a+m,cmp);
cout<<m<<endl;
cout<<a[0].l<<' '<<a[m-1].r<<endl;
for(int i=1;i<m;i++){
cout<<a[i].l<<" "<<a[i-1].r<<endl;
}
}
return 0;
}