2021牛客多校6 I Intervals on the Ring

Posted 布图

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021牛客多校6 I Intervals on the Ring相关的知识,希望对你有一定的参考价值。

题目

链接:https://ac.nowcoder.com/acm/contest/11257/I
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

There is a ring of numbers consisting of 111 to nnn sequentially. For every number iii (1≤i≤n−1)(1 \\leq i \\leq n - 1)(1≤i≤n−1), iii and i+1i+1i+1 are adjacent to each other. Particularly, nnn and 111 are adjacent. We use [l,r][l,r][l,r] to describe an interval on the ring. Formally, if l≤rl \\leq rl≤r, then the interval [l,r][l,r][l,r] is equivalent to the set {l,l+1,…,r−1,r}{ l, l + 1, \\ldots, r - 1, r }{l,l+1,…,r−1,r}. Otherwise, the interval [l,r][l,r][l,r] is equivalent to {l,l+1,…,n,1,…,r−1,r}{l,l+1,\\ldots,n,1,\\ldots,r-1,r}{l,l+1,…,n,1,…,r−1,r}.

Yukikaze has mmm non-intersecting intervals. She wants you to construct a set of intervals such that the intersection of them is the union of the mmm 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≤T≤1000)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≤n≤1000)n\\ (3 \\leq n \\leq 1000)n (3≤n≤1000) and m (1≤m≤n)m\\ (1 \\leq m \\leq n)m (1≤m≤n), denoting that the ring consists of numbers from 111 to nnn.

Each of the next mmm lines contains two integers l,r (1≤l,r≤n)l,r\\ (1 \\leq l, r \\leq n)l,r (1≤l,r≤n), denoting an interval Yukikaze has. It's guaranteed that the mmm intervals won't intersect with each other.

输出描述:

For each test case, if the answer doesn't exist, output −1-1−1 in a line. Otherwise, output an integer kkk indicating the number of intervals you construct in a line. Then output the kkk intervals in kkk lines. The number of intervals you used should never be less than one or greater than 200020002000. 



If there are multiple solutions, print any. Don't print any extra spaces at the end of each line.

示例1

输入

[复制](javascript:void(0)😉

2
3 1
2 2
4 2
1 1
3 3

输出

[复制](javascript:void(0)😉

1
2 2
2
3 1
1 3

解释与代码

给你m个区间,要求输出k给区间,使得k个区间的交集和m个区间的并集相同

比赛时候写的差不多了,不知道什么情况就是过不了(现在还是不知道)

ac

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1009;
const ll N = 1000000007;
const ull N2 = 1000000007;

typedef struct LNode{
    int coef,index;
    struct LNode *next;
}LNode,*LinkList;


struct node {
	int l, r;
}arr[maxn];

bool cmp(node a,node b){
	return a.l < b.l;
}




void solve(){
	ini(arr);
	int n, m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>arr[i].l>>arr[i].r;
	}
	sort(arr+1, arr+1+m, cmp);
	cout<<m<<endl;
	cout<<arr[1].l<<" "<<arr[m].r<<endl;
	for (int i=2; i<=m; i++) {
		cout<<arr[i].l<<" "<<arr[i-1].r<<endl;
	}
}

int main()
{
//	TIE;
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
//	solve();
    sc{solve();}
//    sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}

wa

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1009;
const ll N = 1000000007;
const ull N2 = 1000000007;

typedef struct LNode{
    int coef,index;
    struct LNode *next;
}LNode,*LinkList;

bool cmp(int a,int b){
	return a>b;
}

int cb[maxn];
int mx[maxn][2];

void solve(){
	ini(cb);
	ini(mx);
	int n,m,l,r;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>l>>r;
		int p = 1;
		if(l<=r){
			for(int i=l;i<=r;i++){
				cb[i] = 1;
			}
		}else{
			for(int i=r;;i++){
				if(p==0&&i>l){
					break;
				}
				if(i==n+1){
					i=1;
					p=0;
				}
				cb[i] = 1;
			}
		}
	}
	int cnt = 0;
	for(int i=1;i<=n;i++){
		if(cb[i]==1){
			if(cb[i-1]==0){
				mx[cnt][0] = i;
			}
			if(cb[i+1]==0){
				mx[cnt++][1] = i;
			}
		}
		if (i==n && cb[n]==0) {
			
		}
	}
	cout<<cnt<<endl;
	cout<<mx[0][0]<<" "<<mx[cnt-1][以上是关于2021牛客多校6 I Intervals on the Ring的主要内容,如果未能解决你的问题,请参考以下文章

2021牛客多校1 - Hash Function(思维+FFT)

2021牛客多校8 D.OR(位运算)

2021牛客多校5 - Double Strings(dp+组合数学)

2021牛客多校6 - Hopping Rabbit(矩形取模+扫描线)

2021牛客多校10 - Browser Games(哈希)

2021牛客多校3 - Black and white(思维+最小生成树)