日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Intervals on the Ring

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Intervals on the Ring 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Intervals on the Ring

題意:

給出環上的一組區間,需要構造環上的一組區間使得這些區間的交是給定的區間的并

題解:

集合的摩爾定理告訴我們:∪Ai ̄=∩Ai ̄\overline{\cup A_{i}}=\cap\overline{A_{i}}Ai??=Ai??(區間的并的補等于區間的補的交),所以直接輸出每一段未被給出區間覆蓋的區間的補即可
例如:n=6,A1=[1,1],A2=[3,3],A3=[5,5],則輸出[3,1],[5,3],[1,5]

代碼:

#include<bits/stdc++.h> using namespace std; const int maxn=1e3+10; int sum[maxn*2]; int a[maxn*2]; int t,n,m; struct xy {int l,r; }e[maxn]; bool cmp(xy x,xy y) {return x.l<y.l; } int main() {cin>>t;while(t--){cin>>n>>m;memset(sum,0,sizeof(sum));memset(e,0,sizeof(e));memset(a,0,sizeof(a));for(int i=1;i<=m;i++){int a,b;cin>>a>>b;if(b<a){sum[a]++,sum[b+n+1]--;//sum[1]++,sum[b+1]--;}else {sum[a]++,sum[b+1]--;}}for(int i=1;i<=n;i++){a[i]=a[i-1]+sum[i];}for(int i=n+1;i<=2*n;i++){a[i]=a[i-1]+sum[i];if(a[i]){a[i-n]++;}}//cout<<1<<" "<<n<<endl;int tot=0;int flag=0;int x[maxn];int cnt=0;int l,r;a[n+1]=0;for(int i=1;i<=n+1;i++){//cout<<a[i]<<endl;if(flag==0){if(a[i]){flag=1;e[++tot].l=i;//x[++cnt]=i;}}else if(flag==1){if(a[i]==0){flag=0;e[tot].r=i-1;x[++cnt]=i-1;}}//cout<<i<<" "<<tot<<endl;} //cout<<a[n]<<endl;//cout<<tot<<endl;//if(a[n])e[tot].r=n;sort(e+1,e+tot+1,cmp);if(a[1]&&a[n]){if(tot==1){cout<<1<<endl;cout<<1<<" "<<n<<endl;continue;}cout<<tot-1<<endl;for(int i=2;i<=tot;i++)cout<<e[i].l<<" "<<e[i-1].r<<endl; continue;}else {cout<<tot<<endl;if(tot==1){cout<<e[1].l<<" "<<e[1].r<<endl;}else {cout<<e[1].l<<" "<<e[tot].r<<endl;for(int i=2;i<=tot;i++)cout<<e[i].l<<" "<<e[i-1].r<<endl; }continue;}}return 0; } /* 10 7 3 7 2 6 3 5 2 */ 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Intervals on the Ring的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。