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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

牛客多校2 - Greater and Greater(bitset优化暴力)

發布時間:2024/4/11 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客多校2 - Greater and Greater(bitset优化暴力) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 n 的數組 a ,再給出一個長度最大為 m 的數組 b,現在問在 a 中所有長度為 m 的子數組中,有多少個子數組滿足每個相應的元素都大于數組 b

題目分析:很顯然的一個暴力時間復雜度是 n * m 級別的,高達 6e9 ,但是除以 32 后,時間復雜度好像就在可行范圍內了,所以需要利用 bitset 優化一下

參考博客:https://www.cnblogs.com/whitelily/p/13311327.html

首先對于數組 b 的每個元素維護一個長度為 n 的bitset,假設 bit[ i ] 是 b[ i ] 所維護的的 bitset,bit[ i ][ j ]?= 1 當且僅當 a[ j ] 大于等于 b[ i ]

不難看出,如果長度為 m 的 a 的一個子數組滿足條件,需要滿足 a[ i ] >= b[ j ] && a[ i + 1 ] >= b[ j + 1 ] && ... && a[ i + m - 1 ] >= b[ j + m - 1] ,換句話說需要 bit[ i ][ j?] = bit[ i + 1 ][ j + 1 ] = ... =? bit[ i + m - 1 ][ j + m - 1 ] = 1 同時成立,如果將 bit[ i ][ j ] 放在一個二維平面中去,顯然上述條件涉及到的 m 個 bit 元素位于一條斜率為 1 的斜線上,換句話說,上述條件也等同于 bit[ i ][ j ] & bit[ i + 1 ][ j + 1 ] >> 1 & ... & bit[ i + m - 1 ][ j + m - 1 ] >> ( m - 1 ) 成立,這樣在求出 bit 數組后,?m 個 bit 數組進行 與 運算后?1 的個數就是答案了

但構造 bit 數組樸素的話需要 n * m 的時間復雜度,顯然是不可以的,不難發現,如果 b[ i ] > b[ j ] ,當 bit[ j ] 已經轉移完畢時,bit[ i ] 僅需要在 bit[ j ] 的基礎上再轉移就可以了,基于此,我們可以將數組 a 和數組 b 排序后雙指針轉移,這樣時間復雜度就降低到了 O( n + m )

因為維護 m 個 bit 數組的 與 運算還是需要 n * m 的時間復雜度的,所以總的時間復雜度為?

代碼:
?

#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> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=150000+100;bitset<N>ans,t;vector<pair<int,int>>a,b;int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;scanf("%d%d",&n,&m);for(int i=0;i<n;i++){int num;scanf("%d",&num);a.emplace_back(num,i);}for(int i=0;i<m;i++){int num;scanf("%d",&num);b.emplace_back(num,i);}sort(a.begin(),a.end());sort(b.begin(),b.end());int pos=n-1;ans.set();for(int i=m-1;i>=0;i--){while(pos>=0&&a[pos].first>=b[i].first){t.set(a[pos].second);pos--;}ans&=t>>b[i].second;}printf("%d\n",ans.count());return 0; }

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的牛客多校2 - Greater and Greater(bitset优化暴力)的全部內容,希望文章能夠幫你解決所遇到的問題。

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