Cod sursa(job #137275)

Utilizator vlad_popaVlad Popa vlad_popa Data 17 februarie 2008 10:45:20
Problema Nivele Scor 100
Compilator cpp Status done
Runda preONI 2008, Runda 4, Clasele 11-12 Marime 1.53 kb
#include <cstdio>

#define MAXN 50001

int V[MAXN];
int S[MAXN];
int N, check;

void read ()
{
    scanf ("%d ", &N);
    check = 1;
    
    int i;
    for (i = 1; i <= N; ++ i)
    {
        scanf ("%d ", V + i);
        if (V[i] > N)
            check = 0;
        S[i] = i - 1;
    }
}

/*void print ()
{
    int i;
    
    for (i = 1; i <= N; ++ i)
        printf ("%d ", V[i]);
    printf ("\n");
    
    for (i = 1; i <= N; ++ i)
        printf ("%d ", S[i]);
    printf ("\n");
}*/

void solve ()
{
    int i = 1, j;
    
    if (!check)
    {
        printf ("NU\n");
        return;
    }
    
    while (V[N] != 1)
    {
        if (i == N)
        {
            printf ("NU\n");
            return;
        }
        
        if (V[i] == V[i + 1])
        {
            V[i] = 0;
            -- V[i + 1];
            S[i + 1] = S[i];
            j = S[i + 1];
            //print();
            
            while (V[j] == V[i + 1])
            {
                V[j] = 0;
                -- V[i + 1];
                S[i + 1] = S[j];
                j = S[j];
                //print ();
            }
        }

        ++ i;        
    }
    
    if (V[N] == 1)
        printf ("DA\n");
    else
        printf ("NU\n");
}

int
 main ()
{
    freopen ("nivele.in", "rt", stdin);
    freopen ("nivele.out", "wt", stdout);
    
    int T;
    scanf ("%d\n", &T);
    
    while (T--)
    {
        read ();
        solve ();
    }
    
    return 0;
}