Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)C

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string s;
pair<int,int>p[200007];
int ans[200007];
char b[200007];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i){
int m;
cin>>m>>s;
if(m==1){
cout<<1<<"\n";
continue;
}
for(int j=0;j<m;++j)
p[j]={s[j]-'0',j};
sort(p,p+m);
int pos=m;
ans[p[0].second]=1;
for(int j=1;j<m;++j){
if(p[j].second<p[j-1].second){//遇到比前一个数字大的并且出现的位置在它之前的
pos=j;//就此截断
break;
}
else
ans[p[j].second]=1;//出现的位置比前一个晚,都染成1没问题
}
int now=pos;
while(now<m&&p[now].first==p[pos].first&&p[now].second<p[pos-1].second){
++now;
ans[p[now-1].second]=2;//数字和pos截断点的数字一样大并且出现的位置也都小于截断点之前的位置,全部染成2
}
for(int j=now;j<m;++j){
if(p[j].first==p[pos].first)//此时now所在位置的元素要么比pos所在的元素要大,要么出现的位置就比pos要晚
ans[p[j].second]=1;//如果是出现的位置晚的话染成1
else
ans[p[j].second]=2;//如果是更大的话染成2
}
int cnt=0;
for(int j=0;j<m;++j)
if(ans[j]==1)
b[++cnt]=s[j];
for(int j=0;j<m;++j)
if(ans[j]==2)
b[++cnt]=s[j];
int flag=0;
for(int j=2;j<=m;++j)
if(b[j]<b[j-1])//把染好色的数组分别按照1,2放进b数组进行检查,如果不是非递减序,则输出-,否则输出染色答案
flag=1;
if(flag)
cout<<"-";
else
for(int j=0;j<m;++j)
cout<<ans[j];
cout<<"\n";
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11522806.html