Question
給你一串長度爲,值爲的數組,每次找到最長子串,將其中間(奇數中間,若爲偶數則選)
Solution
優先隊列模擬
利用優先隊列保存,每次保存去掉其中間之後的滿足題意的子區間,
優先隊列是用運算符,但是排序的順序和sort是相反的,所以裏面的符號要和sort時用的寫起來相反即可。
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 5;
const ll N = 2e5 + 5;
struct node{
int l,r;
bool operator < (const node &T) const{
if(r-l==T.r-T.l) return r>T.r;
return r-l<T.r-T.l;
}
};
void solve(){
int n;cin>>n;
priority_queue<node>q;
q.push({1,n});
int cnt=0;
vector<int> a(n+1);
while(!q.empty()){
cnt++;
int L=q.top().l;
int R=q.top().r;
q.pop();
int mid;
if((R-L+1)&1)
mid=(L+R)/2;
else
mid=(L+R-1)/2;
a[mid]=cnt;
if(L<=mid-1 && L>=1) q.push({L,mid-1});
if(R>=mid+1 && R<=n) q.push({mid+1,R});
}
for(int i=1;i<=n;i++){
cout<<a[i]<<" \n"[i==n];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--){
solve();
}
return 0;
}