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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CodeForces - 1353D Constructing the Array(bfs)

發布時間:2024/4/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1353D Constructing the Array(bfs) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 n ,初始時全部為 0 的數組 a ,后續進行 n 次操作,每次操作找到最長的連續 0 ,如果有多個則選擇位置最靠左邊的,將這組連續?0 的,最中間位置的數賦值為 i ,i 為第 i 次操作,輸出最后的數列

題目分析:一開始沒什么思路,用線段樹區間合并暴力模擬的,果不其然 TLE 了,其實自己手算模擬幾次就會發現,每個位置至多會被遍歷到一次,且是按照深度擴展的,所以我們可以用 bfs + 優先隊列 不斷擴展就好了,具體實現可以看代碼

代碼:
?

#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> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;int ans[N],pos;struct Node {int l,r;Node(int l,int r):l(l),r(r){}bool operator<(const Node& t)const{if(r-l+1!=t.r-t.l+1)return r-l+1<t.r-t.l+1;return l>t.l;} };void bfs(int l,int r) {priority_queue<Node>q;q.push(Node(l,r));while(q.size()){Node cur=q.top();q.pop();l=cur.l;r=cur.r;int mid=l+r>>1;ans[mid]=++pos;if(l==r)continue;if(mid+1<=r)q.push(Node(mid+1,r));if(l<=mid-1)q.push(Node(l,mid-1));} }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){int n;scanf("%d",&n);pos=0;bfs(1,n);for(int i=1;i<=n;i++)printf("%d ",ans[i]);puts("");}return 0; }

?

總結

以上是生活随笔為你收集整理的CodeForces - 1353D Constructing the Array(bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。

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