题解 (by;zjvarphi)
所有非 (nan) 的数在排完序后一定是升序的,所以只要考虑 (nan) 的位置。
当出现一个 (nan) 时,剩下的所有数都会比它大,所以它的位置确定。
当出现一个数时,所有小于它的数都会到它前面,就会对在它后面的 (nan) 造成贡献。
每次遇到数时更新一下贡献即可。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
static const int N=5e5+7;
int st[N],num[N],be[N],T,n;
bool vis[N];
char s[107];
struct node{bool nan;int x;}pnt[N];
inline int main() {
FI=freopen("qsort.in","r",stdin);
FO=freopen("qsort.out","w",stdout);
scanf("%d",&T);
for (ri z(1);z<=T;pd(z)) {
scanf("%d",&n);
int cnt=0,add=0,mx=0;
for (ri i(1);i<=n;pd(i)) {
scanf("%s",s+1);
if (s[1]=='n') vis[i]=true;
else {
vis[i]=false;
int pt=1,res=0;
while(s[pt]) {
res=res*10+s[pt]-'0';
++pt;
}
be[i]=st[++cnt]=res;
}
}
std::sort(st+1,st+cnt+1);
for (ri i(1);i<=n;pd(i))
if (!vis[i]) {
add=cmax(add,(int)(std::lower_bound(st+1,st+cnt+1,be[i])-st-1));
if (mx<=be[i]) mx=be[i],++add;
} else ++num[add];
for (ri i(1);i<=num[0];pd(i)) printf("nan ");
for (ri i(1);i<=cnt;pd(i)) {
printf("%d ",st[i]);
for (ri j(1);j<=num[i];pd(j)) printf("nan ");
}
printf("
");
memset(num,0,sizeof(int)*(n+1));
}
return 0;
}
}
int main() {return nanfeng::main();}