CF1353D Constructing the array(优先队列)

题意:

给出一串初始值全为0的序列,每次操作找到最长的连续为0的子区间,如果有并列则取靠左的那个,修改区间的中间值为对应操作的编号,询问最后的序列状态。

题解:

枚举最大子区间,一开始想到的是双指针法,结果TLE了,看了网上的题解才知道可以利用题目的性质用优先队列做,太神奇了。。。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
struct node {
    int l,r,len;
    bool operator < (const node &p) const {
        if (len==p.len)
            return l>=p.l;
        else
            return len<p.len;
    }
};
priority_queue<node> q;
int a[maxn];
int T,N;
int main () {
    scanf("%d",&T);
    while (T--) {
        scanf("%d",&N);
        q.push({1,N,N});
        int tot=0;
        while (!q.empty()) {
            node u=q.top();
            q.pop();
            int mid=(u.l+u.r)>>1;
            a[mid]=++tot;
            if (u.l<=mid-1) q.push({u.l,mid-1,mid-u.l});
            if (mid+1<=u.r) q.push({mid+1,u.r,u.r-mid});
        }
        for (int i=1;i<=N;i++) printf("%d ",a[i]);
        printf("
");
    }
}
原文地址:https://www.cnblogs.com/zhanglichen/p/12900907.html