2021牛客暑假训练营6---Intervals on the Ring(思维)


任意门
链接: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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值