fork download
#include <bits/stdc++.h>
#define LSOne(s) ((S) & (-S))

using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

void solve(){
    ll n,i,a,k,t,m,actual,ini,fin;
    cin>>n;
    vector<pair<ll,ll> > datos(n);
    for(i=0;i<n;i++){
        cin>>datos[i].first;
        datos[i].second=i;
    }
    ll conteo=0;
    vector<ll> elementos(n);
    sort(datos.begin(),datos.end());
    conteo=0;
    actual=n;
    ini=0;
    fin=n;
    bool posible=1;
    ll aux;
    ll positivos=0;
    while(ini<fin && actual>0){
        if(conteo<datos[ini].first && ini<fin){
            while(conteo<datos[ini].first && ini<fin){
                aux = fin;
                while(fin>0 && datos[fin-1].first==datos[fin-2].first){
                    elementos[datos[fin-1].second]=actual;
                    fin--;
                    conteo++;
                    positivos++;
                }
                if(fin-ini+positivos!=datos[fin-1].first){
                    posible=0;
                }
                elementos[datos[fin-1].second]=actual;
                fin--;
                conteo++;
                positivos++;
            }
            actual--;
        }
        if(ini>=fin)continue;
        if(actual==0){
            posible=0;
            continue;
        }
        if(conteo==datos[ini].first){
            while(datos[ini].first==datos[ini+1].first && ini<fin-1){
                elementos[datos[ini].second]=-actual;
                ini++;
            }
            elementos[datos[ini].second]=-actual;
            ini++;
        }else{
            posible=0;
        }
        actual--;
    }
    if(posible){
        cout<<"YES\n";
        for(i=0;i<n;i++){
            cout<<elementos[i]<<" ";
        }
        cout<<"\n";
    }else{
        cout<<"NO\n";
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t;
    cin>>t;
    while(t--)solve();
    return 0;
}
Runtime error #stdin #stdout #stderr 0.01s 5504KB
stdin

35
3
1 1 3 
2
2 0 
6
5 0 3 1 5 5 
1
1 
1
0 
3
0 2 1 
3
0 0 0 
4
2 4 0 0 
3
1 1 0 
2
0 1 
2
1 2 
5
3 4 3 1 2 
1
1 
6
0 1 1 2 1 0 
3
0 2 3 
1
0 
6
5 2 0 1 0 1 
4
3 1 3 3 
2
2 2 
2
2 2 
3
1 1 0 
6
2 6 6 1 3 1 
3
0 3 0 
6
5 0 6 4 5 3 
1
0 
3
2 2 3 
2
0 0 
5
5 5 1 4 4 
4
3 2 2 1 
7
6 4 3 6 0 5 7 
7
0 0 1 0 6 4 3 
4
4 1 1 0 
2
2 1 
6
6 3 4 6 1 1 
2
0 2 

stdout
Standard output is empty
stderr
prog: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.