巡逻
题目描述
可怜家里雇佣了
这天,可怜一不小心把存储巡逻顺序的文件删了,凭借着记忆,可怜回忆出了部分的巡逻顺序,即可怜回忆起了一个长度为
现在可怜想让你帮他还原出原来的巡逻顺序。满足巡逻顺序可能有很多,你需要给出字典序最小的那个。
输入格式
第一行输出两个整数
接下来
输出格式
输出
题解
这是一道难得的送分题。
我们把
单个元素和串首元素排序不受限制。那我们把这些元素加入小根堆。每次输出堆中的最小元素。若堆顶是串上的元素,那就把它弹出后把它儿子加进去。最后输出的就是字典序序列。时间复杂度
然而有
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cctype>
#define reint register int
using namespace std;
const int N=100005;
int n,m,fa[N],a[N],son[N];
priority_queue< int,vector<int>,greater<int> >hp;
void read(int &in){
int x=0;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
in=x;return;
}
int main(){
read(n),read(m);
for(reint i=1;i<=m;i++)read(a[i]);
for(reint i=2;i<=m;i++)
fa[a[i]]=a[i-1],son[a[i-1]]=a[i];
for(reint i=1;i<=n;i++)
if(!fa[i])hp.push(i);
reint u;
while(!hp.empty()){
u=hp.top(),hp.pop();
printf("%d\n",u);
if(son[u])hp.push(son[u]);
}return 0;
}