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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1408F Two Different(构造+分治)

發(fā)布時(shí)間:2024/4/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1408F Two Different(构造+分治) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:點(diǎn)擊查看

題目大意:初始時(shí)給出一個(gè)長(zhǎng)度為 n 的序列,每個(gè)位置 a[ i ] = i ,再給出一個(gè)映射 f( x , y ) = z,現(xiàn)在規(guī)定每次操作可以使得:

  • t = f( a[ i ] , a[ j ] )
  • a[ i ] = t
  • a[ j ] = t
  • 對(duì)于完全相同的 x 和 y,得到的 f( x , y ) 是相同的

    現(xiàn)在需要給出一種操作順序,使得無論映射 f 如何定義,最后都能使得整個(gè)序列最多有兩種不同的元素

    題目分析:自己手玩一下不難發(fā)現(xiàn),如果 n 是 2 的冪次的話,總是有辦法可以將其變?yōu)橥环N元素的,以 n = 8 為例:

  • 1 2 3 4 5 6 7 8
  • 9 9 10 10 11 11 12 12
  • 13 13 13 13 14 14 14 14
  • 15 15 15 15 15 15 15 15
  • 這樣可以類比于 st 表的思想,找出最大的小于等于 n 的 2 的冪次記為 k,對(duì)于區(qū)間 ( 1 , 1 + k - 1 ) 和 ( n - k + 1 , n ) 進(jìn)行上述操作,就可以保證所有的 n 個(gè)元素都能被覆蓋到,且最后最多有兩種不同的元素了

    因?yàn)閷?duì)應(yīng)的過程比較麻煩,寫分治的話相對(duì)比較簡(jiǎn)單一些,時(shí)間復(fù)雜度是 nlogn 級(jí)別的,換句話說最后的答案也是 nlogn 級(jí)別的

    代碼:

    //#pragma GCC optimize(2) //#pragma GCC optimize("Ofast","inline","-ffast-math") //#pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;vector<pair<int,int>>ans;void dfs(int l,int r) {if(l==r)return;int mid=l+r>>1;dfs(l,mid);dfs(mid+1,r);for(int i=l,j=mid+1;i<=mid;i++,j++)ans.emplace_back(i,j); }int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;scanf("%d",&n);int p=1;while(p*2<=n)p*=2;dfs(1,1+p-1);if(n!=p)dfs(n-p+1,n);printf("%d\n",ans.size());for(auto t:ans)printf("%d %d\n",t.first,t.second);return 0; }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的CodeForces - 1408F Two Different(构造+分治)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。